1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Definitions\Helpers; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Yiisoft\Definitions\Contract\DefinitionInterface; |
9
|
|
|
use Yiisoft\Definitions\Contract\ReferenceInterface; |
10
|
|
|
use Yiisoft\Definitions\Exception\InvalidConfigException; |
11
|
|
|
use Yiisoft\Definitions\ParameterDefinition; |
12
|
|
|
use Yiisoft\Definitions\ValueDefinition; |
13
|
|
|
|
14
|
|
|
use function is_array; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @internal |
18
|
|
|
*/ |
19
|
|
|
final class DefinitionResolver |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Resolves dependencies by replacing them with the actual object instances. |
23
|
|
|
* |
24
|
|
|
* @param ContainerInterface $container Container to get dependencies from. |
25
|
|
|
* @param ContainerInterface|null $referenceContainer Container to get references from. |
26
|
|
|
* @param array $definitions Definitions to resolve. |
27
|
|
|
* |
28
|
|
|
* @return array The resolved dependencies. |
29
|
|
|
*/ |
30
|
43 |
|
public static function resolveArray( |
31
|
|
|
ContainerInterface $container, |
32
|
|
|
?ContainerInterface $referenceContainer, |
33
|
|
|
array $definitions |
34
|
|
|
): array { |
35
|
43 |
|
$result = []; |
36
|
43 |
|
foreach ($definitions as $key => $definition) { |
37
|
|
|
// Don't resolve variadic parameters |
38
|
39 |
|
if ($definition instanceof ParameterDefinition && $definition->isVariadic()) { |
39
|
19 |
|
continue; |
40
|
|
|
} |
41
|
|
|
|
42
|
39 |
|
$result[$key] = self::resolve($container, $referenceContainer, $definition); |
43
|
|
|
} |
44
|
|
|
|
45
|
43 |
|
return $result; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* This function resolves a definition recursively, checking for loops. |
50
|
|
|
* |
51
|
|
|
* @param mixed $definition Definition to resolve. |
52
|
|
|
*/ |
53
|
41 |
|
public static function resolve( |
54
|
|
|
ContainerInterface $container, |
55
|
|
|
?ContainerInterface $referenceContainer, |
56
|
|
|
mixed $definition |
57
|
|
|
): mixed { |
58
|
41 |
|
if ($definition instanceof DefinitionInterface) { |
59
|
40 |
|
$container = $referenceContainer !== null && $definition instanceof ReferenceInterface |
60
|
1 |
|
? $referenceContainer |
61
|
40 |
|
: $container; |
62
|
40 |
|
$definition = $definition->resolve($container); |
63
|
8 |
|
} elseif (is_array($definition)) { |
64
|
2 |
|
return self::resolveArray($container, $referenceContainer, $definition); |
65
|
|
|
} |
66
|
|
|
|
67
|
41 |
|
return $definition; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @throws InvalidConfigException |
72
|
|
|
*/ |
73
|
33 |
|
public static function ensureResolvable(mixed $value): array|ReferenceInterface|ValueDefinition |
74
|
|
|
{ |
75
|
33 |
|
if ($value instanceof ReferenceInterface || is_array($value)) { |
76
|
12 |
|
return $value; |
77
|
|
|
} |
78
|
|
|
|
79
|
30 |
|
if ($value instanceof DefinitionInterface) { |
80
|
1 |
|
throw new InvalidConfigException( |
81
|
1 |
|
'Only references are allowed in constructor arguments, a definition object was provided: ' . |
82
|
1 |
|
var_export($value, true) |
83
|
1 |
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
29 |
|
return new ValueDefinition($value); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|