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 string $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)
73 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
75 public static function register($hook, $file, $function, $priority = 0)
77 $file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
79 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
80 if (DBA::exists('hook', $condition)) {
84 $result = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
92 * @param string $hook the name of the hook
93 * @param string $file the name of the file that hooks into
94 * @param string $function the name of the function that the hook called
96 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
98 public static function unregister($hook, $file, $function)
100 $relative_file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
102 // This here is only needed for fixing a problem that existed on the develop branch
103 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
104 DBA::delete('hook', $condition);
106 $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
107 $result = DBA::delete('hook', $condition);
112 * Returns the list of callbacks for a single hook
114 * @param string $name Name of the hook
117 public static function getByName($name)
121 if (isset(self::$hooks[$name])) {
122 $return = self::$hooks[$name];
129 * @brief Forks a hook.
131 * Use this function when you want to fork a hook via the worker.
133 * @param integer $priority of the hook
134 * @param string $name of the hook to call
135 * @param mixed $data to transmit to the callback handler
136 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
138 public static function fork($priority, $name, $data = null)
140 if (array_key_exists($name, self::$hooks)) {
141 foreach (self::$hooks[$name] as $hook) {
142 // Call a hook to check if this hook call needs to be forked
143 if (array_key_exists('hook_fork', self::$hooks)) {
144 $hookdata = ['name' => $name, 'data' => $data, 'execute' => true];
146 foreach (self::$hooks['hook_fork'] as $fork_hook) {
147 if ($hook[0] != $fork_hook[0]) {
150 self::callSingle(self::getApp(), 'hook_fork', $fork_hook, $hookdata);
153 if (!$hookdata['execute']) {
158 Worker::add($priority, 'ForkHook', $name, $hook, $data);
164 * @brief Calls a hook.
166 * Use this function when you want to be able to allow a hook to manipulate
169 * @param string $name of the hook to call
170 * @param string|array &$data to transmit to the callback handler
171 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
173 public static function callAll($name, &$data = null)
175 if (array_key_exists($name, self::$hooks)) {
176 foreach (self::$hooks[$name] as $hook) {
177 self::callSingle(self::getApp(), $name, $hook, $data);
183 * @brief Calls a single hook.
186 * @param string $name of the hook to call
187 * @param array $hook Hook data
188 * @param string|array &$data to transmit to the callback handler
189 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
191 public static function callSingle(App $a, $name, $hook, &$data = null)
193 // Don't run a theme's hook if the user isn't using the theme
194 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
198 @include_once($hook[0]);
199 if (function_exists($hook[1])) {
203 // remove orphan hooks
204 $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
205 DBA::delete('hook', $condition, ['cascade' => false]);
210 * Checks if an app_menu hook exist for the provided addon name.
211 * Return true if the addon is an app
213 * @param string $name Name of the addon
216 public static function isAddonApp($name)
218 if (array_key_exists('app_menu', self::$hooks)) {
219 foreach (self::$hooks['app_menu'] as $hook) {
220 if ($hook[0] == 'addon/' . $name . '/' . $name . '.php') {