diff options
Diffstat (limited to 'app/Core/Plugin')
-rw-r--r-- | app/Core/Plugin/Hook.php | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/app/Core/Plugin/Hook.php b/app/Core/Plugin/Hook.php new file mode 100644 index 00000000..4fb55569 --- /dev/null +++ b/app/Core/Plugin/Hook.php @@ -0,0 +1,70 @@ +<?php + +namespace Core\Plugin; + +/** + * Plugin Hooks Handler + * + * @package plugin + * @author Frederic Guillot + */ +class Hook +{ + /** + * List of hooks + * + * @access private + * @var array + */ + private $hooks = array(); + + /** + * Bind something on a hook + * + * @access public + * @param string $hook + * @param mixed $value + */ + public function on($hook, $value) + { + if (! isset($this->hooks[$hook])) { + $this->hooks[$hook] = array(); + } + + $this->hooks[$hook][] = $value; + } + + /** + * Get all bindings for a hook + * + * @access public + * @param string $hook + * @return array + */ + public function getListeners($hook) + { + return isset($this->hooks[$hook]) ? $this->hooks[$hook] : array(); + } + + /** + * Merge listener results with input array + * + * @access public + * @param string $hook + * @param array $values + * @param array $params + * @return array + */ + public function merge($hook, array &$values, array $params = array()) + { + foreach ($this->getListeners($hook) as $listener) { + $result = call_user_func_array($listener, $params); + + if (is_array($result) && ! empty($result)) { + $values = array_merge($values, $result); + } + } + + return $values; + } +} |