hook类的add方法增加first参数 支持把某个行为放到钩子的开头首先执行

This commit is contained in:
thinkphp
2015-12-28 12:22:38 +08:00
parent de04d32551
commit 7d025ba2cb
2 changed files with 5 additions and 2 deletions

View File

@@ -41,7 +41,6 @@ abstract class Driver
protected $_linkID = null;
// 数据库连接参数配置
protected $config = [
'db_like_fields' => '', //like字段自动替换为%%包裹
'type' => '', // 数据库类型
'hostname' => '127.0.0.1', // 服务器地址
'database' => '', // 数据库名
@@ -57,6 +56,7 @@ abstract class Driver
'rw_separate' => false, // 数据库读写是否分离 主从式有效
'master_num' => 1, // 读写分离后 主服务器数量
'slave_no' => '', // 指定从服务器序号
'db_like_fields' => '', //like字段自动替换为%%包裹
];
// 数据库表达式
protected $exp = ['eq' => '=', 'neq' => '<>', 'gt' => '>', 'egt' => '>=', 'lt' => '<', 'elt' => '<=', 'notlike' => 'NOT LIKE', 'like' => 'LIKE', 'in' => 'IN', 'notin' => 'NOT IN', 'not in' => 'NOT IN', 'between' => 'BETWEEN', 'not between' => 'NOT BETWEEN', 'notbetween' => 'NOT BETWEEN'];

View File

@@ -20,15 +20,18 @@ class Hook
* 动态添加行为扩展到某个标签
* @param string $tag 标签名称
* @param mixed $behavior 行为名称
* @param bool $first 是否放到开头执行
* @return void
*/
public static function add($tag, $behavior)
public static function add($tag, $behavior,$first=false)
{
if (!isset(self::$tags[$tag])) {
self::$tags[$tag] = [];
}
if (is_array($behavior)) {
self::$tags[$tag] = array_merge(self::$tags[$tag], $behavior);
} elseif($first){
array_unshift(self::$tags[$tag],$behavior);
} else {
self::$tags[$tag][] = $behavior;
}