1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Traits; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\Core\CallbackWrapper; |
5
|
|
|
use PHPDaemon\Core\Daemon; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Event handlers trait |
9
|
|
|
* @package PHPDaemon\Traits |
10
|
|
|
* @author Zorin Vasily <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
trait EventHandlers { |
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() { |
|
|
|
|
35
|
|
|
$args = func_get_args(); |
36
|
|
|
$name = array_shift($args); |
37
|
|
|
if ($this->addThisToEvents) { |
38
|
|
|
array_unshift($args, $this); |
39
|
|
|
} |
40
|
|
|
if (isset($this->eventHandlers[$name])) { |
41
|
|
|
$this->lastEventName = $name; |
42
|
|
|
foreach ($this->eventHandlers[$name] as $cb) { |
43
|
|
|
if (call_user_func_array($cb, $args) === true) { |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Propagate event |
53
|
|
|
* @param string $name Event name |
|
|
|
|
54
|
|
|
* @param mixed ...$args Arguments |
55
|
|
|
* @return this |
|
|
|
|
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function trigger() { |
|
|
|
|
58
|
|
|
$args = func_get_args(); |
59
|
|
|
$name = array_shift($args); |
60
|
|
|
if ($this->addThisToEvents) { |
61
|
|
|
array_unshift($args, $this); |
62
|
|
|
} |
63
|
|
|
if (isset($this->eventHandlers[$name])) { |
64
|
|
|
$this->lastEventName = $name; |
65
|
|
|
foreach ($this->eventHandlers[$name] as $cb) { |
66
|
|
|
if (call_user_func_array($cb, $args) === true) { |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Propagate event |
76
|
|
|
* @param string $name Event name |
|
|
|
|
77
|
|
|
* @param mixed ...$args Arguments |
78
|
|
|
* @return integer |
79
|
|
|
*/ |
80
|
|
|
public function triggerAndCount() { |
81
|
|
|
$args = func_get_args(); |
82
|
|
|
$name = array_shift($args); |
83
|
|
|
if ($this->addThisToEvents) { |
84
|
|
|
array_unshift($args, $this); |
85
|
|
|
} |
86
|
|
|
$cnt = 0; |
87
|
|
|
if (isset($this->eventHandlers[$name])) { |
88
|
|
|
$this->lastEventName = $name; |
89
|
|
|
foreach ($this->eventHandlers[$name] as $cb) { |
90
|
|
|
if (call_user_func_array($cb, $args) !== 0) { |
91
|
|
|
++$cnt; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
return $cnt; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Use it to define event name, when one callback was bind to more than one events |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getLastEventName() { |
103
|
|
|
return $this->lastEventName; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Bind event or events |
108
|
|
|
* @param string|array $event Event name |
109
|
|
|
* @param callable $cb Callback |
110
|
|
|
* @return this |
|
|
|
|
111
|
|
|
*/ |
112
|
|
|
public function bind($event, $cb) { |
113
|
|
|
if ($cb !== null) { |
114
|
|
|
$cb = CallbackWrapper::wrap($cb); |
115
|
|
|
} |
116
|
|
|
is_array($event) or $event = [$event]; |
|
|
|
|
117
|
|
|
foreach ($event as $e) { |
|
|
|
|
118
|
|
|
CallbackWrapper::addToArray($this->eventHandlers[$e], $cb); |
119
|
|
|
} |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Bind event or events |
125
|
|
|
* @alias EventHandlers::bind |
126
|
|
|
* @param string|array $event Event name |
127
|
|
|
* @param callable $cb Callback |
128
|
|
|
* @return this |
|
|
|
|
129
|
|
|
*/ |
130
|
|
|
public function on($event, $cb) { |
131
|
|
|
return $this->bind($event, $cb); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Unbind event(s) or callback from event(s) |
136
|
|
|
* @param string|array $event Event name |
137
|
|
|
* @param callable $cb Callback, optional |
|
|
|
|
138
|
|
|
* @return this |
|
|
|
|
139
|
|
|
*/ |
140
|
|
|
public function unbind($event, $cb = null) { |
141
|
|
|
if ($cb !== null) { |
142
|
|
|
$cb = CallbackWrapper::wrap($cb); |
143
|
|
|
} |
144
|
|
|
is_array($event) or $event = [$event]; |
|
|
|
|
145
|
|
|
$success = true; |
|
|
|
|
146
|
|
|
foreach ($event as $e) { |
|
|
|
|
147
|
|
|
if (!isset($this->eventHandlers[$e])) { |
148
|
|
|
$success = false; |
|
|
|
|
149
|
|
|
continue; |
150
|
|
|
} |
151
|
|
|
if ($cb === null) { |
152
|
|
|
unset($this->eventHandlers[$e]); |
153
|
|
|
continue; |
154
|
|
|
} |
155
|
|
|
CallbackWrapper::removeFromArray($this->eventHandlers[$e], $cb); |
156
|
|
|
} |
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Unbind event(s) or callback from event(s) |
162
|
|
|
* @alias EventHandlers::unbind |
163
|
|
|
* @param string|array $event Event name |
164
|
|
|
* @param callable $cb Callback, optional |
165
|
|
|
* @return this |
|
|
|
|
166
|
|
|
*/ |
167
|
|
|
public function off($event, $cb) { |
168
|
|
|
return $this->unbind($event, $cb); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Clean up all events |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
|
|
protected function cleanupEventHandlers() { |
176
|
|
|
$this->eventHandlers = []; |
177
|
|
|
//Daemon::log('clean up event handlers '.get_class($this). ' -- '.$this->attrs->server['REQUEST_URI']. PHP_EOL .Debug::backtrace()); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.