123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- namespace think;
- class Hook
- {
-
- private $tags = [];
-
- protected $bind = [];
-
- private static $portal = 'run';
-
- protected $app;
- public function __construct(App $app)
- {
- $this->app = $app;
- }
-
- public function portal($name)
- {
- self::$portal = $name;
- 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;
- }
-
- public function add($tag, $behavior, $first = false)
- {
- isset($this->tags[$tag]) || $this->tags[$tag] = [];
- if (is_array($behavior) && !is_callable($behavior)) {
- if (!array_key_exists('_overlay', $behavior)) {
- $this->tags[$tag] = array_merge($this->tags[$tag], $behavior);
- } else {
- unset($behavior['_overlay']);
- $this->tags[$tag] = $behavior;
- }
- } elseif ($first) {
- array_unshift($this->tags[$tag], $behavior);
- } else {
- $this->tags[$tag][] = $behavior;
- }
- }
-
- public function import(array $tags, $recursive = true)
- {
- if ($recursive) {
- foreach ($tags as $tag => $behavior) {
- $this->add($tag, $behavior);
- }
- } else {
- $this->tags = $tags + $this->tags;
- }
- }
-
- public function get($tag = '')
- {
- if (empty($tag)) {
-
- return $this->tags;
- }
- return array_key_exists($tag, $this->tags) ? $this->tags[$tag] : [];
- }
-
- public function listen($tag, $params = null, $once = false)
- {
- $results = [];
- $tags = $this->get($tag);
- foreach ($tags as $key => $name) {
- $results[$key] = $this->execTag($name, $tag, $params);
- if (false === $results[$key] || (!is_null($results[$key]) && $once)) {
- break;
- }
- }
- return $once ? end($results) : $results;
- }
-
- public function exec($class, $params = null)
- {
- if ($class instanceof \Closure || is_array($class)) {
- $method = $class;
- } else {
- if (isset($this->bind[$class])) {
- $class = $this->bind[$class];
- }
- $method = [$class, self::$portal];
- }
- return $this->app->invoke($method, [$params]);
- }
-
- 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;
- } else {
- $obj = Container::get($class);
- if (!is_callable([$obj, $method])) {
- $method = self::$portal;
- }
- $call = [$class, $method];
- $class = $class . '->' . $method;
- }
- $result = $this->app->invoke($call, [$params]);
- return $result;
- }
- public function __debugInfo()
- {
- $data = get_object_vars($this);
- unset($data['app']);
- return $data;
- }
- }
|