| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Psr\Container\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Yiisoft\Definitions\ArrayDefinition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Yiisoft\Definitions\Contract\DefinitionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Yiisoft\Definitions\Contract\ReferenceInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Yiisoft\Definitions\Infrastructure\DefinitionValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Yiisoft\Definitions\Exception\CircularReferenceException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Yiisoft\Definitions\Exception\InvalidConfigException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Yiisoft\Definitions\Exception\NotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Yiisoft\Definitions\Exception\NotInstantiableException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Yiisoft\Definitions\Infrastructure\Normalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Factory allows creating objects passing arguments runtime. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * A factory will try to use a PSR-11 compliant container to get dependencies, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * but will fall back to manual instantiation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * if the container cannot provide a required dependency. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | final class Factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private ?ContainerInterface $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private FactoryContainer $factoryContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var bool $validate Validate definitions when set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private bool $validate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * Factory constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @psalm-param array<string, mixed> $definitions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @throws InvalidConfigException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 86 |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         ContainerInterface $container = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         array $definitions = [], | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         bool $validate = true | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 86 |  |         $this->container = $container; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 86 |  |         $this->factoryContainer = new FactoryContainer($container); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 86 |  |         $this->validate = $validate; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 86 |  |         $this->setMultiple($definitions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 84 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Creates a new object using the given configuration. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * You may view this method as an enhanced version of the `new` operator. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * The method supports creating an object based on a class name, a configuration array or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * an anonymous function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * Below are some usage examples: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * // create an object using a class name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * $object = $factory->create(\Yiisoft\Db\Connection::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * // create an object using a configuration array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * $object = $factory->create([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      *     'class' => \Yiisoft\Db\Connection\Connection::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      *     '__construct()' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      *         'dsn' => 'mysql:host=127.0.0.1;dbname=demo', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      *     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      *     'setUsername()' => ['root'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      *     'setPassword()' => [''], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      *     'setCharset()' => ['utf8'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * Using [[Container|dependency injection container]], this method can also identify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * dependent objects, instantiate them and inject them into the newly created object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param mixed $config The object configuration. This can be specified in one of the following forms: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * - A string: representing the class name of the object to be created. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * - A configuration array: the array must contain a `class` element which is treated as the object class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      *   and the rest of the name-value pairs will be used to initialize the corresponding object properties. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * - A PHP callable: either an anonymous function or an array representing a class method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      *   (`[$class or $object, $method]`). The callable should return a new instance of the object being created. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @throws InvalidConfigException If the configuration is invalid. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @throws CircularReferenceException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @throws NotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @throws NotInstantiableException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @return mixed|object The created object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @psalm-template T | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @psalm-param mixed|class-string<T> $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @psalm-return ($config is class-string ? T : mixed) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @psalm-suppress MixedReturnStatement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 83 |  |     public function create($config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 83 |  |         if ($this->validate) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 79 |  |             DefinitionValidator::validate($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 79 |  |         if (is_string($config)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 62 |  |             if ($this->factoryContainer->hasDefinition($config)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 59 |  |                 return $this->factoryContainer->get($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 3 |  |             throw new NotFoundException($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 19 |  |         $definition = $this->createDefinition($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 18 |  |         if ($definition instanceof ArrayDefinition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 16 |  |             $definition->setReferenceContainer($this->factoryContainer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 18 |  |             $container = ($this->container === null || $definition instanceof ReferenceInterface) ? $this->factoryContainer : $this->container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 18 |  |             return $definition->resolve($container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } finally { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 18 |  |             if ($definition instanceof ArrayDefinition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 18 |  |                 $definition->setReferenceContainer(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * Sets a definition to the factory. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @param mixed $definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @throws InvalidConfigException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 54 |  |     public function set(string $id, $definition): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 54 |  |         if ($this->validate) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 51 |  |             DefinitionValidator::validate($definition, $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 51 |  |         $this->factoryContainer->setDefinition($id, $definition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 51 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * Sets multiple definitions at once. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @param array $definitions definitions indexed by their ids | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @psalm-param array<string, mixed> $definitions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * @throws InvalidConfigException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 86 |  |     public function setMultiple(array $definitions): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         /** @var mixed $definition */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 86 |  |         foreach ($definitions as $id => $definition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 52 |  |             $this->set($id, $definition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 84 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @param mixed $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @throws InvalidConfigException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 19 |  |     private function createDefinition($config): DefinitionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 19 |  |         $definition = Normalizer::normalize($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         if ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 18 |  |             ($definition instanceof ArrayDefinition) && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 18 |  |             $this->factoryContainer->hasDefinition($definition->getClass()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 16 |  |             $definition = $this->mergeDefinitions( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 16 |  |                 $this->factoryContainer->getDefinition($definition->getClass()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |                 $definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 18 |  |         return $definition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 16 |  |     private function mergeDefinitions(DefinitionInterface $one, ArrayDefinition $two): DefinitionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 | 16 |  |         return $one instanceof ArrayDefinition ? $one->merge($two) : $two; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 189 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 190 |  |  |  |