diff options
author | redmatrix <git@macgirvin.com> | 2016-04-21 17:03:05 -0700 |
---|---|---|
committer | redmatrix <git@macgirvin.com> | 2016-04-21 17:03:05 -0700 |
commit | 1ff189ee907e6465dfb35118f789f10e714d38a1 (patch) | |
tree | 7c91b24d605a13ff9f36cacfd5c8d7c1c23ec9ed /Zotlabs/Extend/Hook.php | |
parent | 692e41c41ea54f6957c93b901a5ed4a437969691 (diff) | |
download | volse-hubzilla-1ff189ee907e6465dfb35118f789f10e714d38a1.tar.gz volse-hubzilla-1ff189ee907e6465dfb35118f789f10e714d38a1.tar.bz2 volse-hubzilla-1ff189ee907e6465dfb35118f789f10e714d38a1.zip |
new hook interface (the old one still works but requires handlers to have two calling arguments; the first of which is no longer used). The new interface is called from Zotlabs\Extend\Hook::register() and allows you to specify which hook version to use. The default will be the new interface with one function argument. We also implement the hook priority field which was always there but needed to be set manually in the DB. This provides a way for two hook handlers that implement the same hook interface to determine which order to be called in the event of conflicts.
Diffstat (limited to 'Zotlabs/Extend/Hook.php')
-rw-r--r-- | Zotlabs/Extend/Hook.php | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/Zotlabs/Extend/Hook.php b/Zotlabs/Extend/Hook.php new file mode 100644 index 000000000..836b29db8 --- /dev/null +++ b/Zotlabs/Extend/Hook.php @@ -0,0 +1,76 @@ +<?php + +namespace Zotlabs\Extend; + + +class Hook { + + static public function register($hook,$file,$function,$version = 1,$priority = 0) { + $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' and priority = %d and hook_version = %d LIMIT 1", + dbesc($hook), + dbesc($file), + dbesc($function), + intval($priority), + intval($version) + ); + if($r) + return true; + + $r = q("INSERT INTO `hook` (`hook`, `file`, `function`, `priority`, `hook_version`) VALUES ( '%s', '%s', '%s', %d, %d )", + dbesc($hook), + dbesc($file), + dbesc($function), + intval($priority), + intval($version) + ); + + return $r; + } + + static public function unregister($hook,$file,$function,$version = 1,$priority = 0) { + $r = q("DELETE FROM hook WHERE hook = '%s' AND `file` = '%s' AND `function` = '%s' and priority = %d and hook_version = %d", + dbesc($hook), + dbesc($file), + dbesc($function), + intval($priority), + intval($version) + ); + + return $r; + } + + + /** + * @brief Inserts a hook into a page request. + * + * Insert a short-lived hook into the running page request. + * Hooks are normally persistent so that they can be called + * across asynchronous processes such as delivery and poll + * processes. + * + * insert_hook lets you attach a hook callback immediately + * which will not persist beyond the life of this page request + * or the current process. + * + * @param string $hook + * name of hook to attach callback + * @param string $fn + * function name of callback handler + * @param int $version + * hook interface version, 0 uses two callback params, 1 uses one callback param + * @param int $priority + * currently not implemented in this function, would require the hook array to be resorted + */ + + static public function insert($hook, $fn, $version = 0, $priority = 0) { + + if(! is_array(App::$hooks)) + App::$hooks = array(); + + if(! array_key_exists($hook, App::$hooks)) + App::$hooks[$hook] = array(); + + App::$hooks[$hook][] = array('', $fn, $priority, $version); + } + +}
\ No newline at end of file |