|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace pjpawel\LightApi\Component\Event; |
|
4
|
|
|
|
|
5
|
|
|
use pjpawel\LightApi\Exception\ProgrammerException; |
|
6
|
|
|
|
|
7
|
|
|
class EventHandler |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
public const KERNEL_AFTER_BOOT = 'KernelAfterBoot'; |
|
11
|
|
|
public const KERNEL_BEFORE_REQUEST = 'KernelBeforeRequest'; |
|
12
|
|
|
public const KERNEL_AFTER_REQUEST = 'KernelAfterRequest'; |
|
13
|
|
|
public const KERNEL_BEFORE_COMMAND = 'KernelBeforeCommand'; |
|
14
|
|
|
public const KERNEL_AFTER_COMMAND = 'KernelAfterCommand'; |
|
15
|
|
|
public const KERNEL_ON_DESTRUCT = 'KernelOnDestruct'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array<string,EventInterface> |
|
19
|
|
|
*/ |
|
20
|
|
|
private array $events = []; |
|
21
|
|
|
|
|
22
|
|
|
public function registerEvent(string $id, ?callable $callable, null|array|string $data): void |
|
23
|
|
|
{ |
|
24
|
|
|
if (isset($callable)) { |
|
25
|
|
|
$this->events[$id] = new CallbackEvent($callable, $data ?? []); |
|
|
|
|
|
|
26
|
|
|
} elseif (is_array($data)) { |
|
|
|
|
|
|
27
|
|
|
$dataLen = count($data); |
|
28
|
|
|
if ($dataLen < 2) { |
|
29
|
|
|
throw new ProgrammerException('Cannot pass less than 2 arguments to ' . $id); |
|
30
|
|
|
} elseif ($dataLen == 2) { |
|
31
|
|
|
$this->events[$id] = new DefinedFunctionEvent($data[0], $data[1]); |
|
32
|
|
|
} else { |
|
33
|
|
|
$this->events[$id] = new MethodEvent($data[0], $data[1], array_slice($data, 2)); |
|
34
|
|
|
} |
|
35
|
|
|
} elseif (is_string($data)){ |
|
36
|
|
|
$this->events[$id] = new DefinedFunctionEvent($data, []); |
|
37
|
|
|
} else { |
|
38
|
|
|
throw new ProgrammerException('Cannot register event ' . $id . ' without callable or data'); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function has(string $eventId): bool |
|
43
|
|
|
{ |
|
44
|
|
|
return isset($this->events[$eventId]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $eventId |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
* @throws ProgrammerException|\Exception |
|
51
|
|
|
*/ |
|
52
|
|
|
public function trigger(string $eventId): mixed |
|
53
|
|
|
{ |
|
54
|
|
|
if (!isset($this->events[$eventId])) { |
|
55
|
|
|
throw new ProgrammerException(sprintf('Event %s was called, but it seems that there is no such event', $eventId)); |
|
56
|
|
|
} |
|
57
|
|
|
return $this->events[$eventId]->run(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* This method does not throw exception if Event is not registered |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $eventId |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
*/ |
|
66
|
|
|
public function tryTriggering(string $eventId): mixed |
|
67
|
|
|
{ |
|
68
|
|
|
if (!isset($this->events[$eventId])) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
return $this->events[$eventId]->run(); |
|
72
|
|
|
} |
|
73
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths