ave-cms/class/class.hooks.php

175 lines
3.1 KiB
PHP
Raw Normal View History

2017-06-24 00:58:56 +03:00
<?php
2018-04-19 19:11:58 +03:00
// Проверка
if (! defined('BASE_DIR'))
exit('Access denied');
2017-06-24 00:58:56 +03:00
2018-04-19 19:11:58 +03:00
/**
* 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;
2017-06-24 00:58:56 +03:00
2018-04-19 19:11:58 +03:00
public static $hooks;
2017-06-24 00:58:56 +03:00
2018-04-19 19:11:58 +03:00
public static $current_hook;
2017-06-24 00:58:56 +03:00
2018-04-19 19:11:58 +03:00
public static $run_hooks;
2017-06-24 00:58:56 +03:00
2020-08-25 15:55:10 +03:00
public static function init ()
2017-06-24 00:58:56 +03:00
{
2018-04-19 19:11:58 +03:00
if (!self::$instance) {
self::$instance = new Hooks();
}
return self::$instance;
2017-06-24 00:58:56 +03:00
}
2018-04-19 19:11:58 +03:00
2017-06-24 00:58:56 +03:00
/**
2018-04-19 19:11:58 +03:00
* Add Hook
2017-06-24 00:58:56 +03:00
*/
2020-08-25 15:55:10 +03:00
public static function register ($name, $function, $priority = 10)
2017-06-24 00:58:56 +03:00
{
2018-04-19 19:11:58 +03:00
// 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
2017-06-24 00:58:56 +03:00
{
// Store the action hook in the $hooks array
2018-04-19 19:11:58 +03:00
self::$hooks[$name][$priority][$function] = array(
2017-06-24 00:58:56 +03:00
"function" => $function
);
}
2018-04-19 19:11:58 +03:00
return true;
}
2017-06-24 00:58:56 +03:00
2018-04-19 19:11:58 +03:00
/**
* Do Hook
*/
2020-08-25 15:55:10 +03:00
public static function trigger ($name, $arguments = "")
2017-06-24 00:58:56 +03:00
{
2018-04-19 19:11:58 +03:00
// Oh, no you didn't. Are you trying to run an action hook that doesn't exist?
if (! isset(self::$hooks[$name]))
{
return $arguments;
}
2017-06-24 00:58:56 +03:00
2018-04-19 19:11:58 +03:00
// Set the current running hook to this
self::$current_hook = $name;
2017-06-24 00:58:56 +03:00
2018-04-19 19:11:58 +03:00
// Key sort our action hooks
ksort(self::$hooks[$name]);
foreach (self::$hooks[$name] AS $priority => $names)
2017-06-24 00:58:56 +03:00
{
2018-04-19 19:11:58 +03:00
if (is_array($names))
2017-06-24 00:58:56 +03:00
{
2018-04-19 19:11:58 +03:00
foreach ($names AS $name)
2017-06-24 00:58:56 +03:00
{
2018-04-19 19:11:58 +03:00
$return = call_user_func_array($name['function'], array(
&$arguments
));
if ($return)
{
$arguments = $return;
}
2017-06-24 00:58:56 +03:00
2018-04-19 19:11:58 +03:00
self::$run_hooks[$name][$priority];
}
2017-06-24 00:58:56 +03:00
}
}
2018-04-19 19:11:58 +03:00
self::$current_hook = '';
2017-06-24 00:58:56 +03:00
2018-04-19 19:11:58 +03:00
return $arguments;
2017-06-24 00:58:56 +03:00
}
2018-04-19 19:11:58 +03:00
/**
* Remove Hook
*/
2020-08-25 15:55:10 +03:00
public static function unregister ($name, $function, $priority = 10)
2018-04-19 19:11:58 +03:00
{
// 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]);
2017-06-24 00:58:56 +03:00
2018-04-19 19:11:58 +03:00
return '';
}
2017-06-24 00:58:56 +03:00
2018-04-19 19:11:58 +03:00
/**
* Current Hook
*
* Get the currently running action hook
*
*/
2020-08-25 15:55:10 +03:00
public static function current ()
2017-06-24 00:58:56 +03:00
{
2018-04-19 19:11:58 +03:00
return self::$current_hook;
2017-06-24 00:58:56 +03:00
}
2018-04-19 19:11:58 +03:00
/**
* Has Run
*/
2020-08-25 15:55:10 +03:00
public static function has ($hook, $priority = 10)
2017-06-24 00:58:56 +03:00
{
2018-04-19 19:11:58 +03:00
if (isset(self::$hooks[$hook][$priority]))
{
return true;
}
else
{
return false;
}
2017-06-24 00:58:56 +03:00
}
2018-04-19 19:11:58 +03:00
/**
* Hook Exists
*/
2020-08-25 15:55:10 +03:00
public static function exists ($name)
2017-06-24 00:58:56 +03:00
{
2018-04-19 19:11:58 +03:00
if (isset(self::$hooks[$name]))
{
return true;
}
else
{
return false;
}
2017-06-24 00:58:56 +03:00
}
}
2018-04-19 19:11:58 +03:00
?>