feat: 同步下新环境

This commit is contained in:
笔记本里的永平
2025-07-07 11:31:25 +08:00
parent 3d1050db3d
commit 86c261ba70
196 changed files with 13146 additions and 29319 deletions

View File

@@ -14,122 +14,68 @@ namespace think;
class Hook
{
/**
* 钩子行为定义
* @var array
* @var array 标签
*/
private $tags = [];
/**
* 绑定行为列表
* @var array
*/
protected $bind = [];
/**
* 入口方法名称
* @var string
*/
private static $portal = 'run';
/**
* 应用对象
* @var App
*/
protected $app;
public function __construct(App $app)
{
$this->app = $app;
}
/**
* 指定入口方法名称
* @access public
* @param string $name 方法名
* @return $this
*/
public function portal($name)
{
self::$portal = $name;
return $this;
}
/**
* 指定行为标识 便于调用
* @access public
* @param string|array $name 行为标识
* @param mixed $behavior 行为
* @return $this
*/
public function alias($name, $behavior = null)
{
if (is_array($name)) {
$this->bind = array_merge($this->bind, $name);
} else {
$this->bind[$name] = $behavior;
}
return $this;
}
private static $tags = [];
/**
* 动态添加行为扩展到某个标签
* @access public
* @param string $tag 标签名称
* @param mixed $behavior 行为名称
* @param bool $first 是否放到开头执行
* @param string $tag 标签名称
* @param mixed $behavior 行为名称
* @param bool $first 是否放到开头执行
* @return void
*/
public function add($tag, $behavior, $first = false)
public static function add($tag, $behavior, $first = false)
{
isset($this->tags[$tag]) || $this->tags[$tag] = [];
isset(self::$tags[$tag]) || self::$tags[$tag] = [];
if (is_array($behavior) && !is_callable($behavior)) {
if (!array_key_exists('_overlay', $behavior)) {
$this->tags[$tag] = array_merge($this->tags[$tag], $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']);
$this->tags[$tag] = $behavior;
self::$tags[$tag] = $behavior;
}
} elseif ($first) {
array_unshift($this->tags[$tag], $behavior);
array_unshift(self::$tags[$tag], $behavior);
} else {
$this->tags[$tag][] = $behavior;
self::$tags[$tag][] = $behavior;
}
}
/**
* 批量导入插件
* @access public
* @param array $tags 插件信息
* @param bool $recursive 是否递归合并
* @param array $tags 插件信息
* @param boolean $recursive 是否递归合并
* @return void
*/
public function import(array $tags, $recursive = true)
public static function import(array $tags, $recursive = true)
{
if ($recursive) {
foreach ($tags as $tag => $behavior) {
$this->add($tag, $behavior);
self::add($tag, $behavior);
}
} else {
$this->tags = $tags + $this->tags;
self::$tags = $tags + self::$tags;
}
}
/**
* 获取插件信息
* @access public
* @param string $tag 插件位置 留空获取全部
* @param string $tag 插件位置(留空获取全部)
* @return array
*/
public function get($tag = '')
public static function get($tag = '')
{
if (empty($tag)) {
//获取全部的插件信息
return $this->tags;
return self::$tags;
}
return array_key_exists($tag, $this->tags) ? $this->tags[$tag] : [];
return array_key_exists($tag, self::$tags) ? self::$tags[$tag] : [];
}
/**
@@ -137,17 +83,18 @@ class Hook
* @access public
* @param string $tag 标签名称
* @param mixed $params 传入参数
* @param mixed $extra 额外参数
* @param bool $once 只获取一个有效返回值
* @return mixed
*/
public function listen($tag, $params = null, $once = false)
public static function listen($tag, &$params = null, $extra = null, $once = false)
{
$results = [];
$tags = $this->get($tag);
foreach ($tags as $key => $name) {
$results[$key] = $this->execTag($name, $tag, $params);
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;
}
@@ -157,64 +104,45 @@ class Hook
}
/**
* 执行行为
* 执行某个行为
* @access public
* @param mixed $class 行为
* @param mixed $params 参数
* @param mixed $class 要执行的行为
* @param string $tag 方法名(标签名)
* @param mixed $params 传人的参数
* @param mixed $extra 额外参数
* @return mixed
*/
public function exec($class, $params = null)
public static function exec($class, $tag = '', &$params = null, $extra = null)
{
if ($class instanceof \Closure || is_array($class)) {
$method = $class;
} else {
if (isset($this->bind[$class])) {
$class = $this->bind[$class];
}
$method = [$class, self::$portal];
}
App::$debug && Debug::remark('behavior_start', 'time');
return $this->app->invoke($method, [$params]);
}
/**
* 执行某个标签的行为
* @access protected
* @param mixed $class 要执行的行为
* @param string $tag 方法名(标签名)
* @param mixed $params 参数
* @return mixed
*/
protected function execTag($class, $tag = '', $params = null)
{
$method = Loader::parseName($tag, 1, false);
if ($class instanceof \Closure) {
$call = $class;
$class = 'Closure';
} elseif (is_array($class) || strpos($class, '::')) {
$call = $class;
$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 = Container::get($class);
if (!is_callable([$obj, $method])) {
$method = self::$portal;
}
$call = [$class, $method];
$class = $class . '->' . $method;
$obj = new $class();
$method = ($tag && is_callable([$obj, $method])) ? $method : 'run';
$result = $obj->$method($params, $extra);
}
$result = $this->app->invoke($call, [$params]);
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;
}
public function __debugInfo()
{
$data = get_object_vars($this);
unset($data['app']);
return $data;
}
}