Hook.php 4.6 KB

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