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