| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Cocotte\DependencyInjection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Cocotte\Environment\FromEnvLazyFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Cocotte\Environment\LazyEnvironmentLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Cocotte\Environment\LazyEnvironmentValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Cocotte\Shell\Env; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\DependencyInjection\ContainerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\DependencyInjection\Definition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\DependencyInjection\Reference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | final class LazyEnvironmentPass implements CompilerPassInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public function process(ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $configs = $container->findTaggedServiceIds(LazyEnvironmentValue::LAZY_ENVIRONMENT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $lazyLoaderDefinition = $container->getDefinition(LazyEnvironmentLoader::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         foreach ($configs as $id => $attributes) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             $this->processTag($container, $id, $lazyLoaderDefinition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     private function processTag(ContainerBuilder $container, string $id, Definition $lazyLoaderDefinition): void | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $valueDefinition = $container->getDefinition($id); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         $valueClass = $container->getParameterBag()->resolveValue($valueDefinition->getClass()); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $reflectionValueClass = $container->getReflectionClass($valueClass); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $this->assertLazyEnvironmentValue($reflectionValueClass, $valueClass); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         if (!$valueDefinition->getFactory()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             $this->assertCustomFactory($reflectionValueClass, $valueClass); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             $valueDefinition->setFactory([$valueClass, "fromEnv"]); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             $valueDefinition->setArguments([new Reference(Env::class)]); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $valueDefinition->setLazy(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $lazyLoaderDefinition->addMethodCall('registerValue', [new Reference($id)]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     private function assertLazyEnvironmentValue(\ReflectionClass $reflectionValueClass, string $valueClass): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if (!$reflectionValueClass->implementsInterface(LazyEnvironmentValue::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             throw new \Exception( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 "$valueClass does not implement ".LazyEnvironmentValue::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     private function assertCustomFactory(\ReflectionClass $reflectionValueClass, string $valueClass): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if (!$reflectionValueClass->implementsInterface(FromEnvLazyFactory::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             throw new \Exception( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 "There is not custom factory and $valueClass does not implement ".FromEnvLazyFactory::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 62 |  |  | } |