1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpWinTools\WmiScripting\Support\Events; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Countable; |
7
|
|
|
use PhpWinTools\WmiScripting\Configuration\Config; |
8
|
|
|
|
9
|
|
|
class EventHistoryProvider implements Countable |
10
|
|
|
{ |
11
|
|
|
/** @var Config */ |
12
|
|
|
protected $config; |
13
|
|
|
|
14
|
|
|
/** @var array|FiredEvent[] */ |
15
|
|
|
protected $fired_events = []; |
16
|
|
|
|
17
|
|
|
/** @var array */ |
18
|
|
|
protected $event_cache = [ |
19
|
|
|
'actual' => [], |
20
|
|
|
'ancestors' => [], |
21
|
|
|
'listeners' => [], |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
protected $eventContainer; |
25
|
|
|
|
26
|
|
|
public function __construct(Config $config = null) |
27
|
|
|
{ |
28
|
|
|
$this->config = $config ?? Config::instance(); |
29
|
|
|
|
30
|
|
|
$this->eventContainer = new EventCacheContainer(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function container() |
34
|
|
|
{ |
35
|
|
|
return $this->eventContainer; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function cache() |
39
|
|
|
{ |
40
|
|
|
return $this->event_cache; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Event $event |
45
|
|
|
* @param array|Listener[] $listeners |
46
|
|
|
* |
47
|
|
|
* @return self |
48
|
|
|
*/ |
49
|
|
|
public function add(Event $event, array $listeners = []): self |
50
|
|
|
{ |
51
|
|
|
if ($this->shouldNotTrack()) { |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->fired_events[] = new FiredEvent($event, $listeners); |
56
|
|
|
|
57
|
|
|
return $this->cacheEvent($event, $listeners); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array|FiredEvent[] |
62
|
|
|
*/ |
63
|
|
|
public function all() |
64
|
|
|
{ |
65
|
|
|
return $this->fired_events; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $event |
70
|
|
|
* @param array|Closure|mixed $default |
71
|
|
|
* |
72
|
|
|
* @return array|FiredEvent[]|mixed |
73
|
|
|
*/ |
74
|
|
|
public function get(string $event, $default = []) |
75
|
|
|
{ |
76
|
|
|
return $this->findEvents($this->getFiredEventKeys($event), $default); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $listener |
81
|
|
|
* @param array|Closure|mixed $default |
82
|
|
|
* |
83
|
|
|
* @return array|FiredEvent[]|mixed |
84
|
|
|
*/ |
85
|
|
|
public function getFromListener(string $listener, $default = []) |
86
|
|
|
{ |
87
|
|
|
return $this->findEvents($this->event_cache['listeners'][$listener] ?? [], $default); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $event |
92
|
|
|
* |
93
|
|
|
* @return array|mixed |
94
|
|
|
*/ |
95
|
|
|
public function getFiredEventKeys(string $event) |
96
|
|
|
{ |
97
|
|
|
$keys = []; |
98
|
|
|
|
99
|
|
|
if ($this->wasFiredByName($event)) { |
100
|
|
|
$keys = $this->event_cache['actual'][$event]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($this->wasFiredByDescendant($event)) { |
104
|
|
|
$keys = array_merge($keys, $this->event_cache['ancestors'][$event]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $keys; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
|
|
public function count(): int |
114
|
|
|
{ |
115
|
|
|
return count($this->fired_events); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string|null $event |
120
|
|
|
* |
121
|
|
|
* @return int |
122
|
|
|
*/ |
123
|
|
|
public function eventCount(string $event = null): int |
124
|
|
|
{ |
125
|
|
|
return is_null($event) ? $this->count() : count($this->get($event)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return int |
130
|
|
|
*/ |
131
|
|
|
public function lastIndex(): int |
132
|
|
|
{ |
133
|
|
|
return $this->count() - 1; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $event |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function hasFired(string $event): bool |
142
|
|
|
{ |
143
|
|
|
return $this->wasFiredByName($event) || $this->wasFiredByDescendant($event); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $event |
148
|
|
|
* |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
|
|
public function hasNotFired(string $event): bool |
152
|
|
|
{ |
153
|
|
|
return $this->hasFired($event) === false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $event |
158
|
|
|
* |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
public function wasFiredByName(string $event): bool |
162
|
|
|
{ |
163
|
|
|
return array_key_exists($event, $this->event_cache['actual']); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $event |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
public function wasFiredByDescendant(string $event): bool |
172
|
|
|
{ |
173
|
|
|
return array_key_exists($event, $this->event_cache['ancestors']); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param array $event_keys |
178
|
|
|
* @param array|Closure|mixed $default |
179
|
|
|
* |
180
|
|
|
* @return array|FiredEvent[]|mixed |
181
|
|
|
*/ |
182
|
|
|
protected function findEvents(array $event_keys = [], $default = []) |
183
|
|
|
{ |
184
|
|
|
$events = array_values(array_map(function ($event_key) { |
185
|
|
|
return $this->fired_events[$event_key]; |
186
|
|
|
}, $event_keys)); |
187
|
|
|
|
188
|
|
|
if (empty($events)) { |
189
|
|
|
return is_callable($default) ? $default() : $default; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $events; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param Event $event |
197
|
|
|
* @param array|Listener[] $listeners |
198
|
|
|
* |
199
|
|
|
* @return self |
200
|
|
|
*/ |
201
|
|
|
protected function cacheEvent(Event $event, array $listeners = []): self |
202
|
|
|
{ |
203
|
|
|
$this->event_cache['actual'][get_class($event)][] = $this->lastIndex(); |
204
|
|
|
$this->eventContainer->addActual(get_class($event), $this->lastIndex()); |
205
|
|
|
|
206
|
|
|
$this->cacheEventSet($event, 'ancestors', class_parents($event), function ($item) { |
207
|
|
|
return $item; |
208
|
|
|
}); |
209
|
|
|
|
210
|
|
|
$this->cacheEventSet($event, 'listeners', $listeners, function (Listener $listener) { |
211
|
|
|
return get_class($listener); |
212
|
|
|
}); |
213
|
|
|
|
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
protected function cacheEventSet(Event $event, string $set_name, array $set, Closure $getItemKey) |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
array_map(function ($item) use ($set_name, $getItemKey) { |
220
|
|
|
$this->event_cache[$set_name][$getItemKey($item)][] = $this->lastIndex(); |
221
|
|
|
}, $set); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return bool |
226
|
|
|
*/ |
227
|
|
|
protected function shouldTrack(): bool |
228
|
|
|
{ |
229
|
|
|
return $this->config->shouldTrackEvents(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @return bool |
234
|
|
|
*/ |
235
|
|
|
protected function shouldNotTrack(): bool |
236
|
|
|
{ |
237
|
|
|
return $this->shouldTrack() === false; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.