|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Definitions\Helpers; |
|
6
|
|
|
|
|
7
|
|
|
use ReflectionClass; |
|
8
|
|
|
use ReflectionMethod; |
|
9
|
|
|
use ReflectionProperty; |
|
10
|
|
|
use Yiisoft\Definitions\ArrayDefinition; |
|
11
|
|
|
use Yiisoft\Definitions\Contract\DefinitionInterface; |
|
12
|
|
|
use Yiisoft\Definitions\Contract\ReferenceInterface; |
|
13
|
|
|
use Yiisoft\Definitions\Exception\InvalidConfigException; |
|
14
|
|
|
|
|
15
|
|
|
use function is_array; |
|
16
|
|
|
use function is_callable; |
|
17
|
|
|
use function is_object; |
|
18
|
|
|
use function is_string; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Definition validator checks if definition is valid. |
|
22
|
|
|
*/ |
|
23
|
|
|
final class DefinitionValidator |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Validates that definition is valid. Throws exception otherwise. |
|
27
|
|
|
* |
|
28
|
|
|
* @param mixed $definition Definition to validate. |
|
29
|
22 |
|
* |
|
30
|
|
|
* @throws InvalidConfigException If definition is not valid. |
|
31
|
|
|
*/ |
|
32
|
22 |
|
public static function validate(mixed $definition, ?string $id = null): void |
|
33
|
2 |
|
{ |
|
34
|
|
|
// Reference or ready object |
|
35
|
|
|
if (is_object($definition) && self::isValidObject($definition)) { |
|
36
|
|
|
return; |
|
37
|
20 |
|
} |
|
38
|
1 |
|
|
|
39
|
|
|
// Class |
|
40
|
|
|
if (is_string($definition) && $definition !== '') { |
|
41
|
|
|
return; |
|
42
|
19 |
|
} |
|
43
|
1 |
|
|
|
44
|
|
|
// Callable definition |
|
45
|
|
|
if ($definition !== '' && is_callable($definition, true)) { |
|
46
|
|
|
return; |
|
47
|
18 |
|
} |
|
48
|
16 |
|
|
|
49
|
1 |
|
// Array definition |
|
50
|
|
|
if (is_array($definition)) { |
|
51
|
|
|
self::validateArrayDefinition($definition, $id); |
|
52
|
2 |
|
return; |
|
53
|
|
|
} |
|
54
|
2 |
|
|
|
55
|
|
|
throw new InvalidConfigException( |
|
56
|
|
|
'Invalid definition: ' |
|
57
|
|
|
. ($definition === '' ? 'empty string.' : var_export($definition, true)) |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Validates that array definition is valid. Throws exception otherwise. |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $definition Array definition to validate. |
|
65
|
18 |
|
* |
|
66
|
|
|
* @throws InvalidConfigException If definition is not valid. |
|
67
|
18 |
|
*/ |
|
68
|
16 |
|
public static function validateArrayDefinition(array $definition, ?string $id = null): void |
|
69
|
1 |
|
{ |
|
70
|
1 |
|
/** @var class-string $className */ |
|
71
|
|
|
$className = $definition[ArrayDefinition::CLASS_NAME] ?? $id ?? throw new InvalidConfigException( |
|
72
|
|
|
'Invalid definition: no class name specified.' |
|
73
|
|
|
); |
|
74
|
|
|
self::validateClassName($className); |
|
75
|
|
|
$classReflection = new ReflectionClass($className); |
|
76
|
|
|
$classPublicMethods = []; |
|
77
|
|
|
foreach ($classReflection->getMethods() as $reflectionMethod) { |
|
78
|
16 |
|
if ($reflectionMethod->getModifiers() & ReflectionMethod::IS_PUBLIC) { |
|
79
|
16 |
|
$classPublicMethods[] = $reflectionMethod->getName(); |
|
80
|
1 |
|
} |
|
81
|
1 |
|
} |
|
82
|
|
|
$classPublicProperties = []; |
|
83
|
1 |
|
foreach ($classReflection->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) { |
|
84
|
|
|
if ($reflectionProperty->getModifiers() & ReflectionProperty::IS_PUBLIC) { |
|
85
|
|
|
$classPublicProperties[] = $reflectionProperty->getName(); |
|
86
|
|
|
} |
|
87
|
15 |
|
} |
|
88
|
1 |
|
|
|
89
|
|
|
foreach ($definition as $key => $value) { |
|
90
|
14 |
|
if (!is_string($key)) { |
|
91
|
|
|
throw new InvalidConfigException( |
|
92
|
|
|
sprintf( |
|
93
|
|
|
'Invalid definition: invalid key in array definition. Allow only string keys, got %d.', |
|
94
|
11 |
|
$key, |
|
95
|
9 |
|
), |
|
96
|
1 |
|
); |
|
97
|
1 |
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
// Class |
|
100
|
|
|
if ($key === ArrayDefinition::CLASS_NAME) { |
|
101
|
|
|
continue; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
8 |
|
// Constructor arguments |
|
105
|
8 |
|
if ($key === ArrayDefinition::CONSTRUCTOR) { |
|
106
|
1 |
|
if (!is_array($value)) { |
|
107
|
|
|
throw new InvalidConfigException( |
|
108
|
1 |
|
sprintf( |
|
109
|
|
|
'Invalid definition: incorrect constructor arguments. Expected array, got %s.', |
|
110
|
|
|
get_debug_type($value) |
|
111
|
|
|
) |
|
112
|
7 |
|
); |
|
113
|
|
|
} |
|
114
|
|
|
/** @var mixed $argument */ |
|
115
|
|
|
foreach ($value as $argument) { |
|
116
|
9 |
|
if (is_object($argument) && !self::isValidObject($argument)) { |
|
117
|
|
|
throw new InvalidConfigException( |
|
118
|
|
|
'Only references are allowed in constructor arguments, a definition object was provided: ' . |
|
119
|
|
|
var_export($argument, true) |
|
120
|
9 |
|
); |
|
121
|
1 |
|
} |
|
122
|
1 |
|
} |
|
123
|
|
|
continue; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Methods and properties |
|
127
|
|
|
if (str_ends_with($key, '()')) { |
|
128
|
8 |
|
$parsedKey = mb_substr($key, 0, -2); |
|
129
|
1 |
|
if (!$classReflection->hasMethod($parsedKey)) { |
|
130
|
1 |
|
$possiblePropertiesMessage = $classPublicMethods === [] |
|
131
|
|
|
? 'No public methods available to call.' |
|
132
|
1 |
|
: sprintf( |
|
133
|
|
|
'Possible methods to call: %s', |
|
134
|
|
|
'"' . implode('", "', $classPublicMethods) . '"', |
|
135
|
|
|
); |
|
136
|
7 |
|
throw new InvalidConfigException( |
|
137
|
|
|
sprintf( |
|
138
|
7 |
|
'Invalid definition: class "%s" does not have the public method with name "%s". ' . $possiblePropertiesMessage, |
|
139
|
7 |
|
$className, |
|
140
|
|
|
$parsedKey, |
|
141
|
|
|
) |
|
142
|
7 |
|
); |
|
143
|
|
|
} |
|
144
|
|
|
if (!in_array($parsedKey, $classPublicMethods, true)) { |
|
145
|
4 |
|
throw new InvalidConfigException( |
|
146
|
1 |
|
sprintf( |
|
147
|
|
|
'Invalid definition: method "%s" must be public.' . |
|
148
|
|
|
$className . '::' . $key, |
|
149
|
|
|
) |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
if (!is_array($value)) { |
|
153
|
7 |
|
throw new InvalidConfigException( |
|
154
|
|
|
sprintf( |
|
155
|
7 |
|
'Invalid definition: incorrect method "%s" arguments. Expected array, got "%s". ' . |
|
156
|
|
|
'Probably you should wrap them into square brackets.', |
|
157
|
|
|
$key, |
|
158
|
|
|
get_debug_type($value), |
|
159
|
|
|
) |
|
160
|
7 |
|
); |
|
161
|
2 |
|
} |
|
162
|
2 |
|
continue; |
|
163
|
|
|
} |
|
164
|
|
|
if (str_starts_with($key, '$')) { |
|
165
|
|
|
$parsedKey = mb_substr($key, 1); |
|
166
|
5 |
|
if (!$classReflection->hasProperty($parsedKey)) { |
|
167
|
5 |
|
if ($classPublicProperties === []) { |
|
168
|
|
|
$message = sprintf( |
|
169
|
|
|
'Invalid definition: class "%s" does not have any public properties.', |
|
170
|
|
|
$className, |
|
171
|
|
|
); |
|
172
|
|
|
} else { |
|
173
|
|
|
$message = sprintf( |
|
174
|
|
|
'Invalid definition: class "%s" does not have the public property with name "%s". Possible properties to set: %s.', |
|
175
|
|
|
$className, |
|
176
|
|
|
$parsedKey, |
|
177
|
|
|
'"' . implode('", "', $classPublicProperties) . '"', |
|
178
|
|
|
); |
|
179
|
3 |
|
} |
|
180
|
|
|
throw new InvalidConfigException($message); |
|
181
|
3 |
|
} elseif (!in_array($parsedKey, $classPublicProperties, true)) { |
|
182
|
|
|
throw new InvalidConfigException( |
|
183
|
|
|
sprintf( |
|
184
|
|
|
'Invalid definition: property "%s" must be public.', |
|
185
|
|
|
$className . '::' . $key, |
|
186
|
|
|
) |
|
187
|
|
|
); |
|
188
|
|
|
} |
|
189
|
|
|
continue; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
self::throwInvalidArrayDefinitionKey($key); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @throws InvalidConfigException |
|
198
|
|
|
*/ |
|
199
|
|
|
private static function throwInvalidArrayDefinitionKey(string $key): void |
|
200
|
|
|
{ |
|
201
|
|
|
$preparedKey = trim(strtr($key, [ |
|
202
|
|
|
'()' => '', |
|
203
|
|
|
'$' => '', |
|
204
|
|
|
])); |
|
205
|
|
|
|
|
206
|
|
|
if ($preparedKey === '' || !preg_match('/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/', $preparedKey)) { |
|
207
|
|
|
throw new InvalidConfigException( |
|
208
|
|
|
sprintf('Invalid definition: key "%s" is not allowed.', $key) |
|
209
|
|
|
); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
throw new InvalidConfigException( |
|
213
|
|
|
sprintf( |
|
214
|
|
|
'Invalid definition: key "%s" is not allowed. Did you mean "%s()" or "$%s"?', |
|
215
|
|
|
$key, |
|
216
|
|
|
$preparedKey, |
|
217
|
|
|
$preparedKey |
|
218
|
|
|
) |
|
219
|
|
|
); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Deny `DefinitionInterface`, exclude `ReferenceInterface` |
|
224
|
|
|
*/ |
|
225
|
|
|
private static function isValidObject(object $value): bool |
|
226
|
|
|
{ |
|
227
|
|
|
return !($value instanceof DefinitionInterface) || $value instanceof ReferenceInterface; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
private static function validateClassName(mixed $class): void |
|
231
|
|
|
{ |
|
232
|
|
|
if ($class === '' || !is_string($class)) { |
|
233
|
|
|
throw new InvalidConfigException( |
|
234
|
|
|
sprintf( |
|
235
|
|
|
'Invalid definition: class name must be a non-empty string, got "%s".', |
|
236
|
|
|
get_debug_type($class), |
|
237
|
|
|
) |
|
238
|
|
|
); |
|
239
|
|
|
} |
|
240
|
|
|
if (!class_exists($class)) { |
|
241
|
|
|
throw new InvalidConfigException( |
|
242
|
|
|
sprintf( |
|
243
|
|
|
'Invalid definition: invalid class name "%s". Class must exist.', |
|
244
|
|
|
$class, |
|
245
|
|
|
), |
|
246
|
|
|
); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|