6 * @copyright (c) 2007 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
31 * Results returned by functions called
33 var $hook_result = array();
38 var $current_hook = NULL;
41 * Initialize hook class.
43 * @param array $valid_hooks array containing the hookable functions/methods
45 function phpbb_hook($valid_hooks)
47 foreach ($valid_hooks as $_null => $method)
49 $this->add_hook($method);
52 if (function_exists('phpbb_hook_register'))
54 phpbb_hook_register($this);
59 * Register function/method to be called within hook
60 * This function is normally called by the modification/application to attach/register the functions.
62 * @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
63 * @param mixed $hook The replacement function/method to be called. Passing function name or array with object/class definition
64 * @param string $mode Specify the priority/chain mode. 'normal' -> hook gets appended to the chain. 'standalone' -> only the specified hook gets called - later hooks are not able to overwrite this (E_NOTICE is triggered then). 'first' -> hook is called as the first one within the chain. 'last' -> hook is called as the last one within the chain.
66 function register($definition, $hook, $mode = 'normal')
68 $class = (!is_array($definition)) ? '__global' : $definition[0];
69 $function = (!is_array($definition)) ? $definition : $definition[1];
71 // Method able to be hooked?
72 if (isset($this->hooks[$class][$function]))
77 if (!isset($this->hooks[$class][$function]['standalone']))
79 $this->hooks[$class][$function] = array('standalone' => $hook);
83 trigger_error('Hook not able to be called standalone, previous hook already standalone.', E_NOTICE);
89 $this->hooks[$class][$function][$mode][] = $hook;
94 $this->hooks[$class][$function]['normal'][] = $hook;
101 * Calling all functions/methods attached to a specified hook.
102 * Called by the function allowing hooks...
104 * @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
105 * @return bool False if no hook got executed, true otherwise
107 function call_hook($definition)
109 $class = (!is_array($definition)) ? '__global' : $definition[0];
110 $function = (!is_array($definition)) ? $definition : $definition[1];
112 if (!empty($this->hooks[$class][$function]))
114 // Developer tries to call a hooked function within the hooked function...
115 if ($this->current_hook !== NULL && $this->current_hook['class'] === $class && $this->current_hook['function'] === $function)
120 // Call the hook with the arguments attached and store result
121 $arguments = func_get_args();
122 $this->current_hook = array('class' => $class, 'function' => $function);
123 $arguments[0] = &$this;
125 // Call the hook chain...
126 if (isset($this->hooks[$class][$function]['standalone']))
128 $this->hook_result[$class][$function] = call_user_func_array($this->hooks[$class][$function]['standalone'], $arguments);
132 foreach (array('first', 'normal', 'last') as $mode)
134 if (!isset($this->hooks[$class][$function][$mode]))
139 foreach ($this->hooks[$class][$function][$mode] as $hook)
141 $this->hook_result[$class][$function] = call_user_func_array($hook, $arguments);
146 $this->current_hook = NULL;
150 $this->current_hook = NULL;
155 * Get result from previously called functions/methods for the same hook
157 * @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
158 * @return mixed False if nothing returned if there is no result, else array('result' => ... )
160 function previous_hook_result($definition)
162 $class = (!is_array($definition)) ? '__global' : $definition[0];
163 $function = (!is_array($definition)) ? $definition : $definition[1];
165 if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
167 return array('result' => $this->hook_result[$class][$function]);
174 * Check if the called functions/methods returned something.
176 * @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
177 * @return bool True if results are there, false if not
179 function hook_return($definition)
181 $class = (!is_array($definition)) ? '__global' : $definition[0];
182 $function = (!is_array($definition)) ? $definition : $definition[1];
184 if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
193 * Give actual result from called functions/methods back.
195 * @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
196 * @return mixed The result
198 function hook_return_result($definition)
200 $class = (!is_array($definition)) ? '__global' : $definition[0];
201 $function = (!is_array($definition)) ? $definition : $definition[1];
203 if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
205 $result = $this->hook_result[$class][$function];
206 unset($this->hook_result[$class][$function]);
214 * Add new function to the allowed hooks.
216 * @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
218 function add_hook($definition)
220 if (!is_array($definition))
222 $definition = array('__global', $definition);
225 $this->hooks[$definition[0]][$definition[1]] = array();
229 * Remove function from the allowed hooks.
231 * @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
233 function remove_hook($definition)
235 $class = (!is_array($definition)) ? '__global' : $definition[0];
236 $function = (!is_array($definition)) ? $definition : $definition[1];
238 if (isset($this->hooks[$class][$function]))
240 unset($this->hooks[$class][$function]);
242 if (isset($this->hook_result[$class][$function]))
244 unset($this->hook_result[$class][$function]);