| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ByTIC\EventDispatcher\ListenerProviders; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Psr\EventDispatcher\ListenerProviderInterface; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Class PriorityListenerProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package ByTIC\EventDispatcher\ListenerProviders | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->subscribeTo($event, new OneTimeListener($listener), $priority); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function getListenersForEvent(object $event): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |          * @var string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |          * @var PrioritizedListenersForEvent $group | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         foreach ($this->listenersPerEvent as $key => $group) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             if ($event instanceof $key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 yield from $group->getListeners(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         if (method_exists($event, 'eventName')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             yield from $this->getListenersForEventName($event->eventName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | //    public function subscribeListenersFrom(ListenerSubscriber $subscriber): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | //    { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | //        $subscriber->subscribeListeners($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | //    } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  | 
            
                        
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: