|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Alexey Samoylov <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace YarCode\Events; |
|
7
|
|
|
|
|
8
|
|
|
trait EventEmitterTrait |
|
9
|
|
|
{ |
|
10
|
|
|
protected $listeners = []; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @param string $eventName |
|
14
|
|
|
* @param callable $listener |
|
15
|
|
|
*/ |
|
16
|
|
|
public function addListener($eventName, callable $listener) |
|
17
|
|
|
{ |
|
18
|
|
|
if (!isset($this->listeners[$eventName])) { |
|
19
|
|
|
$this->listeners[$eventName] = []; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
$this->listeners[$eventName][] = $listener; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $eventName |
|
27
|
|
|
* @param callable $listener |
|
28
|
|
|
*/ |
|
29
|
|
|
public function removeListener($eventName, callable $listener) |
|
30
|
|
|
{ |
|
31
|
|
|
if (isset($this->listeners[$eventName])) { |
|
32
|
|
|
if (false !== $index = array_search($listener, $this->listeners[$eventName], true)) { |
|
33
|
|
|
unset($this->listeners[$eventName][$index]); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string|null $eventName |
|
40
|
|
|
*/ |
|
41
|
|
|
public function removeAllListeners($eventName = null) |
|
42
|
|
|
{ |
|
43
|
|
|
if ($eventName !== null) { |
|
44
|
|
|
unset($this->listeners[$eventName]); |
|
45
|
|
|
} else { |
|
46
|
|
|
$this->listeners = []; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $eventName |
|
52
|
|
|
* @param Event $event |
|
53
|
|
|
*/ |
|
54
|
|
|
public function emit($eventName, Event $event = null) |
|
55
|
|
|
{ |
|
56
|
|
|
if (empty($this->listeners($eventName))) { |
|
57
|
|
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$event = $event ?: new Event(); |
|
61
|
|
|
$event->name = $eventName; |
|
62
|
|
|
$event->handled = false; |
|
63
|
|
|
|
|
64
|
|
|
$this->runListeners($event, $event); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $eventName |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
public function listeners($eventName) |
|
72
|
|
|
{ |
|
73
|
|
|
return isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : []; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $eventName |
|
78
|
|
|
* @param Event $event |
|
79
|
|
|
*/ |
|
80
|
|
|
private function runListeners($eventName, Event $event) |
|
81
|
|
|
{ |
|
82
|
|
|
foreach ($this->listeners($eventName) as $listener) { |
|
83
|
|
|
call_user_func($listener, $event); |
|
84
|
|
|
if ($event->handled) { |
|
85
|
|
|
break; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: