You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

149 lines
4.7 KiB

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. class Hook
  13. {
  14. /**
  15. * @var array 标签
  16. */
  17. private static $tags = [];
  18. /**
  19. * 动态添加行为扩展到某个标签
  20. * @access public
  21. * @param string $tag 标签名称
  22. * @param mixed $behavior 行为名称
  23. * @param bool $first 是否放到开头执行
  24. * @return void
  25. */
  26. public static function add($tag, $behavior, $first = false)
  27. {
  28. isset(self::$tags[$tag]) || self::$tags[$tag] = [];
  29. if (is_array($behavior) && !is_callable($behavior)) {
  30. if (!array_key_exists('_overlay', $behavior) || !$behavior['_overlay']) {
  31. unset($behavior['_overlay']);
  32. self::$tags[$tag] = array_merge(self::$tags[$tag], $behavior);
  33. } else {
  34. unset($behavior['_overlay']);
  35. self::$tags[$tag] = $behavior;
  36. }
  37. } elseif ($first) {
  38. array_unshift(self::$tags[$tag], $behavior);
  39. } else {
  40. self::$tags[$tag][] = $behavior;
  41. }
  42. }
  43. /**
  44. * 批量导入插件
  45. * @access public
  46. * @param array $tags 插件信息
  47. * @param boolean $recursive 是否递归合并
  48. * @return void
  49. */
  50. public static function import(array $tags, $recursive = true)
  51. {
  52. if ($recursive) {
  53. foreach ($tags as $tag => $behavior) {
  54. self::add($tag, $behavior);
  55. }
  56. } else {
  57. self::$tags = $tags + self::$tags;
  58. }
  59. }
  60. /**
  61. * 获取插件信息
  62. * @access public
  63. * @param string $tag 插件位置(留空获取全部)
  64. * @return array
  65. */
  66. public static function get($tag = '')
  67. {
  68. if (empty($tag)) {
  69. return self::$tags;
  70. }
  71. return array_key_exists($tag, self::$tags) ? self::$tags[$tag] : [];
  72. }
  73. /**
  74. * 监听标签的行为
  75. * @access public
  76. * @param string $tag 标签名称
  77. * @param mixed $params 传入参数
  78. * @param mixed $extra 额外参数
  79. * @param bool $once 只获取一个有效返回值
  80. * @return mixed
  81. */
  82. public static function listen($tag, &$params = null, $extra = null, $once = false)
  83. {
  84. $results = [];
  85. foreach (static::get($tag) as $key => $name) {
  86. $results[$key] = self::exec($name, $tag, $params, $extra);
  87. // 如果返回 false,或者仅获取一个有效返回则中断行为执行
  88. if (false === $results[$key] || (!is_null($results[$key]) && $once)) {
  89. break;
  90. }
  91. }
  92. return $once ? end($results) : $results;
  93. }
  94. /**
  95. * 执行某个行为
  96. * @access public
  97. * @param mixed $class 要执行的行为
  98. * @param string $tag 方法名(标签名)
  99. * @param mixed $params 传人的参数
  100. * @param mixed $extra 额外参数
  101. * @return mixed
  102. */
  103. public static function exec($class, $tag = '', &$params = null, $extra = null)
  104. {
  105. App::$debug && Debug::remark('behavior_start', 'time');
  106. $method = Loader::parseName($tag, 1, false);
  107. if ($class instanceof \Closure) {
  108. $result = call_user_func_array($class, [ & $params, $extra]);
  109. $class = 'Closure';
  110. } elseif (is_array($class)) {
  111. list($class, $method) = $class;
  112. $result = (new $class())->$method($params, $extra);
  113. $class = $class . '->' . $method;
  114. } elseif (is_object($class)) {
  115. $result = $class->$method($params, $extra);
  116. $class = get_class($class);
  117. } elseif (strpos($class, '::')) {
  118. $result = call_user_func_array($class, [ & $params, $extra]);
  119. } else {
  120. $obj = new $class();
  121. $method = ($tag && is_callable([$obj, $method])) ? $method : 'run';
  122. $result = $obj->$method($params, $extra);
  123. }
  124. if (App::$debug) {
  125. Debug::remark('behavior_end', 'time');
  126. Log::record('[ BEHAVIOR ] Run ' . $class . ' @' . $tag . ' [ RunTime:' . Debug::getRangeTime('behavior_start', 'behavior_end') . 's ]', 'info');
  127. }
  128. return $result;
  129. }
  130. }