-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathHook.php
45 lines (39 loc) · 1.06 KB
/
Hook.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace FOO;
/**
* Class Hook
* Provides functionality to hook into 411 code.
* @package FOO
*/
class Hook {
/** @var callable[] Hook map. */
private static $map = [];
/**
* Call any hooks registered at this site.
* @param string $hook Name of the hook.
* @param mixed[] $args Arguments.
* @return mixed[] Arguments.
*/
public static function call($hook, $args=[]) {
if(!array_key_exists($hook, self::$map)) {
return $args;
}
foreach(self::$map[$hook] as $func) {
$args = $func($args);
}
return $args;
}
/**
* Register a hook. The hooking function should accept an array of
* values. It should make any modifications as necessary and return
* the array.
* @param $hook Name of the hook.
* @param callable $func A function.
*/
public static function register($hook, $func) {
if(!array_key_exists($hook, self::$map)) {
self::$map[$hook] = [];
}
self::$map[$hook][] = $func;
}
}