<?php
/**
* Hook class file
*
* @category Include
* @package Slimium\Module\Core
* @author Jakub Ďuraš <jkblmr@gmail.com>
*/
/**
* Hook class- class for working with hooks
*
* -100 - lowest priority <br>
* 100+ - highest priority <br>
* 50 - default priority <br>
* if the priority is already taken, the nearest free lower one is taken
*/
class hook
{
/**
* List of hooks
* @var array
*/
private static $hooks = [];
/**
* Call hooks and don't pass any returned value
* @param string $hook_enviroment can be module name or other enviroment in which hook is located
* @param string $hook_name name
* @param mixed $argument argument to pass to hook
* @return boolean TRUE if there are any hooks assigned and they were executed, FALSE in there are no hooks assigned
*/
public static function call($hook_enviroment, $hook_name, $argument = NULL)
{
//If there is any hooked function
if (isset(self::$hooks[$hook_enviroment][$hook_name])) {
//Sort array
krsort(self::$hooks[$hook_enviroment][$hook_name]);
//Loop thorugh array
foreach (self::$hooks[$hook_enviroment][$hook_name] as $hook_function) {
$hook_function($argument);
}
return true;
}else{
return false;
}
}
/**
* Call hooks, let argument go through filtering and return filtered argument
* @param string $hook_enviroment can be module name or other enviroment in which hook is located
* @param string $hook_name name
* @param mixed $argument argument to pass to hook
* @return mixed filtered argument
*/
public static function filter($hook_enviroment, $hook_name, $argument = NULL)
{
//If there is any hooked function
if (isset(self::$hooks[$hook_enviroment][$hook_name])) {
//Sort array
krsort(self::$hooks[$hook_enviroment][$hook_name]);
$filtered = $argument;
$first = true;
//Loop thorugh array
foreach (self::$hooks[$hook_enviroment][$hook_name] as $hook_function) {
if($first){
$filtered = $hook_function($argument);
$first = false;
}else{
$filtered = $hook_function($argument, $filtered);
}
}
return $filtered;
}
}
/**
* Add hook
* @param string $hook_enviroment can be module name or other enviroment in which hook is located
* @param string $hook_name name
* @param callable $function name of the function of anonymous function to call
* @param callable $priority priority of the hook, can be anything from 1(executed as last) to 100+(executed as first) if priority is already taken, the nearest free lower one is taken
* @return integer priority at which hook was added
*/
public static function add($hook_enviroment, $hook_name, $function, $priority = 50)
{
//If we do not need to loop through prirorities
if(!isset(self::$hooks[$hook_enviroment][$hook_name][$priority])){
self::$hooks[$hook_enviroment][$hook_name][$priority] = $function;
return $priority;
}
//Loop through hooked functions to find nearest free priority
for ($priority; $priority > -100; $priority--) {
if(!isset(self::$hooks[$hook_enviroment][$hook_name][$priority])){
self::$hooks[$hook_enviroment][$hook_name][$priority] = $function;
break;
}
}
return $priority;
}
/**
* Remove hook at certain priority
* @param string $hook_enviroment can be module name or other enviroment in which hook is located
* @param string $hook_name name
* @param callable $priority priority of the hook to delete
* @return boolean TRUE
*/
public static function remove($hook_enviroment, $hook_name, $priority)
{
unset(self::$hooks[$hook_enviroment][$hook_name][$priority]);
return true;
}
/**
* Remove hook all hooks of some name and in som enviroment
* @param string $hook_enviroment can be module name or other enviroment in which hook is located
* @param string $hook_name name
* @return boolean TRUE
*/
public static function remove_all($hook_enviroment, $hook_name)
{
self::$hooks[$hook_enviroment][$hook_name] = [];
return true;
}
/**
* Is anything hooked at certain priority
* @param string $hook_enviroment can be module name or other enviroment in which hook is located
* @param string $hook_name name
* @param callable $priority priority of the hook
* @return boolean TRUE or FALSE
*/
public static function is_hooked($hook_enviroment, $hook_name, $priority)
{
if(isset(self::$hooks[$hook_enviroment][$hook_name][$priority])){
return true;
}else{
return false;
}
}
}
hook::add('main', 'init', function(){$var = 'foo';});
hook::add('main', 'init', function(){$var = 'foo';});
hook::add('main', 'init', function(){$var = 'foo';});
hook::add('main', 'init', function(){$var = 'foo';});
hook::add('main', 'init', function(){$var = 'foo';});
hook::add('main', 'init', function(){$var = 'foo';});
hook::add('main', 'init', function(){$var = 'foo';});
hook::add('main', 'init', function(){$var = 'foo';});
hook::add('main', 'init', function(){$var = 'foo';});
hook::add('main', 'init', function(){$var = 'foo';});
hook::call('main', 'init');
Parse error: syntax error, unexpected '[' in /in/8pvFH on line 24
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/8pvFH on line 24
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/8pvFH on line 24
Process exited with code 255.
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/8pvFH on line 24
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/8pvFH on line 24
Process exited with code 255.