KodExplorer/app/kod/Hook.class.php

119 lines
2.9 KiB
PHP
Raw Normal View History

2017-08-23 19:40:27 +00:00
<?php
/*
* @link http://kodcloud.com/
* @author warlee | e-mail:kodcloud@qq.com
* @copyright warlee 2014.(Shanghai)Co.,Ltd
* @license http://kodcloud.com/tools/license/license.txt
*/
/**
* hook::add('function','function')
* hook::add('class:function','class.function')
*
* hook::run('class.function',param)
* hook::run('function',param)
*
*/
class Hook{
static private $events = array();
static public function get($event=false){
if(!$event){
return self::$events;
}else{
return self::$events[$event];
}
}
static public function apply($action,$args=array()) {
if(!is_string($action)){
return;
}
if(strstr($action,'.')){
$arr = explode('.',$action);
if(count($arr) !== 2){
return;
}
$className = $arr[0];
$functionName = $arr[1];
if(class_exists($className)){
$class = new $className();
if( method_exists($class,$functionName) ){
//return $class -> $functionName($args);
2018-07-05 13:50:52 +00:00
return @call_user_func_array(array($class,$functionName), $args);
2017-08-23 19:40:27 +00:00
}
}
}else{
if(function_exists($action)){
2018-07-05 13:50:52 +00:00
return @call_user_func_array($action, $args);
2017-08-23 19:40:27 +00:00
}
}
}
static public function bind($event,$action,$once=false) {
if(!isset(self::$events[$event])){
self::$events[$event] = array();
}
if(!is_array($action)){
$action = array($action);
}
for ($i=0; $i < count($action); $i++) {
self::$events[$event][] = array(
'action' => $action[$i],
'once' => $once,
'times' => 0
);
2018-08-22 14:55:14 +00:00
}
2017-08-23 19:40:27 +00:00
}
static public function once($event,$action) {
self::bind($event,$action,true);
}
static public function unbind($event) {
self::$events[$event] = false;
}
2017-08-26 09:16:57 +00:00
//数据处理;只支持传入一个参数
static public function filter($event,&$param) {
$result = self::trigger($event,$param);
if($result){
$param = $result;
}
}
2017-08-23 19:40:27 +00:00
static public function trigger($event) {
2018-08-22 14:55:14 +00:00
$events = self::$events;
if( !isset($events[$event]) ){
return;
}
$actions = $events[$event];
$result = false;
2017-08-23 19:40:27 +00:00
if(is_array($actions) && count($actions) >= 1) {
$args = func_get_args();
array_shift($args);
2018-08-22 14:55:14 +00:00
for ($i=0; $i < count($actions); $i++) {
$action = $actions[$i];
2017-08-23 19:40:27 +00:00
if( $action['once'] && $action['times'] > 1){
continue;
}
2018-08-22 14:55:14 +00:00
if(defined("GLOBAL_DEBUG_HOOK") && GLOBAL_DEBUG_HOOK){
write_log($event.'==>start: '.$action['action'],'hook-trigger');
}
self::$events[$event][$i]['times'] = $action['times'] + 1;
2017-08-23 19:40:27 +00:00
$res = self::apply($action['action'],$args);
2018-08-22 14:55:14 +00:00
if(defined("GLOBAL_DEBUG_HOOK") && GLOBAL_DEBUG_HOOK){
write_log($event.'==>end['.$action['times'].']: '.$action['action'],'hook-trigger');
}
//避免循环调用
if( $action['times'] >= 5000){
show_json("ERROR,Too many trigger on:".$event.'==>'.$action['action'],fasle);
}
2017-08-23 19:40:27 +00:00
if(!is_null($res)){
$result = $res;
}
}
}
return $result;
}
}