includes/hooks/index.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Wed, 07 Mar 2012 23:21:18 +0400
changeset 9 e5e5221fa5e1
parent 0 ceef6d64c21d
permissions -rw-r--r--
HG ignore
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package phpBB3
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2007 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
/**
indvdum@0
    20
* phpBB Hook Class
indvdum@0
    21
* @package phpBB3
indvdum@0
    22
*/
indvdum@0
    23
class phpbb_hook
indvdum@0
    24
{
indvdum@0
    25
	/**
indvdum@0
    26
	* Registered hooks
indvdum@0
    27
	*/
indvdum@0
    28
	var $hooks = array();
indvdum@0
    29
indvdum@0
    30
	/**
indvdum@0
    31
	* Results returned by functions called
indvdum@0
    32
	*/
indvdum@0
    33
	var $hook_result = array();
indvdum@0
    34
indvdum@0
    35
	/**
indvdum@0
    36
	* internal pointer
indvdum@0
    37
	*/
indvdum@0
    38
	var $current_hook = NULL;
indvdum@0
    39
indvdum@0
    40
	/**
indvdum@0
    41
	* Initialize hook class.
indvdum@0
    42
	*
indvdum@0
    43
	* @param array $valid_hooks array containing the hookable functions/methods
indvdum@0
    44
	*/
indvdum@0
    45
	function phpbb_hook($valid_hooks)
indvdum@0
    46
	{
indvdum@0
    47
		foreach ($valid_hooks as $_null => $method)
indvdum@0
    48
		{
indvdum@0
    49
			$this->add_hook($method);
indvdum@0
    50
		}
indvdum@0
    51
indvdum@0
    52
		if (function_exists('phpbb_hook_register'))
indvdum@0
    53
		{
indvdum@0
    54
			phpbb_hook_register($this);
indvdum@0
    55
		}
indvdum@0
    56
	}
indvdum@0
    57
indvdum@0
    58
	/**
indvdum@0
    59
	* Register function/method to be called within hook
indvdum@0
    60
	* This function is normally called by the modification/application to attach/register the functions.
indvdum@0
    61
	*
indvdum@0
    62
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
indvdum@0
    63
	* @param mixed $hook The replacement function/method to be called. Passing function name or array with object/class definition
indvdum@0
    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.
indvdum@0
    65
	*/
indvdum@0
    66
	function register($definition, $hook, $mode = 'normal')
indvdum@0
    67
	{
indvdum@0
    68
		$class = (!is_array($definition)) ? '__global' : $definition[0];
indvdum@0
    69
		$function = (!is_array($definition)) ? $definition : $definition[1];
indvdum@0
    70
indvdum@0
    71
		// Method able to be hooked?
indvdum@0
    72
		if (isset($this->hooks[$class][$function]))
indvdum@0
    73
		{
indvdum@0
    74
			switch ($mode)
indvdum@0
    75
			{
indvdum@0
    76
				case 'standalone':
indvdum@0
    77
					if (!isset($this->hooks[$class][$function]['standalone']))
indvdum@0
    78
					{
indvdum@0
    79
						$this->hooks[$class][$function] = array('standalone' => $hook);
indvdum@0
    80
					}
indvdum@0
    81
					else
indvdum@0
    82
					{
indvdum@0
    83
						trigger_error('Hook not able to be called standalone, previous hook already standalone.', E_NOTICE);
indvdum@0
    84
					}
indvdum@0
    85
				break;
indvdum@0
    86
indvdum@0
    87
				case 'first':
indvdum@0
    88
				case 'last':
indvdum@0
    89
					$this->hooks[$class][$function][$mode][] = $hook;
indvdum@0
    90
				break;
indvdum@0
    91
indvdum@0
    92
				case 'normal':
indvdum@0
    93
				default:
indvdum@0
    94
					$this->hooks[$class][$function]['normal'][] = $hook;
indvdum@0
    95
				break;
indvdum@0
    96
			}
indvdum@0
    97
		}
indvdum@0
    98
	}
indvdum@0
    99
indvdum@0
   100
	/**
indvdum@0
   101
	* Calling all functions/methods attached to a specified hook.
indvdum@0
   102
	* Called by the function allowing hooks...
indvdum@0
   103
	*
indvdum@0
   104
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
indvdum@0
   105
	* @return bool False if no hook got executed, true otherwise
indvdum@0
   106
	*/
indvdum@0
   107
	function call_hook($definition)
indvdum@0
   108
	{
indvdum@0
   109
		$class = (!is_array($definition)) ? '__global' : $definition[0];
indvdum@0
   110
		$function = (!is_array($definition)) ? $definition : $definition[1];
indvdum@0
   111
indvdum@0
   112
		if (!empty($this->hooks[$class][$function]))
indvdum@0
   113
		{
indvdum@0
   114
			// Developer tries to call a hooked function within the hooked function...
indvdum@0
   115
			if ($this->current_hook !== NULL && $this->current_hook['class'] === $class && $this->current_hook['function'] === $function)
indvdum@0
   116
			{
indvdum@0
   117
				return false;
indvdum@0
   118
			}
indvdum@0
   119
indvdum@0
   120
			// Call the hook with the arguments attached and store result
indvdum@0
   121
			$arguments = func_get_args();
indvdum@0
   122
			$this->current_hook = array('class' => $class, 'function' => $function);
indvdum@0
   123
			$arguments[0] = &$this;
indvdum@0
   124
indvdum@0
   125
			// Call the hook chain...
indvdum@0
   126
			if (isset($this->hooks[$class][$function]['standalone']))
indvdum@0
   127
			{
indvdum@0
   128
				$this->hook_result[$class][$function] = call_user_func_array($this->hooks[$class][$function]['standalone'], $arguments);
indvdum@0
   129
			}
indvdum@0
   130
			else
indvdum@0
   131
			{
indvdum@0
   132
				foreach (array('first', 'normal', 'last') as $mode)
indvdum@0
   133
				{
indvdum@0
   134
					if (!isset($this->hooks[$class][$function][$mode]))
indvdum@0
   135
					{
indvdum@0
   136
						continue;
indvdum@0
   137
					}
indvdum@0
   138
indvdum@0
   139
					foreach ($this->hooks[$class][$function][$mode] as $hook)
indvdum@0
   140
					{
indvdum@0
   141
						$this->hook_result[$class][$function] = call_user_func_array($hook, $arguments);
indvdum@0
   142
					}
indvdum@0
   143
				}
indvdum@0
   144
			}
indvdum@0
   145
indvdum@0
   146
			$this->current_hook = NULL;
indvdum@0
   147
			return true;
indvdum@0
   148
		}
indvdum@0
   149
indvdum@0
   150
		$this->current_hook = NULL;
indvdum@0
   151
		return false;
indvdum@0
   152
	}
indvdum@0
   153
indvdum@0
   154
	/**
indvdum@0
   155
	* Get result from previously called functions/methods for the same hook
indvdum@0
   156
	*
indvdum@0
   157
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
indvdum@0
   158
	* @return mixed False if nothing returned if there is no result, else array('result' => ... )
indvdum@0
   159
	*/
indvdum@0
   160
	function previous_hook_result($definition)
indvdum@0
   161
	{
indvdum@0
   162
		$class = (!is_array($definition)) ? '__global' : $definition[0];
indvdum@0
   163
		$function = (!is_array($definition)) ? $definition : $definition[1];
indvdum@0
   164
indvdum@0
   165
		if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
indvdum@0
   166
		{
indvdum@0
   167
			return array('result' => $this->hook_result[$class][$function]);
indvdum@0
   168
		}
indvdum@0
   169
indvdum@0
   170
		return false;
indvdum@0
   171
	}
indvdum@0
   172
indvdum@0
   173
	/**
indvdum@0
   174
	* Check if the called functions/methods returned something.
indvdum@0
   175
	*
indvdum@0
   176
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
indvdum@0
   177
	* @return bool True if results are there, false if not
indvdum@0
   178
	*/
indvdum@0
   179
	function hook_return($definition)
indvdum@0
   180
	{
indvdum@0
   181
		$class = (!is_array($definition)) ? '__global' : $definition[0];
indvdum@0
   182
		$function = (!is_array($definition)) ? $definition : $definition[1];
indvdum@0
   183
indvdum@0
   184
		if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
indvdum@0
   185
		{
indvdum@0
   186
			return true;
indvdum@0
   187
		}
indvdum@0
   188
indvdum@0
   189
		return false;
indvdum@0
   190
	}
indvdum@0
   191
indvdum@0
   192
	/**
indvdum@0
   193
	* Give actual result from called functions/methods back.
indvdum@0
   194
	*
indvdum@0
   195
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
indvdum@0
   196
	* @return mixed The result
indvdum@0
   197
	*/
indvdum@0
   198
	function hook_return_result($definition)
indvdum@0
   199
	{
indvdum@0
   200
		$class = (!is_array($definition)) ? '__global' : $definition[0];
indvdum@0
   201
		$function = (!is_array($definition)) ? $definition : $definition[1];
indvdum@0
   202
indvdum@0
   203
		if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
indvdum@0
   204
		{
indvdum@0
   205
			$result = $this->hook_result[$class][$function];
indvdum@0
   206
			unset($this->hook_result[$class][$function]);
indvdum@0
   207
			return $result;
indvdum@0
   208
		}
indvdum@0
   209
indvdum@0
   210
		return;
indvdum@0
   211
	}
indvdum@0
   212
indvdum@0
   213
	/**
indvdum@0
   214
	* Add new function to the allowed hooks.
indvdum@0
   215
	*
indvdum@0
   216
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
indvdum@0
   217
	*/
indvdum@0
   218
	function add_hook($definition)
indvdum@0
   219
	{
indvdum@0
   220
		if (!is_array($definition))
indvdum@0
   221
		{
indvdum@0
   222
			$definition = array('__global', $definition);
indvdum@0
   223
		}
indvdum@0
   224
indvdum@0
   225
		$this->hooks[$definition[0]][$definition[1]] = array();
indvdum@0
   226
	}
indvdum@0
   227
indvdum@0
   228
	/**
indvdum@0
   229
	* Remove function from the allowed hooks.
indvdum@0
   230
	*
indvdum@0
   231
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
indvdum@0
   232
	*/
indvdum@0
   233
	function remove_hook($definition)
indvdum@0
   234
	{
indvdum@0
   235
		$class = (!is_array($definition)) ? '__global' : $definition[0];
indvdum@0
   236
		$function = (!is_array($definition)) ? $definition : $definition[1];
indvdum@0
   237
indvdum@0
   238
		if (isset($this->hooks[$class][$function]))
indvdum@0
   239
		{
indvdum@0
   240
			unset($this->hooks[$class][$function]);
indvdum@0
   241
indvdum@0
   242
			if (isset($this->hook_result[$class][$function]))
indvdum@0
   243
			{
indvdum@0
   244
				unset($this->hook_result[$class][$function]);
indvdum@0
   245
			}
indvdum@0
   246
		}
indvdum@0
   247
	}
indvdum@0
   248
}
indvdum@0
   249
indvdum@0
   250
?>