3 * @file src/Core/Hook.php
5 namespace Friendica\Core;
8 use Friendica\BaseObject;
9 use Friendica\Database\DBA;
12 * Some functions to handle hooks
14 class Hook extends BaseObject
17 * Array of registered hooks
21 * ["<hook name>"] => [
23 * 1 => "<hook function name>"
30 private static $hooks = [];
35 public static function loadHooks()
38 $stmt = DBA::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
40 while ($hook = DBA::fetch($stmt)) {
41 self::add($hook['hook'], $hook['file'], $hook['function']);
47 * @brief Adds a new hook to the hooks array.
49 * This function is meant to be called by modules on each page load as it works after loadHooks has been called.
53 * @param type $function
55 public static function add($hook, $file, $function)
57 if (!array_key_exists($hook, self::$hooks)) {
58 self::$hooks[$hook] = [];
60 self::$hooks[$hook][] = [$file, $function];
64 * @brief Registers a hook.
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.
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)
74 public static function register($hook, $file, $function, $priority = 0)
76 $file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
78 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
79 if (DBA::exists('hook', $condition)) {
83 $result = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
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
96 public static function unregister($hook, $file, $function)
98 $relative_file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
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);
104 $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
105 $result = DBA::delete('hook', $condition);
110 * Returns the list of callbacks for a single hook
112 * @param string $name Name of the hook
115 public static function getByName($name)
119 if (isset(self::$hooks[$name])) {
120 $return = self::$hooks[$name];
127 * @brief Forks a hook.
129 * Use this function when you want to fork a hook via the worker.
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
135 public static function fork($priority, $name, $data = null)
137 if (array_key_exists($name, self::$hooks)) {
138 foreach (self::$hooks[$name] as $hook) {
139 // Call a hook to check if this hook call needs to be forked
140 $hookdata = ['name' => $name, 'data' => $data, 'execute' => true];
142 if (array_key_exists('hook_fork', self::$hooks)) {
143 foreach (self::$hooks['hook_fork'] as $fork_hook) {
144 if ($hook[0] != $fork_hook[0]) {
147 self::callSingle(self::getApp(), 'hook_fork', $fork_hook, $hookdata);
151 if (!$hookdata['execute']) {
155 Worker::add($priority, 'ForkHook', $name, $hook, $data);
161 * @brief Calls a hook.
163 * Use this function when you want to be able to allow a hook to manipulate
166 * @param string $name of the hook to call
167 * @param string|array &$data to transmit to the callback handler
169 public static function callAll($name, &$data = null)
171 if (array_key_exists($name, self::$hooks)) {
172 foreach (self::$hooks[$name] as $hook) {
173 self::callSingle(self::getApp(), $name, $hook, $data);
179 * @brief Calls a single hook.
182 * @param string $name of the hook to call
183 * @param array $hook Hook data
184 * @param string|array &$data to transmit to the callback handler
186 public static function callSingle(App $a, $name, $hook, &$data = null)
188 // Don't run a theme's hook if the user isn't using the theme
189 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
193 @include_once($hook[0]);
194 if (function_exists($hook[1])) {
198 // remove orphan hooks
199 $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
200 DBA::delete('hook', $condition, ['cascade' => false]);
205 * Checks if an app_menu hook exist for the provided addon name.
206 * Return true if the addon is an app
208 * @param string $name Name of the addon
211 public static function isAddonApp($name)
213 if (array_key_exists('app_menu', self::$hooks)) {
214 foreach (self::$hooks['app_menu'] as $hook) {
215 if ($hook[0] == 'addon/' . $name . '/' . $name . '.php') {