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 if (!array_key_exists($hook['hook'], self::$hooks)) {
42 self::$hooks[$hook['hook']] = [];
44 self::$hooks[$hook['hook']][] = [$hook['file'], $hook['function']];
52 * @param string $hook the name of the hook
53 * @param string $file the name of the file that hooks into
54 * @param string $function the name of the function that the hook will call
55 * @param int $priority A priority (defaults to 0)
58 public static function register($hook, $file, $function, $priority = 0)
60 $file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
62 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
63 if (DBA::exists('hook', $condition)) {
67 $result = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
75 * @param string $hook the name of the hook
76 * @param string $file the name of the file that hooks into
77 * @param string $function the name of the function that the hook called
80 public static function unregister($hook, $file, $function)
82 $relative_file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
84 // This here is only needed for fixing a problem that existed on the develop branch
85 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
86 DBA::delete('hook', $condition);
88 $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
89 $result = DBA::delete('hook', $condition);
94 * Returns the list of callbacks for a single hook
99 public static function getByName($name)
103 if (isset(self::$hooks[$name])) {
104 $return = self::$hooks[$name];
111 * @brief Forks a hook.
113 * Use this function when you want to fork a hook via the worker.
115 * @param string $name of the hook to call
116 * @param string|array $data to transmit to the callback handler
118 public static function fork($priority, $name, $data = null)
120 if (array_key_exists($name, self::$hooks)) {
121 foreach (self::$hooks[$name] as $hook) {
122 Worker::add($priority, 'ForkHook', $name, $hook, $data);
128 * @brief Calls a hook.
130 * Use this function when you want to be able to allow a hook to manipulate
133 * @param string $name of the hook to call
134 * @param string|array &$data to transmit to the callback handler
136 public static function callAll($name, &$data = null)
138 if (array_key_exists($name, self::$hooks)) {
139 foreach (self::$hooks[$name] as $hook) {
140 self::callSingle(self::getApp(), $name, $hook, $data);
146 * @brief Calls a single hook.
149 * @param string $name of the hook to call
150 * @param array $hook Hook data
151 * @param string|array &$data to transmit to the callback handler
153 public static function callSingle(App $a, $name, $hook, &$data = null)
155 // Don't run a theme's hook if the user isn't using the theme
156 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
160 @include_once($hook[0]);
161 if (function_exists($hook[1])) {
165 // remove orphan hooks
166 $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
167 DBA::delete('hook', $condition, ['cascade' => false]);
172 * check if an app_menu hook exist for addon $name.
173 * Return true if the addon is an app
175 public static function isAddonApp($name)
177 if (array_key_exists('app_menu', self::$hooks)) {
178 foreach (self::$hooks['app_menu'] as $hook) {
179 if ($hook[0] == 'addon/' . $name . '/' . $name . '.php') {