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
96 * @param string $name Name of the 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 integer $priority of the hook
116 * @param string $name of the hook to call
117 * @param mixed $data to transmit to the callback handler
119 public static function fork($priority, $name, $data = null)
121 if (array_key_exists($name, self::$hooks)) {
122 foreach (self::$hooks[$name] as $hook) {
123 Worker::add($priority, 'ForkHook', $name, $hook, $data);
129 * @brief Calls a hook.
131 * Use this function when you want to be able to allow a hook to manipulate
134 * @param string $name of the hook to call
135 * @param string|array &$data to transmit to the callback handler
137 public static function callAll($name, &$data = null)
139 if (array_key_exists($name, self::$hooks)) {
140 foreach (self::$hooks[$name] as $hook) {
141 self::callSingle(self::getApp(), $name, $hook, $data);
147 * @brief Calls a single hook.
150 * @param string $name of the hook to call
151 * @param array $hook Hook data
152 * @param string|array &$data to transmit to the callback handler
154 public static function callSingle(App $a, $name, $hook, &$data = null)
156 // Don't run a theme's hook if the user isn't using the theme
157 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
161 @include_once($hook[0]);
162 if (function_exists($hook[1])) {
166 // remove orphan hooks
167 $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
168 DBA::delete('hook', $condition, ['cascade' => false]);
173 * Checks if an app_menu hook exist for the provided addon name.
174 * Return true if the addon is an app
176 * @param string $name Name of the addon
179 public static function isAddonApp($name)
181 if (array_key_exists('app_menu', self::$hooks)) {
182 foreach (self::$hooks['app_menu'] as $hook) {
183 if ($hook[0] == 'addon/' . $name . '/' . $name . '.php') {