| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Borodulin\Container\Autowire; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Borodulin\Container\ContainerException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class ClassNameExtractor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $skipAbstract; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 | 7 |  |     public function __construct(bool $skipAbstract = true) | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 7 |  |         $this->skipAbstract = $skipAbstract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 7 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 7 |  |     public function extract($filename): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 7 |  |         $tokens = token_get_all(file_get_contents($filename)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 7 |  |         $namespace = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 7 |  |         $token = current($tokens); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 7 |  |         while (false !== $token) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 7 |  |             if ($this->skipAbstract && $this->isToken($token, T_ABSTRACT)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 5 |  |                 return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 7 |  |             if ($this->isToken($token, T_NAMESPACE)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 7 |  |                 $namespace = $this->extractNamespace($tokens); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 7 |  |             if ($this->isToken($token, T_CLASS)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 7 |  |                 $className = $this->extractClassName($tokens); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 7 |  |                 return $namespace ? "$namespace\\$className" : $className; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 7 |  |             $token = next($tokens); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 5 |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 7 |  |     private function isToken($token, int $tokenType): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 7 |  |         return \is_array($token) && $token[0] === $tokenType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 7 |  |     private function nextToken(array &$tokens, int $tokenType): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 7 |  |         $token = next($tokens); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 7 |  |         if (($token[0] ?? null) === $tokenType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 7 |  |             return $token[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         throw new ContainerException('Parse error. Expected '.token_name($tokenType)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 7 |  |     private function isNextToken(array &$tokens, int $tokenType): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 7 |  |         $token = next($tokens); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 7 |  |         return $this->isToken($token, $tokenType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 7 |  |     private function extractNamespace(array &$tokens): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 7 |  |         $this->nextToken($tokens, T_WHITESPACE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 7 |  |         $namespace = $this->nextToken($tokens, T_STRING); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 7 |  |         while ($this->isNextToken($tokens, T_NS_SEPARATOR)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 7 |  |             $namespace .= '\\'.$this->nextToken($tokens, T_STRING); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 7 |  |         return $namespace; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 7 |  |     private function extractClassName(array &$tokens): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 7 |  |         $this->nextToken($tokens, T_WHITESPACE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 7 |  |         return $this->nextToken($tokens, T_STRING); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |  |