Hook.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. * 钩子行为定义
  16. * @var array
  17. */
  18. private $tags = [];
  19. /**
  20. * 绑定行为列表
  21. * @var array
  22. */
  23. protected $bind = [];
  24. /**
  25. * 入口方法名称
  26. * @var string
  27. */
  28. private static $portal = 'run';
  29. /**
  30. * 应用对象
  31. * @var App
  32. */
  33. protected $app;
  34. public function __construct(App $app)
  35. {
  36. $this->app = $app;
  37. }
  38. /**
  39. * 指定入口方法名称
  40. * @access public
  41. * @param string $name 方法名
  42. * @return $this
  43. */
  44. public function portal($name)
  45. {
  46. self::$portal = $name;
  47. return $this;
  48. }
  49. /**
  50. * 指定行为标识 便于调用
  51. * @access public
  52. * @param string|array $name 行为标识
  53. * @param mixed $behavior 行为
  54. * @return $this
  55. */
  56. public function alias($name, $behavior = null)
  57. {
  58. if (is_array($name)) {
  59. $this->bind = array_merge($this->bind, $name);
  60. } else {
  61. $this->bind[$name] = $behavior;
  62. }
  63. return $this;
  64. }
  65. /**
  66. * 动态添加行为扩展到某个标签
  67. * @access public
  68. * @param string $tag 标签名称
  69. * @param mixed $behavior 行为名称
  70. * @param bool $first 是否放到开头执行
  71. * @return void
  72. */
  73. public function add($tag, $behavior, $first = false)
  74. {
  75. isset($this->tags[$tag]) || $this->tags[$tag] = [];
  76. if (is_array($behavior) && !is_callable($behavior)) {
  77. if (!array_key_exists('_overlay', $behavior)) {
  78. $this->tags[$tag] = array_merge($this->tags[$tag], $behavior);
  79. } else {
  80. unset($behavior['_overlay']);
  81. $this->tags[$tag] = $behavior;
  82. }
  83. } elseif ($first) {
  84. array_unshift($this->tags[$tag], $behavior);
  85. } else {
  86. $this->tags[$tag][] = $behavior;
  87. }
  88. }
  89. /**
  90. * 批量导入插件
  91. * @access public
  92. * @param array $tags 插件信息
  93. * @param bool $recursive 是否递归合并
  94. * @return void
  95. */
  96. public function import(array $tags, $recursive = true)
  97. {
  98. if ($recursive) {
  99. foreach ($tags as $tag => $behavior) {
  100. $this->add($tag, $behavior);
  101. }
  102. } else {
  103. $this->tags = $tags + $this->tags;
  104. }
  105. }
  106. /**
  107. * 获取插件信息
  108. * @access public
  109. * @param string $tag 插件位置 留空获取全部
  110. * @return array
  111. */
  112. public function get($tag = '')
  113. {
  114. if (empty($tag)) {
  115. //获取全部的插件信息
  116. return $this->tags;
  117. }
  118. return array_key_exists($tag, $this->tags) ? $this->tags[$tag] : [];
  119. }
  120. /**
  121. * 监听标签的行为
  122. * @access public
  123. * @param string $tag 标签名称
  124. * @param mixed $params 传入参数
  125. * @param bool $once 只获取一个有效返回值
  126. * @return mixed
  127. */
  128. public function listen($tag, $params = null, $once = false)
  129. {
  130. $results = [];
  131. $tags = $this->get($tag);
  132. foreach ($tags as $key => $name) {
  133. $results[$key] = $this->execTag($name, $tag, $params);
  134. if (false === $results[$key] || (!is_null($results[$key]) && $once)) {
  135. break;
  136. }
  137. }
  138. return $once ? end($results) : $results;
  139. }
  140. /**
  141. * 执行行为
  142. * @access public
  143. * @param mixed $class 行为
  144. * @param mixed $params 参数
  145. * @return mixed
  146. */
  147. public function exec($class, $params = null)
  148. {
  149. if ($class instanceof \Closure || is_array($class)) {
  150. $method = $class;
  151. } else {
  152. if (isset($this->bind[$class])) {
  153. $class = $this->bind[$class];
  154. }
  155. $method = [$class, self::$portal];
  156. }
  157. return $this->app->invoke($method, [$params]);
  158. }
  159. /**
  160. * 执行某个标签的行为
  161. * @access protected
  162. * @param mixed $class 要执行的行为
  163. * @param string $tag 方法名(标签名)
  164. * @param mixed $params 参数
  165. * @return mixed
  166. */
  167. protected function execTag($class, $tag = '', $params = null)
  168. {
  169. $method = Loader::parseName($tag, 1, false);
  170. if ($class instanceof \Closure) {
  171. $call = $class;
  172. $class = 'Closure';
  173. } elseif (is_array($class) || strpos($class, '::')) {
  174. $call = $class;
  175. } else {
  176. $obj = Container::get($class);
  177. if (!is_callable([$obj, $method])) {
  178. $method = self::$portal;
  179. }
  180. $call = [$class, $method];
  181. $class = $class . '->' . $method;
  182. }
  183. $result = $this->app->invoke($call, [$params]);
  184. return $result;
  185. }
  186. public function __debugInfo()
  187. {
  188. $data = get_object_vars($this);
  189. unset($data['app']);
  190. return $data;
  191. }
  192. }