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