@@ -51,6 +51,19 @@ class Hook implements Iterator, ArrayAccess
51
51
*/
52
52
private $ doingAction = false ;
53
53
54
+ /**
55
+ * @var Transport
56
+ */
57
+ private $ transport ;
58
+
59
+ /**
60
+ * Hook Constructor
61
+ */
62
+ public function __construct ()
63
+ {
64
+ $ this ->transport = new Transport ([]);
65
+ }
66
+
54
67
/**
55
68
* Reveal the callbacks
56
69
*
@@ -63,56 +76,59 @@ public function getCallBacks()
63
76
64
77
/**
65
78
* Hooks a function or method to a specific filter action.
79
+ * @return Transport
66
80
*/
67
81
public function addFilter (string $ tag , $ functionToAdd , int $ priority )
68
82
{
69
83
$ idx = $ this ->uniqueId ($ tag , $ functionToAdd , $ priority );
70
- $ priority_existed = isset ($ this ->callbacks [$ priority ]);
84
+ $ priorityExisted = isset ($ this ->callbacks [$ priority ]);
71
85
72
86
$ this ->callbacks [$ priority ][$ idx ] = [
73
87
'function ' => $ functionToAdd ,
74
88
];
75
89
76
90
// if we're adding a new priority to the list, put them back in sorted order
77
- if (!$ priority_existed && count ($ this ->callbacks ) > 1 ) {
91
+ if (!$ priorityExisted && count ($ this ->callbacks ) > 1 ) {
78
92
ksort ($ this ->callbacks , SORT_NUMERIC );
79
93
}
80
94
81
95
if ($ this ->nestingLevel > 0 ) {
82
- $ this ->resortActiveIterations ($ priority , $ priority_existed );
96
+ $ this ->resortActiveIterations ($ priority , $ priorityExisted );
83
97
}
98
+
99
+ return $ this ->transport ;
84
100
}
85
101
86
102
/**
87
103
* Handles resetting callback priority keys mid-iteration.
88
104
*
89
- * @param bool|int $new_priority Optional. The priority of the new filter being added. Default false,
105
+ * @param bool|int $newPriority Optional. The priority of the new filter being added. Default false,
90
106
* for no priority being added.
91
107
* @param bool $priorityExisted Optional. Flag for whether the priority already existed before the new
92
108
* filter was added. Default false.
93
109
*
94
110
*/
95
- private function resortActiveIterations ($ new_priority = false , bool $ priorityExisted = false )
111
+ private function resortActiveIterations ($ newPriority = false , bool $ priorityExisted = false )
96
112
{
97
- $ new_priorities = array_keys ($ this ->callbacks );
113
+ $ newPriorities = array_keys ($ this ->callbacks );
98
114
99
115
// If there are no remaining hooks, clear out all running iterations.
100
- if (!$ new_priorities ) {
116
+ if (!$ newPriorities ) {
101
117
foreach ($ this ->iterations as $ index => $ iteration ) {
102
- $ this ->iterations [$ index ] = $ new_priorities ;
118
+ $ this ->iterations [$ index ] = $ newPriorities ;
103
119
}
104
120
return ;
105
121
}
106
122
107
- $ min = min ($ new_priorities );
123
+ $ min = min ($ newPriorities );
108
124
foreach ($ this ->iterations as $ index => &$ iteration ) {
109
125
$ current = current ($ iteration );
110
126
// If we're already at the end of this iteration, just leave the array pointer where it is.
111
127
if (false === $ current ) {
112
128
continue ;
113
129
}
114
130
115
- $ iteration = $ new_priorities ;
131
+ $ iteration = $ newPriorities ;
116
132
117
133
if ($ current < $ min ) {
118
134
array_unshift ($ iteration , $ current );
@@ -125,7 +141,7 @@ private function resortActiveIterations($new_priority = false, bool $priorityExi
125
141
}
126
142
}
127
143
128
- if ($ new_priority === $ this ->currentPriority [$ index ] && !$ priorityExisted ) {
144
+ if ($ newPriority === $ this ->currentPriority [$ index ] && !$ priorityExisted ) {
129
145
/*
130
146
* ... and the new priority is the same as what $this->iterations thinks is the previous
131
147
* priority, we need to move back to it.
@@ -141,7 +157,7 @@ private function resortActiveIterations($new_priority = false, bool $priorityExi
141
157
if (false === $ prev ) {
142
158
// Start of the array. Reset, and go about our day.
143
159
reset ($ iteration );
144
- } elseif ($ new_priority !== $ prev ) {
160
+ } elseif ($ newPriority !== $ prev ) {
145
161
// Previous wasn't the same. Move forward again.
146
162
next ($ iteration );
147
163
}
@@ -162,11 +178,11 @@ private function resortActiveIterations($new_priority = false, bool $priorityExi
162
178
*/
163
179
public function removeFilter (string $ tag , callable $ functionToRemove , int $ priority )
164
180
{
165
- $ function_key = $ this ->uniqueId ($ tag , $ functionToRemove , $ priority );
181
+ $ functionKey = $ this ->uniqueId ($ tag , $ functionToRemove , $ priority );
166
182
167
- $ exists = isset ($ this ->callbacks [$ priority ][$ function_key ]);
183
+ $ exists = isset ($ this ->callbacks [$ priority ][$ functionKey ]);
168
184
if ($ exists ) {
169
- unset($ this ->callbacks [$ priority ][$ function_key ]);
185
+ unset($ this ->callbacks [$ priority ][$ functionKey ]);
170
186
if (!$ this ->callbacks [$ priority ]) {
171
187
unset($ this ->callbacks [$ priority ]);
172
188
if ($ this ->nestingLevel > 0 ) {
@@ -191,13 +207,13 @@ public function hasFilter(string $tag = '', $functionToCheck = false)
191
207
return $ this ->hasFilters ();
192
208
}
193
209
194
- $ function_key = $ this ->uniqueId ($ tag , $ functionToCheck , false );
195
- if (!$ function_key ) {
210
+ $ functionKey = $ this ->uniqueId ($ tag , $ functionToCheck , false );
211
+ if (!$ functionKey ) {
196
212
return false ;
197
213
}
198
214
199
215
foreach ($ this ->callbacks as $ priority => $ callbacks ) {
200
- if (isset ($ callbacks [$ function_key ])) {
216
+ if (isset ($ callbacks [$ functionKey ])) {
201
217
return $ priority ;
202
218
}
203
219
}
@@ -294,37 +310,63 @@ private function uniqueId(string $tag, $function, $priority)
294
310
/**
295
311
* @param $value
296
312
* @param array $args
297
- * @return false|mixed
313
+ * @return Transport
298
314
*/
299
315
public function applyFilters ($ value , array $ args )
300
316
{
317
+ $ transport = $ this ->transport ;
318
+
319
+ $ transport ->setData ('_args ' , $ args );
320
+ $ transport ->setData ('_value ' , $ value );
321
+
322
+ $ transport ->setResult ($ value );
323
+
301
324
if (!$ this ->callbacks ) {
302
- return $ value ;
325
+ return $ transport ;
303
326
}
304
327
305
- $ nesting_level = $ this ->nestingLevel ++;
328
+ $ nestingLevel = $ this ->nestingLevel ++;
329
+
330
+ $ this ->iterations [$ nestingLevel ] = array_keys ($ this ->callbacks );
306
331
307
- $ this ->iterations [$ nesting_level ] = array_keys ($ this ->callbacks );
332
+ $ args [] = $ transport ;
333
+
334
+ $ valueClass = is_object ($ value ) ? get_class ($ value ) : null ;
308
335
309
336
do {
310
- $ this ->currentPriority [$ nesting_level ] = current ($ this ->iterations [$ nesting_level ]);
311
- $ priority = $ this ->currentPriority [$ nesting_level ];
337
+ $ this ->currentPriority [$ nestingLevel ] = current ($ this ->iterations [$ nestingLevel ]);
338
+
339
+ $ priority = $ this ->currentPriority [$ nestingLevel ];
312
340
313
341
foreach ($ this ->callbacks [$ priority ] as $ the_ ) {
314
342
if (!$ this ->doingAction ) {
315
343
$ args [0 ] = $ value ;
316
344
}
317
345
318
346
$ value = call_user_func_array ($ the_ ['function ' ], $ args );
347
+
348
+ if ($ value === null ) { // Return void
349
+ $ value = $ transport ->getData ('_value ' );
350
+ } else {
351
+ $ valueType = gettype ($ value );
352
+ $ resultType = gettype ($ transport ->getResult ());
353
+
354
+ $ transport ->setResult ($ value );
355
+
356
+ if ($ valueType !== $ resultType || // Not the same type
357
+ ($ valueType === 'object ' && $ valueClass && get_class ($ value ) !== $ valueClass )) { // Not the same class
358
+ $ value = $ transport ->getData ('_value ' );
359
+ }
360
+ }
319
361
}
320
- } while (false !== next ($ this ->iterations [$ nesting_level ]));
362
+ } while (false !== next ($ this ->iterations [$ nestingLevel ]));
321
363
322
- unset($ this ->iterations [$ nesting_level ]);
323
- unset($ this ->currentPriority [$ nesting_level ]);
364
+ unset($ this ->iterations [$ nestingLevel ]);
365
+ unset($ this ->currentPriority [$ nestingLevel ]);
324
366
325
367
$ this ->nestingLevel --;
326
368
327
- return $ value ;
369
+ return $ transport ;
328
370
}
329
371
330
372
/**
@@ -350,17 +392,17 @@ public function doAction(array $args)
350
392
*/
351
393
public function doAllHook (array &$ args )
352
394
{
353
- $ nesting_level = $ this ->nestingLevel ++;
354
- $ this ->iterations [$ nesting_level ] = array_keys ($ this ->callbacks );
395
+ $ nestingLevel = $ this ->nestingLevel ++;
396
+ $ this ->iterations [$ nestingLevel ] = array_keys ($ this ->callbacks );
355
397
356
398
do {
357
- $ priority = current ($ this ->iterations [$ nesting_level ]);
399
+ $ priority = current ($ this ->iterations [$ nestingLevel ]);
358
400
foreach ($ this ->callbacks [$ priority ] as $ the_ ) {
359
401
call_user_func_array ($ the_ ['function ' ], $ args );
360
402
}
361
- } while (false !== next ($ this ->iterations [$ nesting_level ]));
403
+ } while (false !== next ($ this ->iterations [$ nestingLevel ]));
362
404
363
- unset($ this ->iterations [$ nesting_level ]);
405
+ unset($ this ->iterations [$ nestingLevel ]);
364
406
$ this ->nestingLevel --;
365
407
}
366
408
0 commit comments