| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Yiisoft\EventDispatcher\Provider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Psr\EventDispatcher\ListenerProviderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Provider is a listener provider that registers event listeners for interfaces used in callable type-hints | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * and gives out a list of handlers by event interface provided for further use with Dispatcher. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * $provider = new Yiisoft\EventDispatcher\Provider\Provider(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * // add some listeners | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * $provider->attach(function (AfterDocumentProcessed $event) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  *    $document = $event->getDocument(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *    // do something with document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * // lock provider when done | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * $provider->lock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | final class Provider implements ListenerProviderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private bool $locked = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var callable[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private array $listeners = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 7 |  |     public function getListenersForEvent(object $event): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 7 |  |         yield from $this->listenersFor(get_class($event)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 7 |  |         yield from $this->listenersFor(...array_values(class_parents($event))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 7 |  |         yield from $this->listenersFor(...array_values(class_implements($event))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 7 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Attaches listener to corresponding event based on the type-hint used for the event argument. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Method signature should be the following: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      *  function (MyEvent $event): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * Any callable could be used be it a closure, invokable object or array referencing a class or object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param callable $listener | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param string $eventClassName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 9 |  |     public function attach(callable $listener, string $eventClassName = ''): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 9 |  |         if ($this->locked) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |             throw new \RuntimeException('Can not attach more event listeners. Provider is locked.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 8 |  |         if ($eventClassName === '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 7 |  |             $eventClassName = $this->getParameterType($listener); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 7 |  |         $this->listeners[$eventClassName][] = $listener; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 7 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * Derives the interface type of the first argument of a callable. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @suppress PhanUndeclaredMethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param callable $callable The callable for which we want the parameter type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return string The interface the parameter is type hinted on. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 7 |  |     private function getParameterType(callable $callable): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         // This try-catch is only here to keep OCD linters happy about uncaught reflection exceptions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             switch (true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 // See note on isClassCallable() for why this must be the first case. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 7 |  |                 case $this->isClassCallable($callable): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |                     $reflect = new \ReflectionClass($callable[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |                     $params = $reflect->getMethod($callable[1])->getParameters(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 6 |  |                 case $this->isFunctionCallable($callable): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 5 |  |                 case $this->isClosureCallable($callable): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 4 |  |                     $reflect = new \ReflectionFunction($callable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 4 |  |                     $params = $reflect->getParameters(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 4 |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  |                 case $this->isObjectCallable($callable): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |                     $reflect = new \ReflectionObject($callable[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |                     $params = $reflect->getMethod($callable[1])->getParameters(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |                 case $this->isInvokable($callable): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |                     $params = (new \ReflectionMethod($callable, '__invoke'))->getParameters(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                     throw new \InvalidArgumentException('Not a recognized type of callable'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 7 |  |             $reflectedType = isset($params[0]) ? $params[0]->getType() : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 7 |  |             if ($reflectedType === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |                 throw new \InvalidArgumentException('Listeners must declare an object type they can accept.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 6 |  |             $type = $reflectedType->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |         } catch (\ReflectionException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             throw new \RuntimeException('Type error registering listener.', 0, $e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 6 |  |         return $type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * Determines if a callable represents a function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * Or at least a reasonable approximation, since a function name may not be defined yet. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @param callable $callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @return True if the callable represents a function, false otherwise. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 6 |  |     private function isFunctionCallable(callable $callable): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         // We can't check for function_exists() because it may be included later by the time it matters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 6 |  |         return is_string($callable); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * Determines if a callable represents a closure/anonymous function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param callable $callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @return True if the callable represents a closure object, false otherwise. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 5 |  |     private function isClosureCallable(callable $callable): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 5 |  |         return $callable instanceof \Closure; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @param callable $callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @return True if the callable represents an invokable object, false otherwise. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 1 |  |     private function isInvokable(callable $callable): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 1 |  |         return is_object($callable); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * Determines if a callable represents a method on an object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param callable $callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @return True if the callable represents a method object, false otherwise. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 2 |  |     private function isObjectCallable(callable $callable): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 2 |  |         return is_array($callable) && is_object($callable[0]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * Determines if a callable represents a static class method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * The parameter here is untyped so that this method may be called with an | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * array that represents a class name and a non-static method.  The routine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * to determine the parameter type is identical to a static method, but such | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * an array is still not technically callable.  Omitting the parameter type here | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * allows us to use this method to handle both cases. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * Note that this method must therefore be the first in the switch statement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * above, or else subsequent calls will break as the array is not going to satisfy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * the callable type hint but it would pass `is_callable()`.  Because PHP. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * @param callable $callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * @return True if the callable represents a static method, false otherwise. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 173 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 174 | 7 |  |     private function isClassCallable($callable): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 176 | 7 |  |         return is_array($callable) && is_string($callable[0]) && class_exists($callable[0]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * @param string ...$eventClassNames | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      * @return iterable<callable> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 7 |  |     private function listenersFor(string ...$eventClassNames): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 7 |  |         foreach ($eventClassNames as $eventClassName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 7 |  |             if (isset($this->listeners[$eventClassName])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 | 7 |  |                 yield from $this->listeners[$eventClassName]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 7 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      * Lock provider so no listeners could be attached. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      * It is useful to prevent adding event listeners runtime. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 1 |  |     public function lock(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 1 |  |         $this->locked = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 | 1 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 200 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 201 |  |  |  | 
            
                        
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.