| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ByTIC\EventDispatcher\Discovery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Nip\Utility\Oop; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Nip\Utility\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use ReflectionClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use ReflectionException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Finder\Finder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Finder\SplFileInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Class DiscoverEvents | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @package ByTIC\EventDispatcher\ListenerProviders\Discover | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class DiscoverEvents | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Get all of the events and listeners by searching the given listener directory. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param array|string $listenerPaths | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public static function within($listenerPaths): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         return static::getListenerEvents( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             static::getListenerClasses($listenerPaths) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param array|string $paths | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     protected static function getListenerClasses($paths): iterable | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $paths = is_array($paths) ? $paths : [$paths]; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $files = (new Finder)->files()->in($paths); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         foreach ($files as $file) { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             yield from static::classFromFile($file); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * Extract the class name from the given file path. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param  \SplFileInfo  $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param  string  $basePath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     protected static function classFromFile(SplFileInfo $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return Oop::classesInFile($file); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param array $listeners | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     protected static function getListenerEvents(iterable $listeners): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $listenerEvents = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         foreach ($listeners as $listener) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 $listener = new ReflectionClass($listener); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 foreach ($listener->getMethods() as $method) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     if (!$method->isPublic()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                         continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                     if (!Str::is('handle*', $method->name) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                         !isset($method->getParameters()[0])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                         continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                     $eventName = (string)$method->getParameters()[0]->getType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     $listenerEvents[$eventName][] = [$listener->getName(), $method->getName()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             } catch (ReflectionException $e) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return array_filter($listenerEvents); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |  |