Completed
Push — 6.0 ( cb8a0b...bb9f45 )
by yun
06:00
created

Event::observe()   A

Complexity

Conditions 6
Paths 11

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 22
ccs 0
cts 12
cp 0
crap 42
rs 9.2222
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think;
14
15
/**
16
 * 事件管理类
17
 * @package think
0 ignored issues
show
Coding Style introduced by
Package name "think" is not valid; consider "Think" instead
Loading history...
18
 */
19
class Event
20
{
21
    /**
22
     * 监听者
23
     * @var array
24
     */
25
    protected $listener = [];
26
27
    /**
28
     * 观察者
29
     * @var array
30
     */
31
    protected $observer = [];
32
33
    /**
34
     * 事件别名
35
     * @var array
36
     */
37
    protected $bind = [
38
        'AppInit'  => event\AppInit::class,
39
        'HttpRun'  => event\HttpRun::class,
40
        'HttpEnd'  => event\HttpEnd::class,
41
        'LogLevel' => event\LogLevel::class,
42
        'LogWrite' => event\LogWrite::class,
43
    ];
44
45
    /**
46
     * 是否需要事件响应
47
     * @var bool
48
     */
49
    protected $withEvent = true;
50
51
    /**
52
     * 应用对象
53
     * @var App
54
     */
55
    protected $app;
56
57 9
    public function __construct(App $app)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
58
    {
59 9
        $this->app = $app;
60 9
    }
61
62
    /**
63
     * 设置是否开启事件响应
64
     * @access protected
65
     * @param  bool $event 是否需要事件响应
66
     * @return $this
67
     */
68 6
    public function withEvent(bool $event)
69
    {
70 6
        $this->withEvent = $event;
71 6
        return $this;
72
    }
73
74
    /**
75
     * 批量注册事件监听
76
     * @access public
77
     * @param  array $events 事件定义
78
     * @return $this
79
     */
80 1
    public function listenEvents(array $events)
81
    {
82 1
        if (!$this->withEvent) {
83
            return $this;
84
        }
85
86 1
        foreach ($events as $event => $listeners) {
87
            if (isset($this->bind[$event])) {
88
                $event = $this->bind[$event];
89
            }
90
91
            $this->listener[$event] = array_merge($this->listener[$event] ?? [], $listeners);
92
        }
93
94 1
        return $this;
95
    }
96
97
    /**
98
     * 注册事件监听
99
     * @access public
100
     * @param  string $event    事件名称
101
     * @param  mixed  $listener 监听操作(或者类名)
102
     * @param  bool   $first    是否优先执行
103
     * @return $this
104
     */
105
    public function listen(string $event, $listener, bool $first = false)
106
    {
107
        if (!$this->withEvent) {
108
            return $this;
109
        }
110
111
        if (isset($this->bind[$event])) {
112
            $event = $this->bind[$event];
113
        }
114
115
        if ($first && isset($this->listener[$event])) {
116
            array_unshift($this->listener[$event], $listener);
117
        } else {
118
            $this->listener[$event][] = $listener;
119
        }
120
121
        return $this;
122
    }
123
124
    /**
125
     * 是否存在事件监听
126
     * @access public
127
     * @param  string $event 事件名称
128
     * @return bool
129
     */
130
    public function hasListen(string $event): bool
131
    {
132
        if (isset($this->bind[$event])) {
133
            $event = $this->bind[$event];
134
        }
135
136
        return isset($this->listener[$event]);
137
    }
138
139
    /**
140
     * 移除事件监听
141
     * @access public
142
     * @param  string $event 事件名称
143
     * @return $this
144
     */
145
    public function remove(string $event): void
146
    {
147
        if (isset($this->bind[$event])) {
148
            $event = $this->bind[$event];
149
        }
150
151
        unset($this->listener[$event]);
152
    }
153
154
    /**
155
     * 指定事件别名标识 便于调用
156
     * @access public
157
     * @param  array $events 事件别名
158
     * @return $this
159
     */
160 1
    public function bind(array $events)
161
    {
162 1
        $this->bind = array_merge($this->bind, $events);
163
164 1
        return $this;
165
    }
166
167
    /**
168
     * 注册事件订阅者
169
     * @access public
170
     * @param  mixed $subscriber 订阅者
171
     * @return $this
172
     */
173 1
    public function subscribe($subscriber)
174
    {
175 1
        if (!$this->withEvent) {
176
            return $this;
177
        }
178
179 1
        $subscribers = (array) $subscriber;
180
181 1
        foreach ($subscribers as $subscriber) {
0 ignored issues
show
introduced by
$subscriber is overwriting one of the parameters of this function.
Loading history...
182
            if (is_string($subscriber)) {
183
                $subscriber = $this->app->make($subscriber);
184
            }
185
186
            if (method_exists($subscriber, 'subscribe')) {
187
                // 手动订阅
188
                $subscriber->subscribe($this);
189
            } else {
190
                // 智能订阅
191
                $this->observe($subscriber);
192
            }
193
        }
194
195 1
        return $this;
196
    }
197
198
    /**
199
     * 自动注册事件观察者
200
     * @access public
201
     * @param  string|object $observer 观察者
202
     * @return $this
203
     */
204
    public function observe($observer)
205
    {
206
        if (!$this->withEvent) {
207
            return $this;
208
        }
209
210
        if (is_string($observer)) {
211
            $observer = $this->app->make($observer);
212
        }
213
214
        $events = array_keys($this->listener);
215
216
        foreach ($events as $event) {
217
            $name   = false !== strpos($event, '\\') ? substr(strrchr($event, '\\'), 1) : $event;
218
            $method = 'on' . $name;
219
220
            if (method_exists($observer, $method)) {
221
                $this->listen($event, [$observer, $method]);
222
            }
223
        }
224
225
        return $this;
226
    }
227
228
    /**
229
     * 触发事件
230
     * @access public
231
     * @param  string|object $event  事件名称
232
     * @param  mixed         $params 传入参数
233
     * @param  bool          $once   只获取一个有效返回值
234
     * @return mixed
235
     */
236 8
    public function trigger($event, $params = null, bool $once = false)
237
    {
238 8
        if (!$this->withEvent) {
239
            return;
240
        }
241
242 8
        if (is_object($event)) {
243 2
            $params = $event;
244 2
            $event  = get_class($event);
245
        }
246
247 8
        if (isset($this->bind[$event])) {
248 6
            $event = $this->bind[$event];
249
        }
250
251 8
        $result    = [];
252 8
        $listeners = $this->listener[$event] ?? [];
253
254 8
        foreach ($listeners as $key => $listener) {
255
            $result[$key] = $this->dispatch($listener, $params);
256
257
            if (false === $result[$key] || (!is_null($result[$key]) && $once)) {
258
                break;
259
            }
260
        }
261
262 8
        return $once ? end($result) : $result;
263
    }
264
265
    /**
266
     * 执行事件调度
267
     * @access protected
268
     * @param  mixed $event  事件方法
269
     * @param  mixed $params 参数
270
     * @return mixed
271
     */
272
    protected function dispatch($event, $params = null)
273
    {
274
        if (!is_string($event)) {
275
            $call = $event;
276
        } elseif (strpos($event, '::')) {
277
            $call = $event;
278
        } else {
279
            $obj  = $this->app->make($event);
280
            $call = [$obj, 'handle'];
281
        }
282
283
        return $this->app->invoke($call, [$params]);
284
    }
285
286
}
287