1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Di\Helpers; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Definitions\ArrayDefinition; |
8
|
|
|
|
9
|
|
|
use function count; |
10
|
|
|
use function is_array; |
11
|
|
|
use function is_callable; |
12
|
|
|
use function is_string; |
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 Definition to parse. |
55
|
|
|
* |
56
|
|
|
* @return array Definition parsed into an array of a special structure. |
57
|
|
|
* @psalm-return array{mixed,array} |
58
|
|
|
*/ |
59
|
116 |
|
public static function parse(mixed $definition): array |
60
|
|
|
{ |
61
|
116 |
|
if (!is_array($definition)) { |
62
|
85 |
|
return [$definition, []]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Dedicated definition |
66
|
54 |
|
if (isset($definition[self::DEFINITION_META])) { |
67
|
|
|
/** @var mixed $newDefinition */ |
68
|
3 |
|
$newDefinition = $definition[self::DEFINITION_META]; |
69
|
3 |
|
unset($definition[self::DEFINITION_META]); |
70
|
|
|
|
71
|
3 |
|
return [$newDefinition, $definition]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Callable definition |
75
|
51 |
|
if (is_callable($definition, true)) { |
76
|
3 |
|
return [$definition, []]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Array definition |
80
|
48 |
|
$meta = []; |
81
|
48 |
|
$class = null; |
82
|
48 |
|
$constructorArguments = []; |
83
|
48 |
|
$methodsAndProperties = []; |
84
|
|
|
/** @var mixed $value */ |
85
|
48 |
|
foreach ($definition as $key => $value) { |
86
|
48 |
|
if (is_string($key)) { |
87
|
|
|
// Class |
88
|
48 |
|
if ($key === ArrayDefinition::CLASS_NAME) { |
89
|
|
|
/** @var mixed $class */ |
90
|
45 |
|
$class = $value; |
91
|
45 |
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Constructor arguments |
95
|
47 |
|
if ($key === ArrayDefinition::CONSTRUCTOR) { |
96
|
|
|
/** @var mixed $constructorArguments */ |
97
|
13 |
|
$constructorArguments = $value; |
98
|
13 |
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Methods and properties |
102
|
37 |
|
if (count($methodArray = explode('()', $key, 2)) === 2) { |
103
|
19 |
|
$methodsAndProperties[$key] = [ArrayDefinition::TYPE_METHOD, $methodArray[0], $value]; |
104
|
19 |
|
continue; |
105
|
|
|
} |
106
|
27 |
|
if (count($propertyArray = explode('$', $key, 2)) === 2) { |
107
|
5 |
|
$methodsAndProperties[$key] = [ArrayDefinition::TYPE_PROPERTY, $propertyArray[1], $value]; |
108
|
5 |
|
continue; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** @var mixed */ |
113
|
22 |
|
$meta[$key] = $value; |
114
|
|
|
} |
115
|
48 |
|
return [ |
116
|
48 |
|
[ |
117
|
48 |
|
'class' => $class, |
118
|
48 |
|
'__construct()' => $constructorArguments, |
119
|
48 |
|
'methodsAndProperties' => $methodsAndProperties, |
120
|
48 |
|
self::IS_PREPARED_ARRAY_DEFINITION_DATA => true, |
121
|
48 |
|
], |
122
|
48 |
|
$meta, |
123
|
48 |
|
]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|