[李景山php]每天TP5-20170106|thinkphp5-Hook.php
来源:互联网 发布:c和java哪个好就业 编辑:程序博客网 时间:2024/05/01 17:52
<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: liu21st <liu21st@gmail.com>// +----------------------------------------------------------------------namespace think;use think\App;// 应用use think\Debug;// 调试use think\Log;// 日志// 上述 基本上就已经直接 包含 类了class Hook{ private static $tags = [];// 钩子仓库 /** * 动态添加行为扩展到某个标签 * @param string $tag 标签名称 * @param mixed $behavior 行为名称 * @param bool $first 是否放到开头执行 * @return void */ public static function add($tag, $behavior, $first = false)// 动态添加 行为扩展 到 某个标签 {// add($tag,$behavior,$first) isset(self::$tags[$tag]) || self::$tags[$tag] = [];// 初始化 仓库 对应的标签,经典的写法 //isset(self::$tags[$tag]) || self::$tags[$tag] = []; if (is_array($behavior) && !is_callable($behavior)) {// 如果是 行为 名称 是数组,并且 不能被调用 if (!array_key_exists('_overlay', $behavior) || !$behavior['_overlay']) { // 如果不存在 这个 _overlay 的key 或者 这个 key的值为空 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;// 否则 放到 后面 } } /** * 批量导入插件 * @param array $tags 插件信息 * @param boolean $recursive 是否递归合并 */ public static function import(array $tags, $recursive = true) {// 批量导入,基本上的批量导入 ,都是 tag 标签 都是 foreach 循环 if ($recursive) {// 递归 合并 foreach ($tags as $tag => $behavior) { self::add($tag, $behavior); } } else { self::$tags = $tags + self::$tags;// 普通合并 } } /** * 获取插件信息 * @param string $tag 插件位置 留空获取全部 * @return array */ public static function get($tag = '')// 经典的 获取方案,数据 { if (empty($tag)) {//获取全部的插件信息 return self::$tags; } else { return array_key_exists($tag, self::$tags) ? self::$tags[$tag] : []; } } /** * 监听标签的行为 * @param string $tag 标签名称 * @param mixed $params 传入参数 * @param mixed $extra 额外参数 * @param bool $once 只获取一个有效返回值 * @return mixed */ public static function listen($tag, &$params = null, $extra = null, $once = false) { $results = [];// 仓库 $tags = static::get($tag);// 太多类内部 外包了 foreach ($tags as $key => $name) {// 此处的 name 应该是行为的具体情况 是 class名称 $results[$key] = self::exec($name, $tag, $params, $extra); if (false === $results[$key]) {// 如果返回false 则中断行为执行 break; } elseif (!is_null($results[$key]) && $once) {// 一次 break; } } return $once ? end($results) : $results;// 返回最后的结果 } /** * 执行某个行为 * @param mixed $class 要执行的行为 * @param string $tag 方法名(标签名) * @param Mixed $params 传人的参数 * @param mixed $extra 额外参数 * @return mixed */ public static function exec($class, $tag = '', &$params = null, $extra = null) {// 执行 对应的类 App::$debug && Debug::remark('behavior_start', 'time');// 行为记录 if (is_callable($class)) { $result = call_user_func_array($class, [ & $params, $extra]); $class = 'Closure'; } elseif (is_object($class)) { $result = call_user_func_array([$class, $tag], [ & $params, $extra]); $class = get_class($class); } else { $obj = new $class(); $result = ($tag && is_callable([$obj, $tag])) ? $obj->$tag($params, $extra) : $obj->run($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; }}// 其实 hook 就是一个以标签 tag为 抓手的一类 函数的执行// 就是这个意思,根本就没有所谓监听的意图
0 0
- [李景山php]每天TP5-20170106|thinkphp5-Hook.php
- [李景山php]每天TP5-20161216|thinkphp5-helper.php-1
- [李景山php]每天TP5-20161217|thinkphp5-helper.php-2
- [李景山php]每天TP5-20161219|thinkphp5-softDelete.php
- [李景山php]每天TP5-20161220|thinkphp5-build.php
- [李景山php]每天TP5-20161221|thinkphp5-jump.php
- [李景山php]每天TP5-20161222|thinkphp5-Cache.php
- [李景山php]每天TP5-20161223|thinkphp5-Collection.php
- [李景山php]每天TP5-20161224|thinkphp5-Console.php-1
- [李景山php]每天TP5-20161225|thinkphp5-Console.php-2
- [李景山php]每天TP5-20161227|thinkphp5-Console.php-4
- [李景山php]每天TP5-20161228|thinkphp5-Console.php-5
- [李景山php]每天TP5-20161229|thinkphp5-Controller.php
- [李景山php]每天TP5-20161230|thinkphp5-Cookie.php
- [李景山php]每天TP5-20161231|thinkphp5-Db.php
- [李景山php]每天TP5-20170101|thinkphp5-Debug.php
- [李景山php]每天TP5-20170102|thinkphp5-Env.php
- [李景山php]每天TP5-20170103|thinkphp5-Exception.php
- 输入一个数插入有序数组中
- Mybatis Generator最完整配置详解
- nyoj 水池数目(DFS)
- Oracle中的NVL函数
- IDEA使用Maven-Springmvc
- [李景山php]每天TP5-20170106|thinkphp5-Hook.php
- numa init
- 搭建本地SVN服务器完整流程
- linux 查看访问本机ip次数
- numa init 2
- OC中关于字符串的操作
- 【Linux】Linux中常用操作命令
- 修改Win7开机界面,让Win7也炫起来。
- 【Java每日一题】20161201