1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/wechat. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace EasyWeChat\Kernel\EventDispatcher; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Dispatcher. |
18
|
|
|
* |
19
|
|
|
* @author mingyoung <[email protected]> |
20
|
|
|
* |
21
|
|
|
* @method string|null dispatch(object $event) |
22
|
|
|
*/ |
23
|
|
|
class Dispatcher |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var \Symfony\Component\EventDispatcher\EventDispatcher |
27
|
|
|
*/ |
28
|
|
|
protected $dispatcher; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Dispatcher constructor. |
32
|
|
|
*/ |
33
|
292 |
|
public function __construct() |
34
|
|
|
{ |
35
|
292 |
|
$this->dispatcher = class_exists(EventDispatcher::class) ? new EventDispatcher() : null; |
36
|
292 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Adds an event listener that listens on the specified events. |
40
|
|
|
* |
41
|
|
|
* @param string $eventName The event to listen on |
42
|
|
|
* @param callable $listener The listener |
43
|
|
|
* @param int $priority The higher this value, the earlier an event |
44
|
|
|
* listener will be triggered in the chain (defaults to 0) |
45
|
|
|
*/ |
46
|
|
|
public function addListener($eventName, $listener, $priority = 0) |
47
|
|
|
{ |
48
|
|
|
// Makes callable. |
49
|
|
|
if (is_string($listener)) { |
50
|
|
|
$listener = new $listener(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($this->dispatcher) { |
54
|
|
|
$this->dispatcher->addListener($eventName, $listener, $priority); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $name |
60
|
|
|
* @param array $args |
61
|
|
|
* |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
292 |
|
public function __call($name, $args) |
65
|
|
|
{ |
66
|
292 |
|
if (!$this->dispatcher) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
292 |
|
return call_user_func_array([$this->dispatcher, $name], $args); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|