| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Proxy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Yiisoft\Proxy\Config\ClassConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | final class ProxyManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     private ?string $cachePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private ClassRenderer $classRenderer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private ClassConfigFactory $classConfigFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private ClassCache $classCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 8 |  |     public function __construct(string $cachePath = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 8 |  |         $this->cachePath = $cachePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 8 |  |         $this->classCache = new ClassCache($cachePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 8 |  |         $this->classRenderer = new ClassRenderer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 8 |  |         $this->classConfigFactory = new ClassConfigFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 8 |  |     public function createObjectProxy( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         string $baseStructure, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         string $parentProxyClass, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         array $constructorArguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     ): ?object { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 8 |  |         $className = $baseStructure . 'Proxy'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 8 |  |         $shortClassName = $this->getProxyClassName($className); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 8 |  |         if (class_exists($shortClassName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 5 |  |             return new $shortClassName(...$constructorArguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 3 |  |         if (!($classDeclaration = $this->classCache->get($className, $parentProxyClass))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 3 |  |             $classConfig = $this->classConfigFactory->getClassConfig($baseStructure); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 3 |  |             $classConfig = $this->generateProxyClassConfig($classConfig, $parentProxyClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 3 |  |             $classDeclaration = $this->classRenderer->render($classConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  |             $this->classCache->set($className, $parentProxyClass, $classDeclaration); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 3 |  |         if ($this->cachePath === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |             eval(str_replace('<?php', '', $classDeclaration)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |             $path = $this->classCache->getClassPath($className, $parentProxyClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |             require $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 3 |  |         return new $shortClassName(...$constructorArguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 3 |  |     private function generateProxyClassConfig( | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         ClassConfig $classConfig, | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         string $parentProxyClass | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     ): ClassConfig { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 3 |  |         if ($classConfig->isInterface) { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 2 |  |             $classConfig->isInterface = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 2 |  |             $classConfig->interfaces = [$classConfig->name]; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 3 |  |         $classConfig->parent = $parentProxyClass; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 3 |  |         $classConfig->name .= 'Proxy'; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 3 |  |         $classConfig->shortName = $this->getProxyClassName($classConfig->name); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 3 |  |         foreach ($classConfig->methods as $methodIndex => $method) { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 3 |  |             foreach ($method->modifiers as $index => $modifier) { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 3 |  |                 if ($modifier === 'abstract') { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 2 |  |                     unset($classConfig->methods[$methodIndex]->modifiers[$index]); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 3 |  |         return $classConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 8 |  |     private function getProxyClassName(string $fullClassName): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 8 |  |         return str_replace('\\', '_', $fullClassName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |  |