| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the LaravelYaml package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Théo FIDRY <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Fidry\LaravelYaml\DependencyInjection\Builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Fidry\LaravelYaml\DependencyInjection\Definition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Fidry\LaravelYaml\DependencyInjection\Definition\Alias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Fidry\LaravelYaml\DependencyInjection\Definition\DecorationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Fidry\LaravelYaml\DependencyInjection\Definition\Reference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Fidry\LaravelYaml\DependencyInjection\Definition\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Fidry\LaravelYaml\DependencyInjection\Definition\ServiceInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Illuminate\Contracts\Foundation\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\ExpressionLanguage\Expression; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @author Théo FIDRY <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | final class ContainerBuilder implements BuilderInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @var Alias[] | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     private $aliases = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     private $parameters = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * @var ServiceInterface[] | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     private $services = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @var BuilderInterface|null | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     private $parametersBuilder; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @var BuilderInterface|null | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     private $aliasesBuilder; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * @var BuilderInterface|null | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     private $servicesBuilder; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         BuilderInterface $parametersBuilder = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         BuilderInterface $aliasesBuilder = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         BuilderInterface $servicesBuilder = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |         $this->parametersBuilder = $parametersBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |         $this->aliasesBuilder = $aliasesBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |         $this->servicesBuilder = $servicesBuilder; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * @param string                            $key | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * @param array|Reference|string|Expression $value | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function setParameter($key, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $this->parameters[$key] = $value; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * @param Alias $alias | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public function addAlias(Alias $alias) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $this->aliases[$alias->getAlias()] = $alias; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function registerService(ServiceInterface $service) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if ($service instanceof DecorationInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $this->getAndRebuilderDeprecatedService($service); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $service = $this->rebuildServiceWithName($service->getDecorates(), $service); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->services[$service->getName()] = $service; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 2 |  |     public function build(Application $application) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 2 |  |         $parametersBuilder = (null === $this->parametersBuilder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |             ? new ParametersBuilder($this->parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 2 |  |             : $this->parametersBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 2 |  |         $parameters = $parametersBuilder->build($application); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |         $servicesBuilder = (null === $this->servicesBuilder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |             ? new ServicesBuilder($this->services, $parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 2 |  |             : $this->servicesBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 2 |  |         $servicesBuilder->build($application); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 2 |  |         $aliasesBuilder = (null === $this->aliasesBuilder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |             ? new AliasesBuilder($this->aliases) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  |             : $this->aliasesBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |         $aliasesBuilder->build($application); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     private function getAndRebuilderDeprecatedService(DecorationInterface $service) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $oldDecorated = $this->services[$service->getDecorates()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $newDecorated = $this->rebuildServiceWithName($service->getDecorationInnerName(), $oldDecorated); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $this->registerService($newDecorated); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |      * @param string           $name | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |      * @param ServiceInterface $service | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      * @return Service | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     private function rebuildServiceWithName($name, ServiceInterface $service) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         return new Service( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             $service->getClass(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             $service->getArguments(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             $service->getAutowiringTypes(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             $service->getTags() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 140 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |  |