-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvoker.php
210 lines (183 loc) · 5.19 KB
/
Invoker.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
namespace Phug;
use Phug\Event\ListenerQueue;
use ReflectionException;
use ReflectionFunction;
use ReflectionMethod;
use ReflectionNamedType;
use RuntimeException;
class Invoker implements InvokerInterface
{
/**
* List of callbacks grouped by type.
*
* @var ListenerQueue[]
*/
private $invokables;
/**
* Event constructor.
*
* @param callable[] $invokables list of callbacks to start with
*
* @throws ReflectionException
*/
public function __construct(array $invokables = [])
{
$this->reset();
$this->add($invokables);
}
/**
* Remove all callbacks from the list.
*/
public function reset()
{
$this->invokables = [];
}
/**
* Get all callbacks from the list.
*
* @return ListenerQueue[]
*/
public function all()
{
return $this->invokables;
}
/**
* Add a list of callbacks.
*
* @param callable[] $invokables list of callbacks
*
* @throws ReflectionException
* @throws RuntimeException
*/
public function add(array $invokables)
{
foreach ($invokables as $index => $invokable) {
$name = '#'.($index + 1);
if (!is_callable($invokable)) {
throw new RuntimeException("The $name value is not callable.");
}
$this->addCallback($invokable, $name);
}
}
/**
* Add a single callback.
*
* @param callable $invokable typed callback
*
* @throws ReflectionException
* @throws RuntimeException
*/
public function addCallback(callable $invokable, $name = null)
{
$parameter = static::getCallbackType($invokable);
if (!is_string($parameter)) {
throw new RuntimeException(
'Passed callback '.($name ?: gettype($invokable)).
' should have at least 1 argument and this first argument must have a typehint.'
);
}
if (!isset($this->invokables[$parameter])) {
$this->invokables[$parameter] = new ListenerQueue();
}
$this->invokables[$parameter]->insert($invokable, 0);
}
/**
* Remove callbacks from the list.
*
* @param callable[] $invokables list of callbacks
*/
public function remove(array $invokables)
{
$this->invokables = array_filter(array_map(function (ListenerQueue $queue) use (&$invokables) {
$filteredQueue = new ListenerQueue();
foreach ($queue as $invokable) {
if (!in_array($invokable, $invokables, true)) {
$filteredQueue->insert($invokable, 0);
}
}
return $filteredQueue;
}, $this->invokables), function (ListenerQueue $queue) {
return $queue->count();
});
}
/**
* Remove callbacks from the list by a given class/interface name.
*
* @param string $type exact type of a callback argument.
*/
public function removeByType($type)
{
unset($this->invokables[$type]);
}
/**
* Return listeners queues for types that match the given event.
*
* @param object $event instance of callback input.
*
* @return iterable|ListenerQueue[]
*/
public function getQueuesByEvent($event)
{
foreach ($this->invokables as $type => $invokables) {
if (is_a($event, $type)) {
yield $type => $invokables;
}
}
}
/**
* Return callbacks for types that match the given event.
*
* @param object $event instance of callback input.
*
* @return iterable|callable[]
*/
public function getCallbacksByEvent($event)
{
foreach ($this->getQueuesByEvent($event) as $invokables) {
foreach ($invokables as $invokable) {
yield $invokable;
}
}
}
/**
* Invoke callbacks that match the passed event.
*
* @param object $event instance of callback input.
*
* @return array
*/
public function invoke($event)
{
$invocations = [];
foreach ($this->getCallbacksByEvent($event) as $invokable) {
$invocations[] = $invokable($event);
}
return $invocations;
}
/**
* Return the typehint as string of the first argument of a given callback or null if not typed.
*
* @param callable $invokable closure or callable
*
* @throws ReflectionException
*
* @return string|null
*/
public static function getCallbackType(callable $invokable)
{
$reflection = is_array($invokable)
? new ReflectionMethod($invokable[0], $invokable[1])
: new ReflectionFunction($invokable);
$parameters = $reflection->getParameters();
$type = null;
if (count($parameters) && method_exists($parameters[0], 'getType')) {
$type = $parameters[0]->getType();
}
if ($type instanceof ReflectionNamedType) {
return $type->getName();
}
$dump = @strval($parameters[0]);
return preg_match('/>\s(\S+)\s\$/', $dump, $match) ? $match[1] : null;
}
}