This repository has been archived by the owner on Apr 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventEmitter.php
129 lines (117 loc) · 3.69 KB
/
EventEmitter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace Wandu\Event;
use Psr\Container\ContainerInterface;
use Wandu\Event\Contracts\EventEmitter as EventEmitterContract;
use Wandu\Event\Contracts\Listener;
use Wandu\Event\Contracts\ViaQueue;
use Wandu\Event\Listener\CallableListener;
use Wandu\Event\Listener\WorkerListener;
use Wandu\Q\Worker;
class EventEmitter implements EventEmitterContract
{
/** @var array */
protected $listeners = [];
/** @var \Psr\Container\ContainerInterface */
protected $container;
/** @var \Wandu\Q\Worker */
protected $worker;
public function __construct(array $listeners = [])
{
$this->listeners = $listeners;
}
/**
* @param \Psr\Container\ContainerInterface $container
*/
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
}
/**
* @param \Wandu\Q\Worker $worker
*/
public function setWorker(Worker $worker)
{
$this->worker = $worker;
}
/**
* @param string $event
* @param string|\Closure $listener
* @return void
*/
public function on(string $event, $listener)
{
if (!array_key_exists($event, $this->listeners)) {
$this->listeners[$event] = [];
}
if (!in_array($listener, $this->listeners[$event])) {
$this->listeners[$event][] = $listener;
}
}
/**
* @param string $event
* @param string|\Closure|\Wandu\Event\Contracts\Listener $listener
* @return void
*/
public function off(string $event, $listener = null)
{
if (!array_key_exists($event, $this->listeners)) return;
if ($listener) {
$key = array_search($listener, $this->listeners[$event]);
if ($key !== false) {
array_splice($this->listeners[$event], $key, 1);
}
if (count($this->listeners[$event]) === 0) unset($this->listeners[$event]);
} else {
unset($this->listeners[$event]);
}
}
/**
* @param string|object $event
* @param array ...$arguments
* @return void
*/
public function trigger($event, ...$arguments)
{
$eventViaQueue = false;
if (is_object($event)) {
if ($event instanceof ViaQueue) {
$eventViaQueue = true;
}
$eventName = get_class($event);
$arguments = [$event];
} else {
$eventName = $event;
}
/** @var \Wandu\Event\Contracts\Listener $listener */
foreach ($this->getListeners($eventName) as $listener) {
if ($eventViaQueue && !$listener instanceof WorkerListener) {
$listener = new WorkerListener($this->worker, get_class($listener));
}
$listener->call($arguments);
}
}
/**
* @param $event
* @return \Generator|void
*/
protected function getListeners($event)
{
if (!isset($this->listeners[$event])) return;
foreach ($this->listeners[$event] as $listener) {
if ($listener instanceof Listener) {
yield $listener;
} elseif (is_callable($listener)) {
yield new CallableListener($listener);
} elseif ($this->container && $this->container->has($listener)) {
$listenerInstance = $this->container->get($listener);
if ($listenerInstance instanceof Listener) {
if ($listenerInstance instanceof ViaQueue) {
yield new WorkerListener($this->worker, $listener);
} else {
yield $listenerInstance;
}
}
}
}
}
}