Blame Extras/phpBB/3.0.4/includes/hooks/index.php

4c79b5
4c79b5
/**
4c79b5
*
4c79b5
* @package phpBB3
4c79b5
* @version $Id: index.php 8479 2008-03-29 00:22:48Z naderman $
4c79b5
* @copyright (c) 2007 phpBB Group
4c79b5
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
4c79b5
*
4c79b5
*/
4c79b5
4c79b5
/**
4c79b5
* @ignore
4c79b5
*/
4c79b5
if (!defined('IN_PHPBB'))
4c79b5
{
4c79b5
	exit;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* phpBB Hook Class
4c79b5
* @package phpBB3
4c79b5
*/
4c79b5
class phpbb_hook
4c79b5
{
4c79b5
	/**
4c79b5
	* Registered hooks
4c79b5
	*/
4c79b5
	var $hooks = array();
4c79b5
4c79b5
	/**
4c79b5
	* Results returned by functions called
4c79b5
	*/
4c79b5
	var $hook_result = array();
4c79b5
4c79b5
	/**
4c79b5
	* internal pointer
4c79b5
	*/
4c79b5
	var $current_hook = NULL;
4c79b5
4c79b5
	/**
4c79b5
	* Initialize hook class.
4c79b5
	*
4c79b5
	* @param array $valid_hooks array containing the hookable functions/methods
4c79b5
	*/
4c79b5
	function phpbb_hook($valid_hooks)
4c79b5
	{
4c79b5
		foreach ($valid_hooks as $_null => $method)
4c79b5
		{
4c79b5
			$this->add_hook($method);
4c79b5
		}
4c79b5
4c79b5
		if (function_exists('phpbb_hook_register'))
4c79b5
		{
4c79b5
			phpbb_hook_register($this);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Register function/method to be called within hook
4c79b5
	* This function is normally called by the modification/application to attach/register the functions.
4c79b5
	*
4c79b5
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
4c79b5
	* @param mixed $hook The replacement function/method to be called. Passing function name or array with object/class definition
4c79b5
	* @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.
4c79b5
	*/
4c79b5
	function register($definition, $hook, $mode = 'normal')
4c79b5
	{
4c79b5
		$class = (!is_array($definition)) ? '__global' : $definition[0];
4c79b5
		$function = (!is_array($definition)) ? $definition : $definition[1];
4c79b5
4c79b5
		// Method able to be hooked?
4c79b5
		if (isset($this->hooks[$class][$function]))
4c79b5
		{
4c79b5
			switch ($mode)
4c79b5
			{
4c79b5
				case 'standalone':
4c79b5
					if (!isset($this->hooks[$class][$function]['standalone']))
4c79b5
					{
4c79b5
						$this->hooks[$class][$function] = array('standalone' => $hook);
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						trigger_error('Hook not able to be called standalone, previous hook already standalone.', E_NOTICE);
4c79b5
					}
4c79b5
				break;
4c79b5
4c79b5
				case 'first':
4c79b5
				case 'last':
4c79b5
					$this->hooks[$class][$function][$mode][] = $hook;
4c79b5
				break;
4c79b5
4c79b5
				case 'normal':
4c79b5
				default:
4c79b5
					$this->hooks[$class][$function]['normal'][] = $hook;
4c79b5
				break;
4c79b5
			}
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Calling all functions/methods attached to a specified hook.
4c79b5
	* Called by the function allowing hooks...
4c79b5
	*
4c79b5
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
4c79b5
	* @return bool False if no hook got executed, true otherwise
4c79b5
	*/
4c79b5
	function call_hook($definition)
4c79b5
	{
4c79b5
		$class = (!is_array($definition)) ? '__global' : $definition[0];
4c79b5
		$function = (!is_array($definition)) ? $definition : $definition[1];
4c79b5
4c79b5
		if (!empty($this->hooks[$class][$function]))
4c79b5
		{
4c79b5
			// Developer tries to call a hooked function within the hooked function...
4c79b5
			if ($this->current_hook !== NULL && $this->current_hook['class'] === $class && $this->current_hook['function'] === $function)
4c79b5
			{
4c79b5
				return false;
4c79b5
			}
4c79b5
4c79b5
			// Call the hook with the arguments attached and store result
4c79b5
			$arguments = func_get_args();
4c79b5
			$this->current_hook = array('class' => $class, 'function' => $function);
4c79b5
			$arguments[0] = &$this;
4c79b5
4c79b5
			// Call the hook chain...
4c79b5
			if (isset($this->hooks[$class][$function]['standalone']))
4c79b5
			{
4c79b5
				$this->hook_result[$class][$function] = call_user_func_array($this->hooks[$class][$function]['standalone'], $arguments);
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				foreach (array('first', 'normal', 'last') as $mode)
4c79b5
				{
4c79b5
					if (!isset($this->hooks[$class][$function][$mode]))
4c79b5
					{
4c79b5
						continue;
4c79b5
					}
4c79b5
4c79b5
					foreach ($this->hooks[$class][$function][$mode] as $hook)
4c79b5
					{
4c79b5
						$this->hook_result[$class][$function] = call_user_func_array($hook, $arguments);
4c79b5
					}
4c79b5
				}
4c79b5
			}
4c79b5
4c79b5
			$this->current_hook = NULL;
4c79b5
			return true;
4c79b5
		}
4c79b5
4c79b5
		$this->current_hook = NULL;
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Get result from previously called functions/methods for the same hook
4c79b5
	*
4c79b5
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
4c79b5
	* @return mixed False if nothing returned if there is no result, else array('result' => ... )
4c79b5
	*/
4c79b5
	function previous_hook_result($definition)
4c79b5
	{
4c79b5
		$class = (!is_array($definition)) ? '__global' : $definition[0];
4c79b5
		$function = (!is_array($definition)) ? $definition : $definition[1];
4c79b5
4c79b5
		if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
4c79b5
		{
4c79b5
			return array('result' => $this->hook_result[$class][$function]);
4c79b5
		}
4c79b5
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Check if the called functions/methods returned something.
4c79b5
	*
4c79b5
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
4c79b5
	* @return bool True if results are there, false if not
4c79b5
	*/
4c79b5
	function hook_return($definition)
4c79b5
	{
4c79b5
		$class = (!is_array($definition)) ? '__global' : $definition[0];
4c79b5
		$function = (!is_array($definition)) ? $definition : $definition[1];
4c79b5
4c79b5
		if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
4c79b5
		{
4c79b5
			return true;
4c79b5
		}
4c79b5
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Give actual result from called functions/methods back.
4c79b5
	*
4c79b5
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
4c79b5
	* @return mixed The result
4c79b5
	*/
4c79b5
	function hook_return_result($definition)
4c79b5
	{
4c79b5
		$class = (!is_array($definition)) ? '__global' : $definition[0];
4c79b5
		$function = (!is_array($definition)) ? $definition : $definition[1];
4c79b5
4c79b5
		if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
4c79b5
		{
4c79b5
			$result = $this->hook_result[$class][$function];
4c79b5
			unset($this->hook_result[$class][$function]);
4c79b5
			return $result;
4c79b5
		}
4c79b5
4c79b5
		return;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Add new function to the allowed hooks.
4c79b5
	*
4c79b5
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
4c79b5
	*/
4c79b5
	function add_hook($definition)
4c79b5
	{
4c79b5
		if (!is_array($definition))
4c79b5
		{
4c79b5
			$definition = array('__global', $definition);
4c79b5
		}
4c79b5
4c79b5
		$this->hooks[$definition[0]][$definition[1]] = array();
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Remove function from the allowed hooks.
4c79b5
	*
4c79b5
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
4c79b5
	*/
4c79b5
	function remove_hook($definition)
4c79b5
	{
4c79b5
		$class = (!is_array($definition)) ? '__global' : $definition[0];
4c79b5
		$function = (!is_array($definition)) ? $definition : $definition[1];
4c79b5
4c79b5
		if (isset($this->hooks[$class][$function]))
4c79b5
		{
4c79b5
			unset($this->hooks[$class][$function]);
4c79b5
4c79b5
			if (isset($this->hook_result[$class][$function]))
4c79b5
			{
4c79b5
				unset($this->hook_result[$class][$function]);
4c79b5
			}
4c79b5
		}
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
?>