tp5中hook(钩子)类详解
执行过程
troller\\Index', 'app\\index\controller\\Index' ], // 应用开始 'app_begin' => [], // 模块初始化 'module_init' => [], // 操作开始执行 'action_begin' => [], // 视图内容过滤 'view_filter' => [], // 日志写入 'log_write' => [], // 应用结束 'app_end' => [], ]; // 应用行为扩展定义文件
Hook注册
Hook::import();
Hook,在tp5中用来进行行为扩展。作为实现切面编程(AOP)的实现方法;
可以将Hook看做js的事件机制;
注册事件名称对应的处理函数。在代码运行过程中插入事件监听;
与js事件监听不同。js事件监听在dom元素,而tp事件监听在代码运行过程中;
等代码运行到插入的Hook监听处,即可自动运行注册的事件处理函数;
利用使用的静态变量 $tags 存储相关行为(可以理解为注册);
private static $tags = []; public static function add($tag, $behavior, $first = false) { isset(self::$tags[$tag]) || self::$tags[$tag] = []; if (is_array($behavior) && !is_callable($behavior)) { if (!array_key_exists('_overlay', $behavior) || !$behavior['_overlay']) { unset($behavior['_overlay']); self::$tags[$tag] = array_merge(self::$tags[$tag], $behavior); } else { unset($behavior['_overlay']); self::$tags[$tag] = $behavior; } } elseif ($first) { array_unshift(self::$tags[$tag], $behavior); } else { self::$tags[$tag][] = $behavior; } }
注册事件监听
Hook::listen();
在代码运行过程中注册事件监听;
注意:此方法会优先使用子类中get()方法
public static function listen($tag, &$params = null, $extra = null, $once = false) { $results = []; //此方法会优先使用子类中get()方法 foreach (static::get($tag) as $key => $name) { //如果配置了相关方法可以使用就可以执行 $results[$key] = self::exec($name, $tag, $params, $extra); // 如果返回 false,或者仅获取一个有效返回则中断行为执行 if (false === $results[$key] || (!is_null($results[$key]) && $once)) { break; } } return $once ? end($results) : $results; }
获取Hook注册信息
Hook::get(); 获取注册的Hook的tag注册的事件函数;
为空则获取全部;
public static function get($tag = '') { if (empty($tag)) { return self::$tags; } return array_key_exists($tag, self::$tags) ? self::$tags[$tag] : []; }
事件函数运行
Hook::exec();
代码运行到注册的事件监听处,自动运行tag对应的事件处理函数; 一般是在监听获取的使用有使用的时候执行此方法,也直接用跳过监听直接用此函数执行;
支持闭包、和直接执行行为;
public static function exec($class, $tag = '', &$params = null, $extra = null) { //记录执行时间 App::$debug && Debug::remark('behavior_start', 'time'); $method = Loader::parseName($tag, 1, false); //是否闭包 if ($class instanceof \Closure) { $result = call_user_func_array($class, [ & $params, $extra]); $class = 'Closure'; } elseif (is_array($class)) { list($class, $method) = $class; $result = (new $class())->$method($params, $extra); $class = $class . '->' . $method; } elseif (is_object($class)) { $result = $class->$method($params, $extra); $class = get_class($class); } elseif (strpos($class, '::')) { $result = call_user_func_array($class, [ & $params, $extra]); } else { $obj = new $class(); $method = ($tag && is_callable([$obj, $method])) ? $method : 'run'; $result = $obj->$method($params, $extra); } if (App::$debug) { Debug::remark('behavior_end', 'time'); Log::record('[ BEHAVIOR ] Run ' . $class . ' @' . $tag . ' [ RunTime:' . Debug::getRangeTime('behavior_start', 'behavior_end') . 's ]', 'info'); } return $result; }
tp5如何使用
两种使用方法:
第一种使用系统提供的监听
// 应用初始化 注册监听 tags.php 'app_init' => [ 'app\\index\controller\\Index', ] // app\index\controller\Index // 可以使用run方法或者提供的appInit方法 // 注意:两个方法同时存在的时候会优先使用appInit方法 public function appInit(&$params) { echo '初始化中调用钩子'; } public function run(&$params) { echo '初始化中调用钩子'; }
第二种自定义的监听
// 应用初始化 注册监听 tags.php 'action_init' => [ 'app\\index\controller\\Index', ] //也可以直接add方法直接注册 Hook::add('action_init','app\\index\controller\\Index'); //注意:该代码一定要在监听函数前注册 否则无效果 // 监听位置 app\index\controller\test // 注意:如果自定义监听系统一顶要 Hook::listen('action_init',$params); // app\index\controller\Index // 可以使用run方法或者提供的appInit方法 public function appInit(&$params) { echo 'test中调用钩子'; } public function run(&$params) { echo 'test中调用钩子'; }