AVE.CMS v3.28
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
3.1 KiB

7 years ago
<?php
7 years ago
// Проверка
if (! defined('BASE_DIR'))
exit('Access denied');
7 years ago
7 years ago
/**
* This source file is part of the AVE.cms. More information,
* documentation and tutorials can be found at http://www.ave-cms.ru
*
* @package AVE.cms
* @file system/helpers/hooks.php
* @author @
* @copyright 2007-2016 (c) AVE.cms
* @link http://www.ave-cms.ru
* @version 4.0
* @since $date$
* @license license GPL v.2 http://www.ave-cms.ru/license.txt
*/
class Hooks
{
public static $instance;
7 years ago
7 years ago
public static $hooks;
7 years ago
7 years ago
public static $current_hook;
7 years ago
7 years ago
public static $run_hooks;
7 years ago
7 years ago
public static function init()
7 years ago
{
7 years ago
if (!self::$instance) {
self::$instance = new Hooks();
}
return self::$instance;
7 years ago
}
7 years ago
7 years ago
/**
7 years ago
* Add Hook
7 years ago
*/
7 years ago
public static function register($name, $function, $priority = 10)
7 years ago
{
7 years ago
// If we have already registered this action return true
if (isset(self::$hooks[$name][$priority][$function]))
{
return true;
}
/**
* Allows us to iterate through multiple action hooks.
*/
if (is_array($name))
{
foreach ($name AS $item)
{
// Store the action hook in the $hooks array
self::$hooks[$item][$priority][$function] = array(
"function" => $function
);
}
}
else
7 years ago
{
// Store the action hook in the $hooks array
7 years ago
self::$hooks[$name][$priority][$function] = array(
7 years ago
"function" => $function
);
}
7 years ago
return true;
}
7 years ago
7 years ago
/**
* Do Hook
*/
public static function trigger($name, $arguments = "")
7 years ago
{
7 years ago
// Oh, no you didn't. Are you trying to run an action hook that doesn't exist?
if (! isset(self::$hooks[$name]))
{
return $arguments;
}
7 years ago
7 years ago
// Set the current running hook to this
self::$current_hook = $name;
7 years ago
7 years ago
// Key sort our action hooks
ksort(self::$hooks[$name]);
foreach (self::$hooks[$name] AS $priority => $names)
7 years ago
{
7 years ago
if (is_array($names))
7 years ago
{
7 years ago
foreach ($names AS $name)
7 years ago
{
7 years ago
$return = call_user_func_array($name['function'], array(
&$arguments
));
if ($return)
{
$arguments = $return;
}
7 years ago
7 years ago
self::$run_hooks[$name][$priority];
}
7 years ago
}
}
7 years ago
self::$current_hook = '';
7 years ago
7 years ago
return $arguments;
7 years ago
}
7 years ago
/**
* Remove Hook
*/
public static function unregister($name, $function, $priority = 10)
{
// If the action hook doesn't, just return true
if (!isset(self::$hooks[$name][$priority][$function]))
{
return true;
}
// Remove the action hook from our hooks array
unset(self::$hooks[$name][$priority][$function]);
7 years ago
7 years ago
return '';
}
7 years ago
7 years ago
/**
* Current Hook
*
* Get the currently running action hook
*
*/
public static function current()
7 years ago
{
7 years ago
return self::$current_hook;
7 years ago
}
7 years ago
/**
* Has Run
*/
public static function has($hook, $priority = 10)
7 years ago
{
7 years ago
if (isset(self::$hooks[$hook][$priority]))
{
return true;
}
else
{
return false;
}
7 years ago
}
7 years ago
/**
* Hook Exists
*/
public static function exists($name)
7 years ago
{
7 years ago
if (isset(self::$hooks[$name]))
{
return true;
}
else
{
return false;
}
7 years ago
}
}
7 years ago
?>