1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Phact\Container\Builder; |
4
|
|
|
|
5
|
|
|
use Phact\Container\Definition\DefinitionInterface; |
6
|
|
|
use Phact\Container\Details\CallInterface; |
7
|
|
|
use Phact\Container\Details\PropertyInterface; |
8
|
|
|
use Phact\Container\Exceptions\InvalidConfigurationException; |
9
|
|
|
use Phact\Container\Exceptions\InvalidFactoryException; |
10
|
|
|
use Phact\Container\Exceptions\NotFoundException; |
11
|
|
|
use Phact\Container\Inflection\InflectionInterface; |
12
|
|
|
use Psr\Container\ContainerInterface; |
13
|
|
|
|
14
|
|
|
class Builder implements BuilderInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ContainerInterface |
18
|
|
|
*/ |
19
|
|
|
protected $container; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var DependenciesResolver |
23
|
|
|
*/ |
24
|
|
|
protected $dependenciesResolver; |
25
|
|
|
|
26
|
|
|
protected $autoWire = true; |
27
|
|
|
|
28
|
61 |
|
public function __construct(bool $autoWire = true, ?DependenciesResolverInterface $dependenciesResolver = null) |
29
|
|
|
{ |
30
|
61 |
|
$this->autoWire = $autoWire; |
31
|
61 |
|
$this->dependenciesResolver = $dependenciesResolver ?: new DependenciesResolver(); |
32
|
61 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
48 |
|
public function setContainer(ContainerInterface $container): void |
38
|
|
|
{ |
39
|
48 |
|
$this->container = $container; |
40
|
48 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritDoc} |
44
|
|
|
*/ |
45
|
39 |
|
public function construct(DefinitionInterface $definition): object |
46
|
|
|
{ |
47
|
39 |
|
if ($definition->getFactory()) { |
48
|
12 |
|
$object = $this->makeObjectWithFactory($definition); |
49
|
|
|
} else { |
50
|
27 |
|
$object = $this->makeObjectSelf($definition); |
51
|
|
|
} |
52
|
33 |
|
return $object; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
*/ |
58
|
10 |
|
public function configure(object $object, DefinitionInterface $definition): object |
59
|
|
|
{ |
60
|
10 |
|
$this->applyProperties($object, $definition->getProperties()); |
61
|
10 |
|
$this->applyCalls($object, $definition->getCalls()); |
62
|
10 |
|
return $object; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritDoc} |
67
|
|
|
*/ |
68
|
1 |
|
public function invoke(callable $callable, array $arguments = []) |
69
|
|
|
{ |
70
|
1 |
|
return $this->call($callable, $arguments); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritDoc} |
75
|
|
|
*/ |
76
|
2 |
|
public function inflect(object $object, InflectionInterface $inflection): object |
77
|
|
|
{ |
78
|
2 |
|
$this->applyProperties($object, $inflection->getProperties()); |
79
|
2 |
|
$this->applyCalls($object, $inflection->getCalls()); |
80
|
2 |
|
return $object; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param object $object |
85
|
|
|
* @param PropertyInterface[] $properties |
86
|
|
|
*/ |
87
|
12 |
|
protected function applyProperties(object $object, array $properties): void |
88
|
|
|
{ |
89
|
12 |
|
foreach ($properties as $property) { |
90
|
2 |
|
$object->{$property->getName()} = $property->getValue(); |
91
|
|
|
} |
92
|
12 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param object $object |
96
|
|
|
* @param CallInterface[] $calls |
97
|
|
|
* @throws NotFoundException |
98
|
|
|
*/ |
99
|
12 |
|
protected function applyCalls(object $object, array $calls): void |
100
|
|
|
{ |
101
|
12 |
|
foreach ($calls as $call) { |
102
|
4 |
|
$this->call([$object, $call->getMethod()], $call->getArguments()); |
103
|
|
|
} |
104
|
12 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param callable $callable |
108
|
|
|
* @param array $arguments |
109
|
|
|
* @return mixed |
110
|
|
|
* @throws NotFoundException |
111
|
|
|
*/ |
112
|
5 |
|
protected function call(callable $callable, array $arguments = []) |
113
|
|
|
{ |
114
|
5 |
|
$dependencies = []; |
115
|
5 |
|
if ($this->autoWire) { |
116
|
5 |
|
$dependencies = $this->dependenciesResolver->resolveCallableDependencies($callable); |
117
|
|
|
} |
118
|
5 |
|
$parameters = $this->buildParameters($arguments); |
119
|
5 |
|
$args = $this->buildArguments($dependencies, $parameters); |
120
|
5 |
|
return call_user_func_array($callable, $args); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Make object without factory |
125
|
|
|
* |
126
|
|
|
* @param DefinitionInterface $definition |
127
|
|
|
* @return mixed |
128
|
|
|
* @throws NotFoundException |
129
|
|
|
*/ |
130
|
27 |
|
protected function makeObjectSelf(DefinitionInterface $definition) |
131
|
|
|
{ |
132
|
27 |
|
$dependencies = []; |
133
|
|
|
|
134
|
27 |
|
$className = $definition->getClass(); |
135
|
27 |
|
if ($this->autoWire) { |
136
|
26 |
|
$dependencies = $this->dependenciesResolver->resolveConstructorDependencies( |
137
|
26 |
|
$className, |
138
|
26 |
|
$definition->getConstructMethod() |
139
|
|
|
); |
140
|
|
|
} |
141
|
27 |
|
$parameters = $this->buildParameters($definition->getArguments()); |
142
|
27 |
|
$arguments = $this->buildArguments($dependencies, $parameters); |
143
|
|
|
|
144
|
24 |
|
return $this->constructObject($definition->getClass(), $arguments, $definition->getConstructMethod()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Make object with factory |
149
|
|
|
* |
150
|
|
|
* @param DefinitionInterface $definition |
151
|
|
|
* @return mixed |
152
|
|
|
* @throws InvalidConfigurationException |
153
|
|
|
* @throws InvalidFactoryException |
154
|
|
|
* @throws NotFoundException |
155
|
|
|
*/ |
156
|
12 |
|
protected function makeObjectWithFactory(DefinitionInterface $definition) |
157
|
|
|
{ |
158
|
12 |
|
$factory = $definition->getFactory(); |
159
|
|
|
|
160
|
12 |
|
if (!is_callable($factory) || is_array($factory)) { |
161
|
9 |
|
if (!$this->container) { |
162
|
1 |
|
throw new InvalidConfigurationException('Please, provide container for usage non-callable factories'); |
163
|
|
|
} |
164
|
8 |
|
$factory = $this->buildFactoryFromNonCallable($definition); |
165
|
|
|
} |
166
|
|
|
|
167
|
9 |
|
$dependencies = []; |
168
|
9 |
|
if ($this->autoWire) { |
169
|
9 |
|
$dependencies = $this->dependenciesResolver->resolveCallableDependencies($factory); |
170
|
|
|
} |
171
|
9 |
|
$parameters = $this->buildParameters($definition->getArguments()); |
172
|
9 |
|
$arguments = $this->buildArguments($dependencies, $parameters); |
173
|
9 |
|
return call_user_func_array($factory, $arguments); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Build callable factory from non-callable |
178
|
|
|
* |
179
|
|
|
* @param DefinitionInterface $definition |
180
|
|
|
* @return callable |
181
|
|
|
* @throws InvalidFactoryException |
182
|
|
|
*/ |
183
|
8 |
|
protected function buildFactoryFromNonCallable(DefinitionInterface $definition): callable |
184
|
|
|
{ |
185
|
8 |
|
$factory = $definition->getFactory(); |
186
|
8 |
|
if (is_string($factory)) { |
187
|
5 |
|
$factoryId = $this->fetchDependencyId($factory); |
188
|
5 |
|
$factoryMethod = $definition->getConstructMethod() ?: '__invoke'; |
189
|
3 |
|
} elseif (is_array($factory)) { |
190
|
2 |
|
$factoryId = $this->fetchDependencyId($factory[0]); |
191
|
2 |
|
$factoryMethod = $factory[1]; |
192
|
|
|
} else { |
193
|
1 |
|
throw new InvalidFactoryException('Incorrect factory provided, available string and array factories'); |
194
|
|
|
} |
195
|
7 |
|
if ($this->container->has($factoryId)) { |
196
|
6 |
|
$factoryResolved = $this->container->get($factoryId); |
197
|
6 |
|
return [$factoryResolved, $factoryMethod]; |
198
|
|
|
} |
199
|
1 |
|
throw new InvalidFactoryException('Incorrect factory provided'); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Create object with provided arguments and optional construct method |
204
|
|
|
* |
205
|
|
|
* @param string $className |
206
|
|
|
* @param array $arguments |
207
|
|
|
* @param string|null $constructMethod |
208
|
|
|
* @return mixed |
209
|
|
|
*/ |
210
|
24 |
|
protected function constructObject(string $className, array $arguments, ?string $constructMethod = null) |
211
|
|
|
{ |
212
|
24 |
|
if ($constructMethod !== null) { |
213
|
3 |
|
$obj = $className::$constructMethod(...$arguments); |
214
|
|
|
} else { |
215
|
21 |
|
$obj = new $className(...$arguments); |
216
|
|
|
} |
217
|
24 |
|
return $obj; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Build function attributes for type-value representation |
222
|
|
|
* |
223
|
|
|
* @param array $attributes |
224
|
|
|
* @return Parameter[] |
225
|
|
|
*/ |
226
|
41 |
|
protected function buildParameters($attributes = []): array |
227
|
|
|
{ |
228
|
41 |
|
$parameters = []; |
229
|
41 |
|
foreach ($attributes as $key => $attribute) { |
230
|
18 |
|
$parameters[$key] = $this->buildParameter($attribute); |
231
|
|
|
} |
232
|
41 |
|
return $parameters; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Create parameter from provided argument |
237
|
|
|
* |
238
|
|
|
* @param $value |
239
|
|
|
* @return ParameterInterface |
240
|
|
|
*/ |
241
|
18 |
|
protected function buildParameter($value): ParameterInterface |
242
|
|
|
{ |
243
|
18 |
|
$type = ParameterInterface::TYPE_VALUE; |
244
|
18 |
|
if (\is_string($value) && 0 === strpos($value, '@')) { |
245
|
6 |
|
$type = ParameterInterface::TYPE_REFERENCE_REQUIRED; |
246
|
6 |
|
if (0 === strpos($value, '@?')) { |
247
|
2 |
|
$value = substr($value, 2); |
248
|
2 |
|
$type = ParameterInterface::TYPE_REFERENCE_OPTIONAL; |
249
|
4 |
|
} elseif (0 === strpos($value, '@@')) { |
250
|
1 |
|
$value = substr($value, 1); |
251
|
1 |
|
$type = DependencyInterface::TYPE_VALUE; |
252
|
|
|
} else { |
253
|
3 |
|
$value = substr($value, 1); |
254
|
|
|
} |
255
|
|
|
} |
256
|
18 |
|
return new Parameter($type, $value); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Try fetch dependency name from string value |
261
|
|
|
* |
262
|
|
|
* @param string $value |
263
|
|
|
* @return string|null |
264
|
|
|
*/ |
265
|
7 |
|
protected function fetchDependencyId(string $value): ?string |
266
|
|
|
{ |
267
|
7 |
|
if (0 === strpos($value, '@')) { |
268
|
3 |
|
return substr($value, 1); |
269
|
|
|
} |
270
|
4 |
|
return $value; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Build arguments by dependencies and parameters |
275
|
|
|
* |
276
|
|
|
* @param DependencyInterface[] $dependencies |
277
|
|
|
* @param ParameterInterface[] $parameters |
278
|
|
|
* @return array |
279
|
|
|
* @throws NotFoundException |
280
|
|
|
*/ |
281
|
41 |
|
protected function buildArguments(array $dependencies, array $parameters): array |
282
|
|
|
{ |
283
|
41 |
|
$arguments = []; |
284
|
41 |
|
if (count($dependencies) > 0) { |
285
|
33 |
|
$arguments = $this->buildArgumentsFromDependencies($dependencies, $parameters); |
286
|
|
|
} else { |
287
|
10 |
|
foreach ($parameters as $parameter) { |
288
|
1 |
|
$arguments[] = $this->makeArgumentByParameter($parameter); |
289
|
|
|
} |
290
|
|
|
} |
291
|
38 |
|
return $arguments; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param DependencyInterface[] $dependencies |
296
|
|
|
* @param ParameterInterface[] $parameters |
297
|
|
|
* @return array |
298
|
|
|
* @throws NotFoundException |
299
|
|
|
*/ |
300
|
33 |
|
protected function buildArgumentsFromDependencies(array $dependencies, array $parameters): array |
301
|
|
|
{ |
302
|
33 |
|
$arguments = []; |
303
|
33 |
|
$usedParameters = []; |
304
|
|
|
|
305
|
33 |
|
foreach ($dependencies as $key => $dependency) { |
306
|
|
|
/** @var ParameterInterface $parameter */ |
307
|
33 |
|
$parameter = null; |
|
|
|
|
308
|
33 |
|
if (isset($parameters[$key])) { |
309
|
7 |
|
$parameter = $parameters[$key]; |
310
|
7 |
|
$usedParameters[] = $key; |
311
|
7 |
|
$arguments[] = $this->makeArgumentByParameter($parameter); |
312
|
26 |
|
} elseif (isset($parameters[$dependency->getName()])) { |
313
|
10 |
|
$parameter = $parameters[$dependency->getName()]; |
314
|
10 |
|
$usedParameters[] = $dependency->getName(); |
315
|
10 |
|
$arguments[] = $this->makeArgumentByParameter($parameter); |
316
|
|
|
} else { |
317
|
25 |
|
$arguments[] = $this->makeArgumentByDependency($dependency); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
30 |
|
$arguments = $this->appendUnusedParamsToArguments($parameters, $arguments, $usedParameters); |
322
|
|
|
|
323
|
30 |
|
return $arguments; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @param array $parameters |
328
|
|
|
* @param array $usedParameters |
329
|
|
|
* @param array $arguments |
330
|
|
|
* @return array |
331
|
|
|
* @throws NotFoundException |
332
|
|
|
*/ |
333
|
30 |
|
protected function appendUnusedParamsToArguments( |
334
|
|
|
array $parameters, |
335
|
|
|
array $arguments, |
336
|
|
|
array $usedParameters = [] |
337
|
|
|
): array { |
338
|
30 |
|
foreach ($parameters as $key => $parameter) { |
339
|
16 |
|
if (!in_array($key, $usedParameters, true)) { |
340
|
1 |
|
$arguments[] = $this->makeArgumentByParameter($parameter); |
341
|
|
|
} |
342
|
|
|
} |
343
|
30 |
|
return $arguments; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* @param ParameterInterface $parameter |
348
|
|
|
* @return mixed |
349
|
|
|
* @throws NotFoundException |
350
|
|
|
*/ |
351
|
18 |
|
protected function makeArgumentByParameter(ParameterInterface $parameter) |
352
|
|
|
{ |
353
|
18 |
|
switch ($parameter->getType()) { |
354
|
18 |
|
case ParameterInterface::TYPE_REFERENCE_REQUIRED: |
355
|
3 |
|
return $this->retrieveRequiredDependencyFromContainer($parameter->getValue()); |
356
|
|
|
|
357
|
16 |
|
case ParameterInterface::TYPE_REFERENCE_OPTIONAL: |
358
|
2 |
|
return $this->retrieveOptionalDependencyFromContainer($parameter->getValue()); |
359
|
|
|
} |
360
|
14 |
|
return $parameter->getValue(); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @param DependencyInterface $dependency |
365
|
|
|
* @return mixed |
366
|
|
|
* @throws NotFoundException |
367
|
|
|
*/ |
368
|
25 |
|
protected function makeArgumentByDependency(DependencyInterface $dependency) |
369
|
|
|
{ |
370
|
25 |
|
switch ($dependency->getType()) { |
371
|
25 |
|
case DependencyInterface::TYPE_REQUIRED: |
372
|
20 |
|
return $this->retrieveRequiredDependencyFromContainer($dependency->getValue()); |
373
|
|
|
|
374
|
10 |
|
case DependencyInterface::TYPE_OPTIONAL: |
375
|
2 |
|
return $this->retrieveOptionalDependencyFromContainer($dependency->getValue()); |
376
|
|
|
} |
377
|
8 |
|
return $dependency->getValue(); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* @param $id |
382
|
|
|
* @return mixed |
383
|
|
|
* @throws NotFoundException |
384
|
|
|
*/ |
385
|
23 |
|
protected function retrieveRequiredDependencyFromContainer($id) |
386
|
|
|
{ |
387
|
23 |
|
if ($this->container && $this->container->has($id)) { |
388
|
21 |
|
return $this->container->get($id); |
389
|
|
|
} |
390
|
2 |
|
throw new NotFoundException("There is no referenced classes of {$id} found"); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* @param $id |
395
|
|
|
* @return mixed |
396
|
|
|
*/ |
397
|
4 |
|
protected function retrieveOptionalDependencyFromContainer($id) |
398
|
|
|
{ |
399
|
4 |
|
if ($this->container && $this->container->has($id)) { |
400
|
2 |
|
return $this->container->get($id); |
401
|
|
|
} |
402
|
2 |
|
return null; |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.