-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathHookTrait.php
More file actions
366 lines (314 loc) · 13.5 KB
/
HookTrait.php
File metadata and controls
366 lines (314 loc) · 13.5 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
declare(strict_types=1);
namespace Atk4\Core;
trait HookTrait
{
/** @var array<string, array<int, array<int<0, max>, array{\Closure, list<mixed>}>>> Configured hooks (callbacks). */
protected array $hooks = [];
/** @var int<0, max> Next hook index counter. */
private int $_hookIndexCounter = 0;
/** @var \WeakReference<static>|null */
private ?\WeakReference $_hookOrigThis = null;
/**
* Optimize GC. When a Closure is guaranteed to be rebound before invoke, it can be rebound
* to (deduplicated) fake instance before safely.
*/
private function _rebindHookFxToFakeInstance(\Closure $fx): \Closure
{
$fxThis = (new \ReflectionFunction($fx))->getClosureThis();
$instanceWithoutConstructorCache = new class {
/** @var array<class-string, object> */
private static array $_instances = [];
/**
* @param class-string $class
*/
public function getInstance(string $class): object
{
if (!isset(self::$_instances[$class])) {
$dummyInstance = (new \ReflectionClass($class))->newInstanceWithoutConstructor();
foreach ([$class, ...array_keys(class_parents($class))] as $scope) {
\Closure::bind(static function () use ($dummyInstance) {
foreach (array_keys(get_object_vars($dummyInstance)) as $k) {
unset($dummyInstance->{$k});
}
}, null, $scope)();
}
self::$_instances[$class] = $dummyInstance;
}
return self::$_instances[$class];
}
};
$fakeThis = $instanceWithoutConstructorCache->getInstance(get_class($fxThis));
return \Closure::bind($fx, $fakeThis);
}
/**
* When hook Closure is bound to $this, rebinding all hooks after clone can be slow, optimize clone
* by unbinding $this in favor of rebinding $this when hook is invoked.
*/
private function _unbindHookFxIfBoundToThis(\Closure $fx, bool $isShort): \Closure
{
$fxThis = (new \ReflectionFunction($fx))->getClosureThis();
if ($fxThis !== $this) {
return $fx;
}
$fx = $this->_rebindHookFxToFakeInstance($fx);
return $this->_makeHookDynamicFx(null, $fx, $isShort);
}
private function _rebindHooksIfCloned(): void
{
if ($this->_hookOrigThis !== null) {
$hookOrigThis = $this->_hookOrigThis->get();
if ($hookOrigThis === $this) {
return;
}
foreach ($this->hooks as $spot => $hooksByPriority) {
foreach ($hooksByPriority as $priority => $hooksByIndex) {
foreach ($hooksByIndex as $index => $hookData) {
$fxRefl = new \ReflectionFunction($hookData[0]);
$fxThis = $fxRefl->getClosureThis();
if ($fxThis === null) {
continue;
}
// TODO we throw only if the class name is the same, otherwise the check is too strict
// and on a bad side - we should not throw when an object with a hook is cloned,
// but instead we should throw once the closure this object is cloned
// example of legit use: https://github.com/atk4/audit/blob/eb9810e085/src/Controller.php#L85
if (get_class($fxThis) === static::class || preg_match('~^Atk4\\\(?:Core|Data)~', get_class($fxThis))) {
throw (new Exception('Object cannot be cloned with hook bound to a different object than this'))
->addMoreInfo('closure_file', $fxRefl->getFileName())
->addMoreInfo('closure_start_line', $fxRefl->getStartLine());
}
}
}
}
}
$this->_hookOrigThis = \WeakReference::create($this);
}
/**
* Add another callback to be executed during hook($spot);.
*
* Lower priority is called sooner.
*
* If priority is negative, then hook is prepended (executed first for the same priority).
*
* @param list<mixed> $args
*
* @return int<0, max> index under which the hook was added
*/
public function onHook(string $spot, \Closure $fx, array $args = [], int $priority = 5): int
{
$this->_rebindHooksIfCloned();
$fx = $this->_unbindHookFxIfBoundToThis($fx, false);
$index = $this->_hookIndexCounter++;
$data = [$fx, $args];
if ($priority < 0) {
$this->hooks[$spot][$priority] = [$index => $data] + ($this->hooks[$spot][$priority] ?? []);
} else {
$this->hooks[$spot][$priority][$index] = $data;
}
return $index;
}
/**
* Same as onHook() except no $this is passed to the callback as the 1st argument.
*
* @param list<mixed> $args
*
* @return int<0, max> index under which the hook was added
*/
public function onHookShort(string $spot, \Closure $fx, array $args = [], int $priority = 5): int
{
// create long callback and bind it to the same scope class and object
$fxRefl = new \ReflectionFunction($fx);
$fxScopeClassRefl = $fxRefl->getClosureScopeClass();
$fxThis = $fxRefl->getClosureThis();
if ($fxThis === null) {
$fxLong = \Closure::bind(static function ($ignore, &...$args) use ($fx) {
return $fx(...$args);
}, null, $fxScopeClassRefl !== null ? $fxScopeClassRefl->getName() : null);
} else {
$fxLong = $this->_unbindHookFxIfBoundToThis($fx, true);
if ($fxLong === $fx) {
$fx = $this->_rebindHookFxToFakeInstance($fx);
$fxLong = \Closure::bind(function ($ignore, &...$args) use ($fx) {
return \Closure::bind($fx, $this)(...$args);
}, $fxThis, $fxScopeClassRefl->getName());
}
}
return $this->onHook($spot, $fxLong, $args, $priority);
}
/**
* @param \Closure($this): object $getFxThisFx
*/
private function _makeHookDynamicFx(?\Closure $getFxThisFx, \Closure $fx, bool $isShort): \Closure
{
if ($getFxThisFx !== null) {
$getFxThisFxThis = (new \ReflectionFunction($getFxThisFx))->getClosureThis();
if ($getFxThisFxThis !== null) {
throw new \TypeError('New $this getter must be static');
}
}
$fx = $this->_rebindHookFxToFakeInstance($fx);
return static function (self $target, &...$args) use ($getFxThisFx, $fx, $isShort) {
if ($getFxThisFx === null) {
$fxThis = $target;
} else {
$fxThis = $getFxThisFx($target); // @phpstan-ignore argument.type
if (!is_object($fxThis)) { // @phpstan-ignore function.alreadyNarrowedType
throw new \TypeError('New $this must be an object');
}
}
return $isShort
? \Closure::bind($fx, $fxThis)(...$args)
: \Closure::bind($fx, $fxThis)($target, ...$args);
};
}
/**
* Same as onHook() except $this of the callback is dynamically rebound before invoke.
*
* @param \Closure($this): object $getFxThisFx
* @param list<mixed> $args
*
* @return int<0, max> index under which the hook was added
*/
public function onHookDynamic(string $spot, \Closure $getFxThisFx, \Closure $fx, array $args = [], int $priority = 5): int
{
return $this->onHook($spot, $this->_makeHookDynamicFx($getFxThisFx, $fx, false), $args, $priority);
}
/**
* Same as onHookDynamic() except no $this is passed to the callback as the 1st argument.
*
* @param \Closure($this): object $getFxThisFx
* @param list<mixed> $args
*
* @return int<0, max> index under which the hook was added
*/
public function onHookDynamicShort(string $spot, \Closure $getFxThisFx, \Closure $fx, array $args = [], int $priority = 5): int
{
return $this->onHook($spot, $this->_makeHookDynamicFx($getFxThisFx, $fx, true), $args, $priority);
}
/**
* Returns true if at least one callback is defined for this hook.
*
* @param ($priorityIsIndex is true ? int<0, max> : int|null) $priority filter specific priority, null for all
* @param bool $priorityIsIndex filter by index instead of priority
*/
public function hookHasCallbacks(string $spot, ?int $priority = null, bool $priorityIsIndex = false): bool
{
if (!isset($this->hooks[$spot])) {
return false;
} elseif ($priority === null) {
return true;
}
if ($priorityIsIndex) {
$index = $priority;
unset($priority);
foreach (array_keys($this->hooks[$spot]) as $priority) {
if (isset($this->hooks[$spot][$priority][$index])) {
return true;
}
}
return false;
}
return isset($this->hooks[$spot][$priority]);
}
/**
* Delete all hooks for specified spot, priority and index.
*
* @param ($priorityIsIndex is true ? int<0, max> : int|null) $priority filter specific priority, null for all
* @param bool $priorityIsIndex filter by index instead of priority
*
* @return static
*/
public function removeHook(string $spot, ?int $priority = null, bool $priorityIsIndex = false)
{
if ($priority !== null) {
if ($priorityIsIndex) {
$index = $priority;
unset($priority);
foreach (array_keys($this->hooks[$spot] ?? []) as $priority) {
unset($this->hooks[$spot][$priority][$index]);
if ($this->hooks[$spot][$priority] === []) {
unset($this->hooks[$spot][$priority]);
}
}
} else {
unset($this->hooks[$spot][$priority]);
}
if (($this->hooks[$spot] ?? null) === []) {
unset($this->hooks[$spot]);
}
} else {
unset($this->hooks[$spot]);
}
return $this;
}
/**
* Execute all closures assigned to $spot.
*
* @param list<mixed> $args
* @param mixed $brokenBy
*
* @param-out HookBreaker|null $brokenBy
*
* @return array<int<0, max>, mixed>|mixed Array of responses indexed by hook indexes or value specified to breakHook
*/
public function hook(string $spot, array $args = [], &$brokenBy = null)
{
$brokenBy = null;
$this->_rebindHooksIfCloned();
$return = [];
if (isset($this->hooks[$spot])) {
krsort($this->hooks[$spot]); // lower priority is called sooner
$hooksBackup = $this->hooks[$spot];
$priorities = array_keys($hooksBackup);
try {
while (($priority = array_pop($priorities)) !== null) {
$hooks2Backup = $this->hooks[$spot][$priority];
$indexes = array_reverse(array_keys($hooks2Backup));
while (($index = array_pop($indexes)) !== null) {
[$hookFx, $hookArgs] = $this->hooks[$spot][$priority][$index];
$return[$index] = $hookFx($this, ...$args, ...$hookArgs);
if (!isset($this->hooks[$spot][$priority])) {
break;
} elseif ($hooks2Backup !== $this->hooks[$spot][$priority]) {
$hooks2Backup = $this->hooks[$spot][$priority];
$indexes = array_reverse(array_keys($hooks2Backup));
foreach ($indexes as $k => $i) {
if ($i <= $index) {
unset($indexes[$k]);
}
}
}
}
if (!isset($this->hooks[$spot])) { // @phpstan-ignore isset.offset, isset.initializedProperty, notIdentical.alwaysTrue
break;
} elseif ($hooksBackup !== $this->hooks[$spot]) {
krsort($this->hooks[$spot]);
$hooksBackup = $this->hooks[$spot];
$priorities = array_keys($hooksBackup);
foreach ($priorities as $k => $p) {
if ($p <= $priority) {
unset($priorities[$k]);
}
}
}
}
} catch (HookBreaker $e) {
$brokenBy = $e;
return $e->getReturnValue();
}
}
return $return;
}
/**
* When called from inside a hook closure, it will stop execution of other
* closures on the same hook. The passed argument will be returned by the
* hook method.
*
* @param mixed $return What would hook() return?
*/
public function breakHook($return): void
{
throw new HookBreaker($return);
}
}