]> git.mxchange.org Git - friendica.git/blob - src/Core/Hook.php
Misspelling in use statement
[friendica.git] / src / Core / Hook.php
1 <?php
2 /**
3  * @file src/Core/Hook.php
4  */
5 namespace Friendica\Core;
6
7 use Friendica\App;
8 use Friendica\BaseObject;
9 use Friendica\Database\DBA;
10
11 /**
12  * Some functions to handle hooks
13  */
14 class Hook extends BaseObject
15 {
16         /**
17          * Array of registered hooks
18          *
19          * Format:
20          * [
21          *              ["<hook name>"] => [
22          *                      0 => "<hook file>",
23          *                      1 => "<hook function name>"
24          *              ],
25          *              ...
26          * ]
27          *
28          * @var array
29          */
30         private static $hooks = [];
31
32         /**
33          * Load hooks
34          */
35         public static function loadHooks()
36         {
37                 self::$hooks = [];
38                 $stmt = DBA::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
39
40                 while ($hook = DBA::fetch($stmt)) {
41                         self::add($hook['hook'], $hook['file'], $hook['function']);
42                 }
43                 DBA::close($stmt);
44         }
45
46         /**
47          * @brief Adds a new hook to the hooks array.
48          *
49          * This function is meant to be called by modules on each page load as it works after loadHooks has been called.
50          *
51          * @param type $hook
52          * @param type $file
53          * @param type $function
54          */
55         public static function add($hook, $file, $function)
56         {
57                 if (!array_key_exists($hook, self::$hooks)) {
58                         self::$hooks[$hook] = [];
59                 }
60                 self::$hooks[$hook][] = [$file, $function];
61         }
62
63         /**
64          * @brief Registers a hook.
65          *
66          * This function is meant to be called once when an addon is enabled for example as it doesn't add to the current hooks.
67          *
68          * @param string $hook     the name of the hook
69          * @param string $file     the name of the file that hooks into
70          * @param string $function the name of the function that the hook will call
71          * @param int    $priority A priority (defaults to 0)
72          * @return mixed|bool
73          */
74         public static function register($hook, $file, $function, $priority = 0)
75         {
76                 $file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
77
78                 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
79                 if (DBA::exists('hook', $condition)) {
80                         return true;
81                 }
82
83                 $result = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
84
85                 return $result;
86         }
87
88         /**
89          * Unregisters a hook.
90          *
91          * @param string $hook     the name of the hook
92          * @param string $file     the name of the file that hooks into
93          * @param string $function the name of the function that the hook called
94          * @return boolean
95          */
96         public static function unregister($hook, $file, $function)
97         {
98                 $relative_file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
99
100                 // This here is only needed for fixing a problem that existed on the develop branch
101                 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
102                 DBA::delete('hook', $condition);
103
104                 $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
105                 $result = DBA::delete('hook', $condition);
106                 return $result;
107         }
108
109         /**
110          * Returns the list of callbacks for a single hook
111          *
112          * @param  string $name Name of the hook
113          * @return array
114          */
115         public static function getByName($name)
116         {
117                 $return = [];
118
119                 if (isset(self::$hooks[$name])) {
120                         $return = self::$hooks[$name];
121                 }
122
123                 return $return;
124         }
125
126         /**
127          * @brief Forks a hook.
128          *
129          * Use this function when you want to fork a hook via the worker.
130          *
131          * @param integer $priority of the hook
132          * @param string  $name     of the hook to call
133          * @param mixed   $data     to transmit to the callback handler
134          */
135         public static function fork($priority, $name, $data = null)
136         {
137                 if (array_key_exists($name, self::$hooks)) {
138                         foreach (self::$hooks[$name] as $hook) {
139                                 Worker::add($priority, 'ForkHook', $name, $hook, $data);
140                         }
141                 }
142         }
143
144         /**
145          * @brief Calls a hook.
146          *
147          * Use this function when you want to be able to allow a hook to manipulate
148          * the provided data.
149          *
150          * @param string       $name  of the hook to call
151          * @param string|array &$data to transmit to the callback handler
152          */
153         public static function callAll($name, &$data = null)
154         {
155                 if (array_key_exists($name, self::$hooks)) {
156                         foreach (self::$hooks[$name] as $hook) {
157                                 self::callSingle(self::getApp(), $name, $hook, $data);
158                         }
159                 }
160         }
161
162         /**
163          * @brief Calls a single hook.
164          *
165          * @param App $a
166          * @param string         $name of the hook to call
167          * @param array          $hook Hook data
168          * @param string|array   &$data to transmit to the callback handler
169          */
170         public static function callSingle(App $a, $name, $hook, &$data = null)
171         {
172                 // Don't run a theme's hook if the user isn't using the theme
173                 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
174                         return;
175                 }
176
177                 @include_once($hook[0]);
178                 if (function_exists($hook[1])) {
179                         $func = $hook[1];
180                         $func($a, $data);
181                 } else {
182                         // remove orphan hooks
183                         $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
184                         DBA::delete('hook', $condition, ['cascade' => false]);
185                 }
186         }
187
188         /**
189          * Checks if an app_menu hook exist for the provided addon name.
190          * Return true if the addon is an app
191          *
192          * @param string $name Name of the addon
193          * @return boolean
194          */
195         public static function isAddonApp($name)
196         {
197                 if (array_key_exists('app_menu', self::$hooks)) {
198                         foreach (self::$hooks['app_menu'] as $hook) {
199                                 if ($hook[0] == 'addon/' . $name . '/' . $name . '.php') {
200                                         return true;
201                                 }
202                         }
203                 }
204
205                 return false;
206         }
207 }