indvdum@0: $method) indvdum@0: { indvdum@0: $this->add_hook($method); indvdum@0: } indvdum@0: indvdum@0: if (function_exists('phpbb_hook_register')) indvdum@0: { indvdum@0: phpbb_hook_register($this); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Register function/method to be called within hook indvdum@0: * This function is normally called by the modification/application to attach/register the functions. indvdum@0: * indvdum@0: * @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__) indvdum@0: * @param mixed $hook The replacement function/method to be called. Passing function name or array with object/class definition indvdum@0: * @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. indvdum@0: */ indvdum@0: function register($definition, $hook, $mode = 'normal') indvdum@0: { indvdum@0: $class = (!is_array($definition)) ? '__global' : $definition[0]; indvdum@0: $function = (!is_array($definition)) ? $definition : $definition[1]; indvdum@0: indvdum@0: // Method able to be hooked? indvdum@0: if (isset($this->hooks[$class][$function])) indvdum@0: { indvdum@0: switch ($mode) indvdum@0: { indvdum@0: case 'standalone': indvdum@0: if (!isset($this->hooks[$class][$function]['standalone'])) indvdum@0: { indvdum@0: $this->hooks[$class][$function] = array('standalone' => $hook); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: trigger_error('Hook not able to be called standalone, previous hook already standalone.', E_NOTICE); indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: case 'first': indvdum@0: case 'last': indvdum@0: $this->hooks[$class][$function][$mode][] = $hook; indvdum@0: break; indvdum@0: indvdum@0: case 'normal': indvdum@0: default: indvdum@0: $this->hooks[$class][$function]['normal'][] = $hook; indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Calling all functions/methods attached to a specified hook. indvdum@0: * Called by the function allowing hooks... indvdum@0: * indvdum@0: * @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__) indvdum@0: * @return bool False if no hook got executed, true otherwise indvdum@0: */ indvdum@0: function call_hook($definition) indvdum@0: { indvdum@0: $class = (!is_array($definition)) ? '__global' : $definition[0]; indvdum@0: $function = (!is_array($definition)) ? $definition : $definition[1]; indvdum@0: indvdum@0: if (!empty($this->hooks[$class][$function])) indvdum@0: { indvdum@0: // Developer tries to call a hooked function within the hooked function... indvdum@0: if ($this->current_hook !== NULL && $this->current_hook['class'] === $class && $this->current_hook['function'] === $function) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: // Call the hook with the arguments attached and store result indvdum@0: $arguments = func_get_args(); indvdum@0: $this->current_hook = array('class' => $class, 'function' => $function); indvdum@0: $arguments[0] = &$this; indvdum@0: indvdum@0: // Call the hook chain... indvdum@0: if (isset($this->hooks[$class][$function]['standalone'])) indvdum@0: { indvdum@0: $this->hook_result[$class][$function] = call_user_func_array($this->hooks[$class][$function]['standalone'], $arguments); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: foreach (array('first', 'normal', 'last') as $mode) indvdum@0: { indvdum@0: if (!isset($this->hooks[$class][$function][$mode])) indvdum@0: { indvdum@0: continue; indvdum@0: } indvdum@0: indvdum@0: foreach ($this->hooks[$class][$function][$mode] as $hook) indvdum@0: { indvdum@0: $this->hook_result[$class][$function] = call_user_func_array($hook, $arguments); indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $this->current_hook = NULL; indvdum@0: return true; indvdum@0: } indvdum@0: indvdum@0: $this->current_hook = NULL; indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Get result from previously called functions/methods for the same hook indvdum@0: * indvdum@0: * @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__) indvdum@0: * @return mixed False if nothing returned if there is no result, else array('result' => ... ) indvdum@0: */ indvdum@0: function previous_hook_result($definition) indvdum@0: { indvdum@0: $class = (!is_array($definition)) ? '__global' : $definition[0]; indvdum@0: $function = (!is_array($definition)) ? $definition : $definition[1]; indvdum@0: indvdum@0: if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function])) indvdum@0: { indvdum@0: return array('result' => $this->hook_result[$class][$function]); indvdum@0: } indvdum@0: indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Check if the called functions/methods returned something. indvdum@0: * indvdum@0: * @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__) indvdum@0: * @return bool True if results are there, false if not indvdum@0: */ indvdum@0: function hook_return($definition) indvdum@0: { indvdum@0: $class = (!is_array($definition)) ? '__global' : $definition[0]; indvdum@0: $function = (!is_array($definition)) ? $definition : $definition[1]; indvdum@0: indvdum@0: if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function])) indvdum@0: { indvdum@0: return true; indvdum@0: } indvdum@0: indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Give actual result from called functions/methods back. indvdum@0: * indvdum@0: * @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__) indvdum@0: * @return mixed The result indvdum@0: */ indvdum@0: function hook_return_result($definition) indvdum@0: { indvdum@0: $class = (!is_array($definition)) ? '__global' : $definition[0]; indvdum@0: $function = (!is_array($definition)) ? $definition : $definition[1]; indvdum@0: indvdum@0: if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function])) indvdum@0: { indvdum@0: $result = $this->hook_result[$class][$function]; indvdum@0: unset($this->hook_result[$class][$function]); indvdum@0: return $result; indvdum@0: } indvdum@0: indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Add new function to the allowed hooks. indvdum@0: * indvdum@0: * @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__) indvdum@0: */ indvdum@0: function add_hook($definition) indvdum@0: { indvdum@0: if (!is_array($definition)) indvdum@0: { indvdum@0: $definition = array('__global', $definition); indvdum@0: } indvdum@0: indvdum@0: $this->hooks[$definition[0]][$definition[1]] = array(); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Remove function from the allowed hooks. indvdum@0: * indvdum@0: * @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__) indvdum@0: */ indvdum@0: function remove_hook($definition) indvdum@0: { indvdum@0: $class = (!is_array($definition)) ? '__global' : $definition[0]; indvdum@0: $function = (!is_array($definition)) ? $definition : $definition[1]; indvdum@0: indvdum@0: if (isset($this->hooks[$class][$function])) indvdum@0: { indvdum@0: unset($this->hooks[$class][$function]); indvdum@0: indvdum@0: if (isset($this->hook_result[$class][$function])) indvdum@0: { indvdum@0: unset($this->hook_result[$class][$function]); indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: ?>