1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Factory\Definition; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Factory\Exception\InvalidConfigException; |
8
|
|
|
|
9
|
|
|
use function array_key_exists; |
10
|
|
|
use function is_array; |
11
|
|
|
use function is_callable; |
12
|
|
|
use function is_object; |
13
|
|
|
use function is_string; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Definition represents a definition in a container |
17
|
|
|
*/ |
18
|
|
|
class Normalizer |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Definition may be defined multiple ways. |
22
|
|
|
* Interface name as string: |
23
|
|
|
* |
24
|
|
|
* ```php |
25
|
|
|
* $container->set('interface_name', EngineInterface::class); |
26
|
|
|
* ``` |
27
|
|
|
* |
28
|
|
|
* A closure: |
29
|
|
|
* |
30
|
|
|
* ```php |
31
|
|
|
* $container->set('closure', function($container) { |
32
|
|
|
* return new MyClass($container->get('db')); |
33
|
|
|
* }); |
34
|
|
|
* ``` |
35
|
|
|
* |
36
|
|
|
* A callable array: |
37
|
|
|
* |
38
|
|
|
* ```php |
39
|
|
|
* $container->set('static_call', [MyClass::class, 'create']); |
40
|
|
|
* ``` |
41
|
|
|
* |
42
|
|
|
* A definition array: |
43
|
|
|
* |
44
|
|
|
* ```php |
45
|
|
|
* $container->set('full_definition', [ |
46
|
|
|
* 'class' => EngineMarkOne::class, |
47
|
|
|
* '__construct()' => [42], |
48
|
|
|
* '@argName' => 'value', |
49
|
|
|
* 'setX()' => [42], |
50
|
|
|
* ]); |
51
|
|
|
* ``` |
52
|
|
|
* |
53
|
|
|
* @param mixed $definition |
54
|
|
|
* |
55
|
|
|
* @throws InvalidConfigException |
56
|
|
|
*/ |
57
|
|
|
public static function normalize($definition, string $id = null, array $constructorArguments = [], array $allowedMeta = []): DefinitionInterface |
58
|
|
|
{ |
59
|
|
|
if ($definition instanceof DefinitionInterface) { |
60
|
|
|
return $definition; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (is_string($definition)) { |
64
|
|
|
if (empty($definition)) { |
65
|
|
|
throw new InvalidConfigException('Invalid definition: empty string.'); |
66
|
|
|
} |
67
|
|
|
if ($id === $definition || (!empty($constructorArguments) && class_exists($definition))) { |
68
|
|
|
/** @psalm-var class-string $definition */ |
69
|
|
|
return new ArrayDefinition([ |
70
|
|
|
ArrayDefinition::CLASS_NAME => $definition, |
71
|
|
|
ArrayDefinition::CONSTRUCTOR => $constructorArguments, |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
return Reference::to($definition); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (is_callable($definition, true)) { |
78
|
|
|
return new CallableDefinition($definition); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (is_array($definition)) { |
82
|
|
|
$config = $definition; |
83
|
|
|
if (!array_key_exists(ArrayDefinition::CLASS_NAME, $config)) { |
84
|
|
|
$config[ArrayDefinition::CLASS_NAME] = $id; |
85
|
|
|
} |
86
|
|
|
/** @psalm-suppress ArgumentTypeCoercion */ |
87
|
|
|
return new ArrayDefinition($config, $allowedMeta); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (is_object($definition)) { |
91
|
|
|
return new ValueDefinition($definition); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
throw new InvalidConfigException('Invalid definition:' . var_export($definition, true)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Validates definition for correctness. |
99
|
|
|
* |
100
|
|
|
* @param mixed $definition {@see normalize()} |
101
|
|
|
* |
102
|
|
|
* @throws InvalidConfigException |
103
|
|
|
*/ |
104
|
|
|
public static function validate($definition, bool $throw = true): bool |
105
|
|
|
{ |
106
|
|
|
if ($definition instanceof DefinitionInterface) { |
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (is_string($definition) && !empty($definition)) { |
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (is_callable($definition)) { |
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (is_array($definition)) { |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (is_object($definition)) { |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($throw) { |
127
|
|
|
throw new InvalidConfigException('Invalid definition:' . var_export($definition, true)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Validates definition for correctness. |
135
|
|
|
* |
136
|
|
|
* @param mixed $definition |
137
|
|
|
* @param array $allowedMeta |
138
|
|
|
* |
139
|
|
|
* @throws InvalidConfigException |
140
|
|
|
*/ |
141
|
|
|
public static function parse($definition, array $allowedMeta): array |
142
|
|
|
{ |
143
|
|
|
if (!is_array($definition)) { |
144
|
|
|
return [$definition, []]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$meta = []; |
148
|
|
|
if (isset($definition[self::DEFINITION_META])) { |
|
|
|
|
149
|
|
|
$newDefinition = $definition[self::DEFINITION_META]; |
150
|
|
|
unset($definition[self::DEFINITION_META]); |
151
|
|
|
$meta = array_filter($definition, function ($key) use ($allowedMeta) { |
152
|
|
|
return in_array($key, $allowedMeta); |
153
|
|
|
}, ARRAY_FILTER_USE_KEY); |
154
|
|
|
$definition = $newDefinition; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
foreach ($definition as $key => $value) { |
158
|
|
|
// Method. |
159
|
|
|
if ($key === ArrayDefinition::CLASS_NAME || $key === ArrayDefinition::CONSTRUCTOR) { |
160
|
|
|
continue; |
161
|
|
|
} elseif (substr($key, -2) === '()') { |
162
|
|
|
if (!is_array($value)) { |
163
|
|
|
throw new InvalidConfigException( |
164
|
|
|
sprintf('Invalid definition: incorrect method arguments. Expected array, got %s.', self::getType($value)) |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
// Not property = meta. |
168
|
|
|
} elseif (substr($key, 0, 1) !== '@') { |
169
|
|
|
|
170
|
|
|
if (!in_array($key, $allowedMeta, true)) { |
171
|
|
|
throw new InvalidConfigException(sprintf('Invalid definition: metadata "%s" is not allowed. Did you mean "%s()" or "@%s"?', $key, $key, $key)); |
172
|
|
|
} |
173
|
|
|
$meta[$key] = $value; |
174
|
|
|
unset($definition[$key]); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return [$definition, $meta]; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param mixed $value |
183
|
|
|
*/ |
184
|
|
|
private static function getType($value): string |
185
|
|
|
{ |
186
|
|
|
return is_object($value) ? get_class($value) : gettype($value); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|