20
20
*/
21
21
class Hook {
22
22
23
- /**
24
- * Available hooks.
25
- *
26
- * @since 1.0.0
27
- *
28
- * @deprecated 1.0.3
29
- *
30
- * @var array
31
- */
32
- private static $ _hooks = [
33
- 'meta ' ,
34
- 'css ' ,
35
- 'after-body ' ,
36
- 'footer ' ,
37
- 'js ' ,
38
- 'launch ' ,
39
- 'routes '
40
- ];
41
-
42
23
/**
43
24
* Callbacks.
44
25
*
@@ -115,73 +96,6 @@ public static function setSingletonName($method) {
115
96
self ::$ singleton = $ method ;
116
97
}
117
98
118
- /**
119
- * Add hook/hooks to hook list.
120
- *
121
- * @since 1.0.0
122
- *
123
- * @deprecated 1.0.3 This method will be removed in the next version
124
- *
125
- * @param string|array $where → hook to add
126
- *
127
- * @return int → number hooks added
128
- */
129
- public static function setHook ($ where ) {
130
-
131
- if (!is_array ($ where )) {
132
-
133
- self ::$ _hooks [$ where ] = '' ;
134
-
135
- return 1 ;
136
- }
137
-
138
- foreach ($ where as $ where ) {
139
-
140
- self ::$ _hooks [$ where ] = '' ;
141
- }
142
-
143
- return count ($ where );
144
- }
145
-
146
- /**
147
- * Attach custom function to hook.
148
- *
149
- * @since 1.0.0
150
- *
151
- * @deprecated 1.0.3 This method will be replaced by addAction
152
- * and removed in the next version
153
- *
154
- * @param array|string $where → hook to use
155
- * @param string $function → function to attach to hook
156
- *
157
- * @throws HookException → hook location not defined
158
- * @return boolean → success with adding
159
- */
160
- public static function addHook ($ where , $ function = '' ) {
161
-
162
- if (!is_array ($ where )) {
163
-
164
- $ where = [$ where => $ function ];
165
- }
166
-
167
- foreach ($ where as $ hook => $ function ) {
168
-
169
- if (!isset (self ::$ _hooks [$ hook ])) {
170
-
171
- $ message = 'Hook location not defined ' ;
172
-
173
- throw new HookException ($ message . ': ' . $ hook , 811 );
174
- }
175
-
176
- $ theseHooks = explode ('| ' , self ::$ _hooks [$ hook ]);
177
- $ theseHooks [] = $ function ;
178
-
179
- self ::$ _hooks [$ hook ] = implode ('| ' , $ theseHooks );
180
- }
181
-
182
- return true ;
183
- }
184
-
185
99
/**
186
100
* Attach custom function to action hook.
187
101
*
@@ -224,97 +138,6 @@ public static function addActions($actions) {
224
138
return true ;
225
139
}
226
140
227
- /**
228
- * Reset custom function to hook.
229
- *
230
- * @since 1.0.2
231
- *
232
- * @deprecated 1.0.3 This method will be removed in the next version
233
- *
234
- * @param array|string $where → hook to remove
235
- *
236
- * @return boolean
237
- */
238
- public static function resetHook ($ where ) {
239
-
240
- if (isset (self ::$ _hooks [$ where ])) {
241
-
242
- self ::$ _hooks [$ where ] = '' ;
243
-
244
- return true ;
245
- }
246
-
247
- return false ;
248
- }
249
-
250
- /**
251
- * Run all hooks attached to the hook.
252
- *
253
- * By default it will look for getInstance method to use singleton
254
- * pattern and create a single instance of the class. If it does not
255
- * exist it will create a new object.
256
- *
257
- * @see setSingletonName() for change the method name.
258
- *
259
- * @since 1.0.0
260
- *
261
- * @deprecated 1.0.3 This method will be replaced by doAction
262
- * and removed in the next version
263
- *
264
- * @param string $where → hook to run
265
- * @param string $args → optional arguments
266
- *
267
- * @throws HookException → the hook is not yet known
268
- * @return object|false → returns the calling function
269
- */
270
- public static function run ($ where , $ args ='' ) {
271
-
272
- if (!isset (self ::$ _hooks [$ where ])) {
273
-
274
- $ message = 'Hook location not defined ' ;
275
-
276
- throw new HookException ($ message . ': ' . $ where , 811 );
277
- }
278
-
279
- $ theseHooks = explode ('| ' , self ::$ _hooks [$ where ]);
280
-
281
- foreach ($ theseHooks as $ hook ) {
282
-
283
- if (preg_match ("/@/i " , $ hook )) {
284
-
285
- $ parts = explode ('/ ' , $ hook );
286
-
287
- $ last = end ($ parts );
288
-
289
- $ segments = explode ('@ ' , $ last );
290
-
291
- $ class = $ segments [0 ];
292
-
293
- $ method = $ segments [1 ];
294
-
295
- if (method_exists ($ class , self ::$ singleton )) {
296
-
297
- $ instance = call_user_func ([$ class , self ::$ singleton ]);
298
-
299
- call_user_func ([$ instance , $ method ], $ args );
300
-
301
- continue ;
302
- }
303
-
304
- $ instance = new $ class ;
305
-
306
- call_user_func ([$ instance , $ method ], $ args );
307
-
308
- } else {
309
-
310
- if (function_exists ($ hook )) {
311
-
312
- call_user_func ($ hook , $ result );
313
- }
314
- }
315
- }
316
- }
317
-
318
141
/**
319
142
* Run all hooks attached to the hook.
320
143
*
@@ -329,6 +152,8 @@ public static function run($where, $args='') {
329
152
* @param string $tag → action hook name
330
153
* @param mixed $args → optional arguments
331
154
* @param boolean $remove → delete hook after executing actions
155
+ *
156
+ * @return returns the output of the last action or false
332
157
*/
333
158
public static function doAction ($ tag , $ args = [], $ remove = true ) {
334
159
@@ -351,11 +176,13 @@ public static function doAction($tag, $args = [], $remove = true) {
351
176
352
177
foreach ($ priority as $ action ) {
353
178
354
- self ::_runAction ($ action , $ args );
179
+ $ action = self ::_runAction ($ action , $ args );
355
180
}
356
181
}
357
182
358
183
self ::$ current = false ;
184
+
185
+ return (isset ($ action )) ? $ action : false ;
359
186
}
360
187
361
188
/**
@@ -469,25 +296,4 @@ public static function current() {
469
296
470
297
return self ::$ current ;
471
298
}
472
-
473
- /**
474
- * Execute hooks attached to run and collect instead of running.
475
- *
476
- * @since 1.0.0
477
- *
478
- * @deprecated 1.0.3 This method will be removed in the next version
479
- *
480
- * @param string $where → hook
481
- * @param string $args → optional arguments
482
- *
483
- * @return object → returns output of hook call
484
- */
485
- public function collectHook ($ where , $ args = null ) {
486
-
487
- ob_start ();
488
-
489
- echo $ this ->run ($ where , $ args );
490
-
491
- return ob_get_clean ();
492
- }
493
299
}
0 commit comments