1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Di; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Factory\Definition\ArrayDefinition; |
8
|
|
|
use Yiisoft\Factory\Exception\InvalidConfigException; |
9
|
|
|
|
10
|
|
|
use function count; |
11
|
|
|
use function is_array; |
12
|
|
|
use function is_callable; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @internal Splits metadata and definition. |
16
|
|
|
* |
17
|
|
|
* Supports the following configuration: |
18
|
|
|
* |
19
|
|
|
* 1) With a dedicated definition: |
20
|
|
|
* |
21
|
|
|
* ```php |
22
|
|
|
* Engine::class => [ |
23
|
|
|
* 'definition' => [ |
24
|
|
|
* '__class' => BigEngine::class, |
25
|
|
|
* 'setNumber()' => [42], |
26
|
|
|
* ], |
27
|
|
|
* 'tags' => ['a', 'b'], |
28
|
|
|
* 'reset' => function () { |
29
|
|
|
* $this->number = 42; |
30
|
|
|
* }, |
31
|
|
|
* ] |
32
|
|
|
* ``` |
33
|
|
|
* |
34
|
|
|
* 2) Mixed in array definition: |
35
|
|
|
* |
36
|
|
|
* ```php |
37
|
|
|
* Engine::class => [ |
38
|
|
|
* '__class' => BigEngine::class, |
39
|
|
|
* 'setNumber()' => [42], |
40
|
|
|
* 'tags' => ['a', 'b'], |
41
|
|
|
* 'reset' => function () { |
42
|
|
|
* $this->number = 42; |
43
|
|
|
* }, |
44
|
|
|
* ] |
45
|
|
|
* ``` |
46
|
|
|
*/ |
47
|
|
|
final class DefinitionParser |
48
|
|
|
{ |
49
|
|
|
private const DEFINITION_META = 'definition'; |
50
|
|
|
|
51
|
|
|
public const IS_PREPARED_ARRAY_DEFINITION_DATA = 'isPreparedArrayDefinitionData'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param mixed $definition |
55
|
|
|
* |
56
|
|
|
* @throws InvalidConfigException |
57
|
|
|
*/ |
58
|
102 |
|
public static function parse($definition): array |
59
|
|
|
{ |
60
|
102 |
|
if (!is_array($definition)) { |
61
|
99 |
|
return [$definition, []]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Dedicated definition |
65
|
44 |
|
if (isset($definition[self::DEFINITION_META])) { |
66
|
3 |
|
$newDefinition = $definition[self::DEFINITION_META]; |
67
|
3 |
|
unset($definition[self::DEFINITION_META]); |
68
|
|
|
|
69
|
3 |
|
return [$newDefinition, $definition]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Callable definition |
73
|
41 |
|
if (is_callable($definition, true)) { |
74
|
3 |
|
return [$definition, []]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Array definition |
78
|
38 |
|
$meta = []; |
79
|
38 |
|
$class = null; |
80
|
38 |
|
$constructorArguments = []; |
81
|
38 |
|
$methodsAndProperties = []; |
82
|
38 |
|
foreach ($definition as $key => $value) { |
83
|
|
|
// Class |
84
|
38 |
|
if ($key === ArrayDefinition::CLASS_NAME) { |
85
|
37 |
|
$class = $value; |
86
|
37 |
|
continue; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Constructor arguments |
90
|
35 |
|
if ($key === ArrayDefinition::CONSTRUCTOR) { |
91
|
12 |
|
$constructorArguments = $value; |
92
|
12 |
|
continue; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Methods and properties |
96
|
26 |
|
if (count($methodArray = explode('()', $key)) === 2) { |
97
|
10 |
|
$methodsAndProperties[$key] = [ArrayDefinition::TYPE_METHOD, $methodArray[0], $value]; |
98
|
10 |
|
continue; |
99
|
|
|
} |
100
|
21 |
|
if (count($propertyArray = explode('$', $key)) === 2) { |
101
|
5 |
|
$methodsAndProperties[$key] = [ArrayDefinition::TYPE_PROPERTY, $propertyArray[1], $value]; |
102
|
5 |
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
16 |
|
$meta[$key] = $value; |
106
|
|
|
} |
107
|
|
|
return [ |
108
|
|
|
[ |
109
|
38 |
|
$class, |
110
|
38 |
|
$constructorArguments, |
111
|
38 |
|
$methodsAndProperties, |
112
|
38 |
|
self::IS_PREPARED_ARRAY_DEFINITION_DATA => true, |
113
|
|
|
], |
114
|
38 |
|
$meta, |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|