1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Traits; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\Core\CallbackWrapper; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Event handlers trait |
8
|
|
|
* @package PHPDaemon\Traits |
9
|
|
|
* @author Vasily Zorin <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
trait EventHandlers |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array Event handlers |
15
|
|
|
*/ |
16
|
|
|
protected $eventHandlers = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var boolean Unshift $this to arguments of callback? |
20
|
|
|
*/ |
21
|
|
|
protected $addThisToEvents = true; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string Last called event name |
25
|
|
|
*/ |
26
|
|
|
protected $lastEventName; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Propagate event |
30
|
|
|
* @param string $name Event name |
31
|
|
|
* @param mixed ...$args Arguments |
32
|
|
|
* @return this |
|
|
|
|
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function event($name, ...$args) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
if ($this->addThisToEvents) { |
37
|
|
|
array_unshift($args, $this); |
38
|
|
|
} |
39
|
|
|
if (isset($this->eventHandlers[$name])) { |
40
|
|
|
$this->lastEventName = $name; |
41
|
|
|
foreach ($this->eventHandlers[$name] as $cb) { |
42
|
|
|
if ($cb(...$args) === true) { |
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Propagate event |
52
|
|
|
* @param string $name Event name |
53
|
|
|
* @param mixed ...$args Arguments |
54
|
|
|
* @return this |
|
|
|
|
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function trigger($name, ...$args) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
if ($this->addThisToEvents) { |
59
|
|
|
array_unshift($args, $this); |
60
|
|
|
} |
61
|
|
|
if (isset($this->eventHandlers[$name])) { |
62
|
|
|
$this->lastEventName = $name; |
63
|
|
|
foreach ($this->eventHandlers[$name] as $cb) { |
64
|
|
|
if ($cb(...$args) === true) { |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Propagate event |
74
|
|
|
* @param string $name Event name |
75
|
|
|
* @param mixed ...$args Arguments |
76
|
|
|
* @return integer |
77
|
|
|
*/ |
78
|
|
|
public function triggerAndCount($name, ...$args) |
79
|
|
|
{ |
80
|
|
|
if ($this->addThisToEvents) { |
81
|
|
|
array_unshift($args, $this); |
82
|
|
|
} |
83
|
|
|
$cnt = 0; |
84
|
|
|
if (isset($this->eventHandlers[$name])) { |
85
|
|
|
$this->lastEventName = $name; |
86
|
|
|
foreach ($this->eventHandlers[$name] as $cb) { |
87
|
|
|
if ($cb(...$args) !== 0) { |
88
|
|
|
++$cnt; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
return $cnt; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Use it to define event name, when one callback was bind to more than one events |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getLastEventName() |
100
|
|
|
{ |
101
|
|
|
return $this->lastEventName; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Bind event or events |
106
|
|
|
* @alias EventHandlers::bind |
107
|
|
|
* @param string|array $event Event name |
108
|
|
|
* @param callable $cb Callback |
109
|
|
|
* @return this |
|
|
|
|
110
|
|
|
*/ |
111
|
|
|
public function on($event, $cb) |
112
|
|
|
{ |
113
|
|
|
return $this->bind($event, $cb); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Bind event or events |
118
|
|
|
* @param string|array $event Event name |
119
|
|
|
* @param callable $cb Callback |
120
|
|
|
* @return this |
|
|
|
|
121
|
|
|
*/ |
122
|
|
|
public function bind($event, $cb) |
123
|
|
|
{ |
124
|
|
|
if ($cb !== null) { |
125
|
|
|
$cb = CallbackWrapper::wrap($cb); |
126
|
|
|
} |
127
|
|
|
$event = (array) $event; |
128
|
|
|
foreach ($event as $e) { |
129
|
|
|
CallbackWrapper::addToArray($this->eventHandlers[$e], $cb); |
130
|
|
|
} |
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Unbind event(s) or callback from event(s) |
136
|
|
|
* @alias EventHandlers::unbind |
137
|
|
|
* @param string|array $event Event name |
138
|
|
|
* @param callable $cb Callback, optional |
|
|
|
|
139
|
|
|
* @return this |
|
|
|
|
140
|
|
|
*/ |
141
|
|
|
public function off($event, $cb = null) |
142
|
|
|
{ |
143
|
|
|
return $this->unbind($event, $cb); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Unbind event(s) or callback from event(s) |
148
|
|
|
* @param string|array $event Event name |
149
|
|
|
* @param callable $cb Callback, optional |
|
|
|
|
150
|
|
|
* @return this |
|
|
|
|
151
|
|
|
*/ |
152
|
|
|
public function unbind($event, $cb = null) |
153
|
|
|
{ |
154
|
|
|
if ($cb !== null) { |
155
|
|
|
$cb = CallbackWrapper::wrap($cb); |
156
|
|
|
} |
157
|
|
|
$event = (array) $event; |
158
|
|
|
$success = true; |
|
|
|
|
159
|
|
|
foreach ($event as $e) { |
160
|
|
|
if (!isset($this->eventHandlers[$e])) { |
161
|
|
|
$success = false; |
|
|
|
|
162
|
|
|
continue; |
163
|
|
|
} |
164
|
|
|
if ($cb === null) { |
165
|
|
|
unset($this->eventHandlers[$e]); |
166
|
|
|
continue; |
167
|
|
|
} |
168
|
|
|
CallbackWrapper::removeFromArray($this->eventHandlers[$e], $cb); |
169
|
|
|
} |
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Clean up all events |
175
|
|
|
* @return void |
176
|
|
|
*/ |
177
|
|
|
protected function cleanupEventHandlers() |
178
|
|
|
{ |
179
|
|
|
$this->eventHandlers = []; |
180
|
|
|
//Daemon::log('clean up event handlers '.get_class($this). ' -- '.$this->attrs->server['REQUEST_URI']. PHP_EOL .Debug::backtrace()); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.