includes/hooks/index.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/hooks/index.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,250 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package phpBB3
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2007 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* phpBB Hook Class
    1.24 +* @package phpBB3
    1.25 +*/
    1.26 +class phpbb_hook
    1.27 +{
    1.28 +	/**
    1.29 +	* Registered hooks
    1.30 +	*/
    1.31 +	var $hooks = array();
    1.32 +
    1.33 +	/**
    1.34 +	* Results returned by functions called
    1.35 +	*/
    1.36 +	var $hook_result = array();
    1.37 +
    1.38 +	/**
    1.39 +	* internal pointer
    1.40 +	*/
    1.41 +	var $current_hook = NULL;
    1.42 +
    1.43 +	/**
    1.44 +	* Initialize hook class.
    1.45 +	*
    1.46 +	* @param array $valid_hooks array containing the hookable functions/methods
    1.47 +	*/
    1.48 +	function phpbb_hook($valid_hooks)
    1.49 +	{
    1.50 +		foreach ($valid_hooks as $_null => $method)
    1.51 +		{
    1.52 +			$this->add_hook($method);
    1.53 +		}
    1.54 +
    1.55 +		if (function_exists('phpbb_hook_register'))
    1.56 +		{
    1.57 +			phpbb_hook_register($this);
    1.58 +		}
    1.59 +	}
    1.60 +
    1.61 +	/**
    1.62 +	* Register function/method to be called within hook
    1.63 +	* This function is normally called by the modification/application to attach/register the functions.
    1.64 +	*
    1.65 +	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
    1.66 +	* @param mixed $hook The replacement function/method to be called. Passing function name or array with object/class definition
    1.67 +	* @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.
    1.68 +	*/
    1.69 +	function register($definition, $hook, $mode = 'normal')
    1.70 +	{
    1.71 +		$class = (!is_array($definition)) ? '__global' : $definition[0];
    1.72 +		$function = (!is_array($definition)) ? $definition : $definition[1];
    1.73 +
    1.74 +		// Method able to be hooked?
    1.75 +		if (isset($this->hooks[$class][$function]))
    1.76 +		{
    1.77 +			switch ($mode)
    1.78 +			{
    1.79 +				case 'standalone':
    1.80 +					if (!isset($this->hooks[$class][$function]['standalone']))
    1.81 +					{
    1.82 +						$this->hooks[$class][$function] = array('standalone' => $hook);
    1.83 +					}
    1.84 +					else
    1.85 +					{
    1.86 +						trigger_error('Hook not able to be called standalone, previous hook already standalone.', E_NOTICE);
    1.87 +					}
    1.88 +				break;
    1.89 +
    1.90 +				case 'first':
    1.91 +				case 'last':
    1.92 +					$this->hooks[$class][$function][$mode][] = $hook;
    1.93 +				break;
    1.94 +
    1.95 +				case 'normal':
    1.96 +				default:
    1.97 +					$this->hooks[$class][$function]['normal'][] = $hook;
    1.98 +				break;
    1.99 +			}
   1.100 +		}
   1.101 +	}
   1.102 +
   1.103 +	/**
   1.104 +	* Calling all functions/methods attached to a specified hook.
   1.105 +	* Called by the function allowing hooks...
   1.106 +	*
   1.107 +	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
   1.108 +	* @return bool False if no hook got executed, true otherwise
   1.109 +	*/
   1.110 +	function call_hook($definition)
   1.111 +	{
   1.112 +		$class = (!is_array($definition)) ? '__global' : $definition[0];
   1.113 +		$function = (!is_array($definition)) ? $definition : $definition[1];
   1.114 +
   1.115 +		if (!empty($this->hooks[$class][$function]))
   1.116 +		{
   1.117 +			// Developer tries to call a hooked function within the hooked function...
   1.118 +			if ($this->current_hook !== NULL && $this->current_hook['class'] === $class && $this->current_hook['function'] === $function)
   1.119 +			{
   1.120 +				return false;
   1.121 +			}
   1.122 +
   1.123 +			// Call the hook with the arguments attached and store result
   1.124 +			$arguments = func_get_args();
   1.125 +			$this->current_hook = array('class' => $class, 'function' => $function);
   1.126 +			$arguments[0] = &$this;
   1.127 +
   1.128 +			// Call the hook chain...
   1.129 +			if (isset($this->hooks[$class][$function]['standalone']))
   1.130 +			{
   1.131 +				$this->hook_result[$class][$function] = call_user_func_array($this->hooks[$class][$function]['standalone'], $arguments);
   1.132 +			}
   1.133 +			else
   1.134 +			{
   1.135 +				foreach (array('first', 'normal', 'last') as $mode)
   1.136 +				{
   1.137 +					if (!isset($this->hooks[$class][$function][$mode]))
   1.138 +					{
   1.139 +						continue;
   1.140 +					}
   1.141 +
   1.142 +					foreach ($this->hooks[$class][$function][$mode] as $hook)
   1.143 +					{
   1.144 +						$this->hook_result[$class][$function] = call_user_func_array($hook, $arguments);
   1.145 +					}
   1.146 +				}
   1.147 +			}
   1.148 +
   1.149 +			$this->current_hook = NULL;
   1.150 +			return true;
   1.151 +		}
   1.152 +
   1.153 +		$this->current_hook = NULL;
   1.154 +		return false;
   1.155 +	}
   1.156 +
   1.157 +	/**
   1.158 +	* Get result from previously called functions/methods for the same hook
   1.159 +	*
   1.160 +	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
   1.161 +	* @return mixed False if nothing returned if there is no result, else array('result' => ... )
   1.162 +	*/
   1.163 +	function previous_hook_result($definition)
   1.164 +	{
   1.165 +		$class = (!is_array($definition)) ? '__global' : $definition[0];
   1.166 +		$function = (!is_array($definition)) ? $definition : $definition[1];
   1.167 +
   1.168 +		if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
   1.169 +		{
   1.170 +			return array('result' => $this->hook_result[$class][$function]);
   1.171 +		}
   1.172 +
   1.173 +		return false;
   1.174 +	}
   1.175 +
   1.176 +	/**
   1.177 +	* Check if the called functions/methods returned something.
   1.178 +	*
   1.179 +	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
   1.180 +	* @return bool True if results are there, false if not
   1.181 +	*/
   1.182 +	function hook_return($definition)
   1.183 +	{
   1.184 +		$class = (!is_array($definition)) ? '__global' : $definition[0];
   1.185 +		$function = (!is_array($definition)) ? $definition : $definition[1];
   1.186 +
   1.187 +		if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
   1.188 +		{
   1.189 +			return true;
   1.190 +		}
   1.191 +
   1.192 +		return false;
   1.193 +	}
   1.194 +
   1.195 +	/**
   1.196 +	* Give actual result from called functions/methods back.
   1.197 +	*
   1.198 +	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
   1.199 +	* @return mixed The result
   1.200 +	*/
   1.201 +	function hook_return_result($definition)
   1.202 +	{
   1.203 +		$class = (!is_array($definition)) ? '__global' : $definition[0];
   1.204 +		$function = (!is_array($definition)) ? $definition : $definition[1];
   1.205 +
   1.206 +		if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
   1.207 +		{
   1.208 +			$result = $this->hook_result[$class][$function];
   1.209 +			unset($this->hook_result[$class][$function]);
   1.210 +			return $result;
   1.211 +		}
   1.212 +
   1.213 +		return;
   1.214 +	}
   1.215 +
   1.216 +	/**
   1.217 +	* Add new function to the allowed hooks.
   1.218 +	*
   1.219 +	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
   1.220 +	*/
   1.221 +	function add_hook($definition)
   1.222 +	{
   1.223 +		if (!is_array($definition))
   1.224 +		{
   1.225 +			$definition = array('__global', $definition);
   1.226 +		}
   1.227 +
   1.228 +		$this->hooks[$definition[0]][$definition[1]] = array();
   1.229 +	}
   1.230 +
   1.231 +	/**
   1.232 +	* Remove function from the allowed hooks.
   1.233 +	*
   1.234 +	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
   1.235 +	*/
   1.236 +	function remove_hook($definition)
   1.237 +	{
   1.238 +		$class = (!is_array($definition)) ? '__global' : $definition[0];
   1.239 +		$function = (!is_array($definition)) ? $definition : $definition[1];
   1.240 +
   1.241 +		if (isset($this->hooks[$class][$function]))
   1.242 +		{
   1.243 +			unset($this->hooks[$class][$function]);
   1.244 +
   1.245 +			if (isset($this->hook_result[$class][$function]))
   1.246 +			{
   1.247 +				unset($this->hook_result[$class][$function]);
   1.248 +			}
   1.249 +		}
   1.250 +	}
   1.251 +}
   1.252 +
   1.253 +?>
   1.254 \ No newline at end of file