Total Complexity | 10 |
Total Lines | 61 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
11 | class PriorityListenerProvider implements ListenerProviderInterface |
||
12 | { |
||
13 | use Traits\ProviderUtilitiesTrait; |
||
14 | use Traits\MakeListenerTrait; |
||
15 | |||
16 | protected $listenersPerEvent = []; |
||
17 | |||
18 | protected function getListenersForEventName(string $eventName): iterable |
||
19 | { |
||
20 | if (!array_key_exists($eventName, $this->listenersPerEvent)) { |
||
21 | return []; |
||
22 | } |
||
23 | |||
24 | return $this->listenersPerEvent[$eventName]->getListeners(); |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @param callable|string $listener |
||
29 | * @param int $priority |
||
30 | * @param string|null $event |
||
31 | * @return void |
||
32 | */ |
||
33 | public function attach( |
||
34 | $listener, |
||
35 | int $priority = 0, |
||
36 | string $event = null |
||
37 | ): void { |
||
38 | $listener = self::makeListener($listener); |
||
39 | $event = $event ?? $this->getParameterType($listener); |
||
40 | $this->subscribeTo($event, $listener, $priority); |
||
41 | } |
||
42 | |||
43 | |||
44 | public function subscribeTo(string $event, callable $listener, int $priority = 0): void |
||
45 | { |
||
46 | $group = array_key_exists($event, $this->listenersPerEvent) |
||
47 | ? $this->listenersPerEvent[$event] |
||
48 | : $this->listenersPerEvent[$event] = new PrioritizedListenersForEvent(); |
||
49 | |||
50 | $group->addListener($listener, $priority); |
||
51 | } |
||
52 | |||
53 | public function subscribeOnceTo(string $event, callable $listener, int $priority = ListenerPriority::NORMAL): void |
||
56 | } |
||
57 | |||
58 | public function getListenersForEvent(object $event): iterable |
||
72 | } |
||
73 | } |
||
74 | |||
75 | // public function subscribeListenersFrom(ListenerSubscriber $subscriber): void |
||
80 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: