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