]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - Classes/class-hook.php
Add ability to add and delete access to the panel
[irc/unrealircd/unrealircd-webpanel.git] / Classes / class-hook.php
1 <?php
2
3 define('HOOKTYPE_NAVBAR', 100); /* The Hook for the navigation bar */
4 define('HOOKTYPE_PRE_HEADER', 101); /* The hook for pre-header */
5
6 /**
7 * Class for "Hook"
8 * This is the main function which gets called whenever you want to use a Hook.
9 *
10 * Example:
11 * Calling the Hook using a function:
12 * Hook::func(HOOKTYPE_NAVBAR, 'bob');
13 *
14 * This Hook references the function 'bob', and will run this
15 * function bob
16 * {
17 * echo "We rehashed!";
18 * }
19 *
20 * Example 2:
21 * Calling the Hook using an initialized object class method:
22 * Hook::func(HOOKTYPE_NAVBAR, [$this, 'method']);
23 *
24 * Example 3:
25 * Calling the Hook using a static class method:
26 * Hook::func(HOOKTYPE_NAVBAR, 'classname::method');
27 *
28 */
29 class Hook {
30
31 /** A static list of Hooks and their associated functions */
32 private static $actions = [];
33
34 /** Runs a Hook.
35 * The parameter for $Hook should be a "HOOKTYPE_" as defined in hook.php
36 * @param string $Hook The define or string name of the Hook. For example, HOOKTYPE_REHASH.
37 * @param array &$args The array of information you are sending along in the Hook, so that other functions may see and modify things.
38 * @return void Does not return anything.
39 *
40 */
41 public static function run($Hook, &$args = array())
42 {
43 if (!empty(self::$actions[$Hook]))
44 foreach (self::$actions[$Hook] as &$f)
45 $f($args);
46
47 }
48
49 /** Calls a Hook
50 * @param string $Hook The define or string name of the Hook. For example, HOOKTYPE_REHASH.
51 * @param string|Closure $function This is a string reference to a Closure function or a class method.
52 * @return void Does not return anything.
53 */
54 public static function func($Hook, $function)
55 {
56 self::$actions[$Hook][] = $function;
57 }
58
59 /** Deletes a Hook
60 * @param string $Hook The Hook from which we are removing a function reference.
61 * @param string $function The name of the function that we are removing.
62 * @return void Does not reuturn anything.
63 */
64
65 public static function del($Hook, $function)
66 {
67 for ($i = 0; isset(self::$actions[$Hook][$i]); $i++)
68 if (self::$actions[$Hook][$i] == $function)
69 array_splice(self::$actions[$Hook],$i);
70 }
71 }