1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Symfony package. |
5
|
|
|
* |
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Symfony\Component\DependencyInjection\Loader; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
15
|
|
|
use Symfony\Component\DependencyInjection\Argument\AbstractArgument; |
16
|
|
|
use Symfony\Component\DependencyInjection\Argument\BoundArgument; |
17
|
|
|
use Symfony\Component\DependencyInjection\Argument\IteratorArgument; |
18
|
|
|
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; |
19
|
|
|
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; |
20
|
|
|
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; |
21
|
|
|
use Symfony\Component\DependencyInjection\ChildDefinition; |
22
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
23
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
24
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
25
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
26
|
|
|
use Symfony\Component\DependencyInjection\Exception\LogicException; |
27
|
|
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException; |
28
|
|
|
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; |
29
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
30
|
|
|
use Symfony\Component\ExpressionLanguage\Expression; |
31
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
32
|
|
|
use Symfony\Component\Yaml\Parser as YamlParser; |
33
|
|
|
use Symfony\Component\Yaml\Tag\TaggedValue; |
34
|
|
|
use Symfony\Component\Yaml\Yaml; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* YamlFileLoader loads YAML files service definitions. |
38
|
|
|
* |
39
|
|
|
* @author Fabien Potencier <[email protected]> |
40
|
|
|
*/ |
41
|
|
|
class YamlFileLoader extends FileLoader |
42
|
|
|
{ |
43
|
|
|
private const SERVICE_KEYWORDS = [ |
44
|
|
|
'alias' => 'alias', |
45
|
|
|
'parent' => 'parent', |
46
|
|
|
'class' => 'class', |
47
|
|
|
'shared' => 'shared', |
48
|
|
|
'synthetic' => 'synthetic', |
49
|
|
|
'lazy' => 'lazy', |
50
|
|
|
'public' => 'public', |
51
|
|
|
'abstract' => 'abstract', |
52
|
|
|
'deprecated' => 'deprecated', |
53
|
|
|
'factory' => 'factory', |
54
|
|
|
'file' => 'file', |
55
|
|
|
'arguments' => 'arguments', |
56
|
|
|
'properties' => 'properties', |
57
|
|
|
'configurator' => 'configurator', |
58
|
|
|
'calls' => 'calls', |
59
|
|
|
'tags' => 'tags', |
60
|
|
|
'decorates' => 'decorates', |
61
|
|
|
'decoration_inner_name' => 'decoration_inner_name', |
62
|
|
|
'decoration_priority' => 'decoration_priority', |
63
|
|
|
'decoration_on_invalid' => 'decoration_on_invalid', |
64
|
|
|
'autowire' => 'autowire', |
65
|
|
|
'autoconfigure' => 'autoconfigure', |
66
|
|
|
'bind' => 'bind', |
67
|
|
|
'constructor' => 'constructor', |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
private const PROTOTYPE_KEYWORDS = [ |
71
|
|
|
'resource' => 'resource', |
72
|
|
|
'namespace' => 'namespace', |
73
|
|
|
'exclude' => 'exclude', |
74
|
|
|
'parent' => 'parent', |
75
|
|
|
'shared' => 'shared', |
76
|
|
|
'lazy' => 'lazy', |
77
|
|
|
'public' => 'public', |
78
|
|
|
'abstract' => 'abstract', |
79
|
|
|
'deprecated' => 'deprecated', |
80
|
|
|
'factory' => 'factory', |
81
|
|
|
'arguments' => 'arguments', |
82
|
|
|
'properties' => 'properties', |
83
|
|
|
'configurator' => 'configurator', |
84
|
|
|
'calls' => 'calls', |
85
|
|
|
'tags' => 'tags', |
86
|
|
|
'autowire' => 'autowire', |
87
|
|
|
'autoconfigure' => 'autoconfigure', |
88
|
|
|
'bind' => 'bind', |
89
|
|
|
'constructor' => 'constructor', |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
private const INSTANCEOF_KEYWORDS = [ |
93
|
|
|
'shared' => 'shared', |
94
|
|
|
'lazy' => 'lazy', |
95
|
|
|
'public' => 'public', |
96
|
|
|
'properties' => 'properties', |
97
|
|
|
'configurator' => 'configurator', |
98
|
|
|
'calls' => 'calls', |
99
|
|
|
'tags' => 'tags', |
100
|
|
|
'autowire' => 'autowire', |
101
|
|
|
'bind' => 'bind', |
102
|
|
|
'constructor' => 'constructor', |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
private const DEFAULTS_KEYWORDS = [ |
106
|
|
|
'public' => 'public', |
107
|
|
|
'tags' => 'tags', |
108
|
|
|
'autowire' => 'autowire', |
109
|
|
|
'autoconfigure' => 'autoconfigure', |
110
|
|
|
'bind' => 'bind', |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
protected bool $autoRegisterAliasesForSinglyImplementedInterfaces = false; |
114
|
|
|
|
115
|
|
|
private YamlParser $yamlParser; |
116
|
|
|
private int $anonymousServicesCount; |
117
|
|
|
private string $anonymousServicesSuffix; |
118
|
|
|
|
119
|
|
|
public function load(mixed $resource, ?string $type = null): mixed |
120
|
|
|
{ |
121
|
|
|
$path = $this->locator->locate($resource); |
122
|
|
|
|
123
|
|
|
$content = $this->loadFile($path); |
|
|
|
|
124
|
|
|
|
125
|
|
|
$this->container->fileExists($path); |
126
|
|
|
|
127
|
|
|
// empty file |
128
|
|
|
if (null === $content) { |
|
|
|
|
129
|
|
|
return null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
++$this->importing; |
133
|
|
|
try { |
134
|
|
|
$this->loadContent($content, $path); |
135
|
|
|
|
136
|
|
|
// per-env configuration |
137
|
|
|
if ($this->env && isset($content['when@'.$this->env])) { |
138
|
|
|
if (!\is_array($content['when@'.$this->env])) { |
139
|
|
|
throw new InvalidArgumentException(\sprintf('The "when@%s" key should contain an array in "%s". Check your YAML syntax.', $this->env, $path)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$env = $this->env; |
143
|
|
|
$this->env = null; |
144
|
|
|
try { |
145
|
|
|
$this->loadContent($content['when@'.$env], $path); |
146
|
|
|
} finally { |
147
|
|
|
$this->env = $env; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} finally { |
151
|
|
|
--$this->importing; |
152
|
|
|
} |
153
|
|
|
$this->loadExtensionConfigs(); |
154
|
|
|
|
155
|
|
|
return null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function loadContent(array $content, string $path): void |
159
|
|
|
{ |
160
|
|
|
// imports |
161
|
|
|
$this->parseImports($content, $path); |
162
|
|
|
|
163
|
|
|
// parameters |
164
|
|
|
if (isset($content['parameters'])) { |
165
|
|
|
if (!\is_array($content['parameters'])) { |
166
|
|
|
throw new InvalidArgumentException(\sprintf('The "parameters" key should contain an array in "%s". Check your YAML syntax.', $path)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
foreach ($content['parameters'] as $key => $value) { |
170
|
|
|
$this->container->setParameter($key, $this->resolveServices($value, $path, true)); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// extensions |
175
|
|
|
$this->loadFromExtensions($content); |
176
|
|
|
|
177
|
|
|
// services |
178
|
|
|
$this->anonymousServicesCount = 0; |
179
|
|
|
$this->anonymousServicesSuffix = '~'.ContainerBuilder::hash($path); |
180
|
|
|
$this->setCurrentDir(\dirname($path)); |
181
|
|
|
try { |
182
|
|
|
$this->parseDefinitions($content, $path); |
183
|
|
|
} finally { |
184
|
|
|
$this->instanceof = []; |
185
|
|
|
$this->registerAliasesForSinglyImplementedInterfaces(); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function supports(mixed $resource, ?string $type = null): bool |
190
|
|
|
{ |
191
|
|
|
if (!\is_string($resource)) { |
192
|
|
|
return false; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if (null === $type && \in_array(pathinfo($resource, \PATHINFO_EXTENSION), ['yaml', 'yml'], true)) { |
196
|
|
|
return true; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return \in_array($type, ['yaml', 'yml'], true); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
private function parseImports(array $content, string $file): void |
203
|
|
|
{ |
204
|
|
|
if (!isset($content['imports'])) { |
205
|
|
|
return; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if (!\is_array($content['imports'])) { |
209
|
|
|
throw new InvalidArgumentException(\sprintf('The "imports" key should contain an array in "%s". Check your YAML syntax.', $file)); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$defaultDirectory = \dirname($file); |
213
|
|
|
foreach ($content['imports'] as $import) { |
214
|
|
|
if (!\is_array($import)) { |
215
|
|
|
$import = ['resource' => $import]; |
216
|
|
|
} |
217
|
|
|
if (!isset($import['resource'])) { |
218
|
|
|
throw new InvalidArgumentException(\sprintf('An import should provide a resource in "%s". Check your YAML syntax.', $file)); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$this->setCurrentDir($defaultDirectory); |
222
|
|
|
$this->import($import['resource'], $import['type'] ?? null, $import['ignore_errors'] ?? false, $file); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
private function parseDefinitions(array $content, string $file, bool $trackBindings = true): void |
227
|
|
|
{ |
228
|
|
|
if (!isset($content['services'])) { |
229
|
|
|
return; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if (!\is_array($content['services'])) { |
233
|
|
|
throw new InvalidArgumentException(\sprintf('The "services" key should contain an array in "%s". Check your YAML syntax.', $file)); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if (\array_key_exists('_instanceof', $content['services'])) { |
237
|
|
|
$instanceof = $content['services']['_instanceof']; |
238
|
|
|
unset($content['services']['_instanceof']); |
239
|
|
|
|
240
|
|
|
if (!\is_array($instanceof)) { |
241
|
|
|
throw new InvalidArgumentException(\sprintf('Service "_instanceof" key must be an array, "%s" given in "%s".', get_debug_type($instanceof), $file)); |
242
|
|
|
} |
243
|
|
|
$this->instanceof = []; |
244
|
|
|
$this->isLoadingInstanceof = true; |
245
|
|
|
foreach ($instanceof as $id => $service) { |
246
|
|
|
if (!$service || !\is_array($service)) { |
247
|
|
|
throw new InvalidArgumentException(\sprintf('Type definition "%s" must be a non-empty array within "_instanceof" in "%s". Check your YAML syntax.', $id, $file)); |
248
|
|
|
} |
249
|
|
|
if (\is_string($service) && str_starts_with($service, '@')) { |
250
|
|
|
throw new InvalidArgumentException(\sprintf('Type definition "%s" cannot be an alias within "_instanceof" in "%s". Check your YAML syntax.', $id, $file)); |
251
|
|
|
} |
252
|
|
|
$this->parseDefinition($id, $service, $file, [], false, $trackBindings); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
$this->isLoadingInstanceof = false; |
257
|
|
|
$defaults = $this->parseDefaults($content, $file); |
258
|
|
|
foreach ($content['services'] as $id => $service) { |
259
|
|
|
$this->parseDefinition($id, $service, $file, $defaults, false, $trackBindings); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @throws InvalidArgumentException |
265
|
|
|
*/ |
266
|
|
|
private function parseDefaults(array &$content, string $file): array |
267
|
|
|
{ |
268
|
|
|
if (!\array_key_exists('_defaults', $content['services'])) { |
269
|
|
|
return []; |
270
|
|
|
} |
271
|
|
|
$defaults = $content['services']['_defaults']; |
272
|
|
|
unset($content['services']['_defaults']); |
273
|
|
|
|
274
|
|
|
if (!\is_array($defaults)) { |
275
|
|
|
throw new InvalidArgumentException(\sprintf('Service "_defaults" key must be an array, "%s" given in "%s".', get_debug_type($defaults), $file)); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
foreach ($defaults as $key => $default) { |
279
|
|
|
if (!isset(self::DEFAULTS_KEYWORDS[$key])) { |
280
|
|
|
throw new InvalidArgumentException(\sprintf('The configuration key "%s" cannot be used to define a default value in "%s". Allowed keys are "%s".', $key, $file, implode('", "', self::DEFAULTS_KEYWORDS))); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
if (isset($defaults['tags'])) { |
285
|
|
|
if (!\is_array($tags = $defaults['tags'])) { |
286
|
|
|
throw new InvalidArgumentException(\sprintf('Parameter "tags" in "_defaults" must be an array in "%s". Check your YAML syntax.', $file)); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
foreach ($tags as $tag) { |
290
|
|
|
if (!\is_array($tag)) { |
291
|
|
|
$tag = ['name' => $tag]; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
if (1 === \count($tag) && \is_array(current($tag))) { |
295
|
|
|
$name = key($tag); |
296
|
|
|
$tag = current($tag); |
297
|
|
|
} else { |
298
|
|
|
if (!isset($tag['name'])) { |
299
|
|
|
throw new InvalidArgumentException(\sprintf('A "tags" entry in "_defaults" is missing a "name" key in "%s".', $file)); |
300
|
|
|
} |
301
|
|
|
$name = $tag['name']; |
302
|
|
|
unset($tag['name']); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
if (!\is_string($name) || '' === $name) { |
306
|
|
|
throw new InvalidArgumentException(\sprintf('The tag name in "_defaults" must be a non-empty string in "%s".', $file)); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
$this->validateAttributes(\sprintf('Tag "%s", attribute "%s" in "_defaults" must be of a scalar-type in "%s". Check your YAML syntax.', $name, '%s', $file), $tag); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
if (isset($defaults['bind'])) { |
314
|
|
|
if (!\is_array($defaults['bind'])) { |
315
|
|
|
throw new InvalidArgumentException(\sprintf('Parameter "bind" in "_defaults" must be an array in "%s". Check your YAML syntax.', $file)); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
foreach ($this->resolveServices($defaults['bind'], $file) as $argument => $value) { |
319
|
|
|
$defaults['bind'][$argument] = new BoundArgument($value, true, BoundArgument::DEFAULTS_BINDING, $file); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
return $defaults; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
private function isUsingShortSyntax(array $service): bool |
327
|
|
|
{ |
328
|
|
|
foreach ($service as $key => $value) { |
329
|
|
|
if (\is_string($key) && ('' === $key || ('$' !== $key[0] && !str_contains($key, '\\')))) { |
330
|
|
|
return false; |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
return true; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @throws InvalidArgumentException When tags are invalid |
339
|
|
|
*/ |
340
|
|
|
private function parseDefinition(string $id, array|string|null $service, string $file, array $defaults, bool $return = false, bool $trackBindings = true): Definition|Alias|null |
341
|
|
|
{ |
342
|
|
|
if (preg_match('/^_[a-zA-Z0-9_]*$/', $id)) { |
343
|
|
|
throw new InvalidArgumentException(\sprintf('Service names that start with an underscore are reserved. Rename the "%s" service or define it in XML instead.', $id)); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
if (\is_string($service) && str_starts_with($service, '@')) { |
|
|
|
|
347
|
|
|
$alias = new Alias(substr($service, 1)); |
348
|
|
|
|
349
|
|
|
if (isset($defaults['public'])) { |
350
|
|
|
$alias->setPublic($defaults['public']); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
return $return ? $alias : $this->container->setAlias($id, $alias); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
if (\is_array($service) && $this->isUsingShortSyntax($service)) { |
357
|
|
|
$service = ['arguments' => $service]; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
if (!\is_array($service ??= [])) { |
361
|
|
|
throw new InvalidArgumentException(\sprintf('A service definition must be an array or a string starting with "@" but "%s" found for service "%s" in "%s". Check your YAML syntax.', get_debug_type($service), $id, $file)); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
if (isset($service['stack'])) { |
365
|
|
|
if (!\is_array($service['stack'])) { |
366
|
|
|
throw new InvalidArgumentException(\sprintf('A stack must be an array of definitions, "%s" given for service "%s" in "%s". Check your YAML syntax.', get_debug_type($service), $id, $file)); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
$stack = []; |
370
|
|
|
|
371
|
|
|
foreach ($service['stack'] as $k => $frame) { |
372
|
|
|
if (\is_array($frame) && 1 === \count($frame) && !isset(self::SERVICE_KEYWORDS[key($frame)])) { |
373
|
|
|
$frame = [ |
374
|
|
|
'class' => key($frame), |
375
|
|
|
'arguments' => current($frame), |
376
|
|
|
]; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
if (\is_array($frame) && isset($frame['stack'])) { |
380
|
|
|
throw new InvalidArgumentException(\sprintf('Service stack "%s" cannot contain another stack in "%s".', $id, $file)); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
$definition = $this->parseDefinition($id.'" at index "'.$k, $frame, $file, $defaults, true); |
384
|
|
|
|
385
|
|
|
if ($definition instanceof Definition) { |
386
|
|
|
$definition->setInstanceofConditionals($this->instanceof); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
$stack[$k] = $definition; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
if ($diff = array_diff(array_keys($service), ['stack', 'public', 'deprecated'])) { |
393
|
|
|
throw new InvalidArgumentException(\sprintf('Invalid attribute "%s"; supported ones are "public" and "deprecated" for service "%s" in "%s". Check your YAML syntax.', implode('", "', $diff), $id, $file)); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
$service = [ |
397
|
|
|
'parent' => '', |
398
|
|
|
'arguments' => $stack, |
399
|
|
|
'tags' => ['container.stack'], |
400
|
|
|
'public' => $service['public'] ?? null, |
401
|
|
|
'deprecated' => $service['deprecated'] ?? null, |
402
|
|
|
]; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
$definition = isset($service[0]) && $service[0] instanceof Definition ? array_shift($service) : null; |
406
|
|
|
$return = null === $definition ? $return : true; |
407
|
|
|
|
408
|
|
|
if (isset($service['from_callable'])) { |
409
|
|
|
foreach (['alias', 'parent', 'synthetic', 'factory', 'file', 'arguments', 'properties', 'configurator', 'calls'] as $key) { |
410
|
|
|
if (isset($service['factory'])) { |
411
|
|
|
throw new InvalidArgumentException(\sprintf('The configuration key "%s" is unsupported for the service "%s" when using "from_callable" in "%s".', $key, $id, $file)); |
412
|
|
|
} |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
if ('Closure' !== $service['class'] ??= 'Closure') { |
416
|
|
|
$service['lazy'] = true; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
$service['factory'] = ['Closure', 'fromCallable']; |
420
|
|
|
$service['arguments'] = [$service['from_callable']]; |
421
|
|
|
unset($service['from_callable']); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
$this->checkDefinition($id, $service, $file); |
425
|
|
|
|
426
|
|
|
if (isset($service['alias'])) { |
427
|
|
|
$alias = new Alias($service['alias']); |
428
|
|
|
|
429
|
|
|
if (isset($service['public'])) { |
430
|
|
|
$alias->setPublic($service['public']); |
431
|
|
|
} elseif (isset($defaults['public'])) { |
432
|
|
|
$alias->setPublic($defaults['public']); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
foreach ($service as $key => $value) { |
436
|
|
|
if (!\in_array($key, ['alias', 'public', 'deprecated'])) { |
437
|
|
|
throw new InvalidArgumentException(\sprintf('The configuration key "%s" is unsupported for the service "%s" which is defined as an alias in "%s". Allowed configuration keys for service aliases are "alias", "public" and "deprecated".', $key, $id, $file)); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
if ('deprecated' === $key) { |
441
|
|
|
$deprecation = \is_array($value) ? $value : ['message' => $value]; |
442
|
|
|
|
443
|
|
|
if (!isset($deprecation['package'])) { |
444
|
|
|
throw new InvalidArgumentException(\sprintf('Missing attribute "package" of the "deprecated" option in "%s".', $file)); |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
if (!isset($deprecation['version'])) { |
448
|
|
|
throw new InvalidArgumentException(\sprintf('Missing attribute "version" of the "deprecated" option in "%s".', $file)); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
$alias->setDeprecated($deprecation['package'], $deprecation['version'], $deprecation['message'] ?? ''); |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
return $return ? $alias : $this->container->setAlias($id, $alias); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
$changes = []; |
459
|
|
|
if (null !== $definition) { |
460
|
|
|
$changes = $definition->getChanges(); |
461
|
|
|
} elseif ($this->isLoadingInstanceof) { |
462
|
|
|
$definition = new ChildDefinition(''); |
463
|
|
|
} elseif (isset($service['parent'])) { |
464
|
|
|
if ('' !== $service['parent'] && '@' === $service['parent'][0]) { |
465
|
|
|
throw new InvalidArgumentException(\sprintf('The value of the "parent" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $id, $service['parent'], substr($service['parent'], 1))); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
$definition = new ChildDefinition($service['parent']); |
469
|
|
|
} else { |
470
|
|
|
$definition = new Definition(); |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
if (isset($defaults['public'])) { |
474
|
|
|
$definition->setPublic($defaults['public']); |
475
|
|
|
} |
476
|
|
|
if (isset($defaults['autowire'])) { |
477
|
|
|
$definition->setAutowired($defaults['autowire']); |
478
|
|
|
} |
479
|
|
|
if (isset($defaults['autoconfigure'])) { |
480
|
|
|
$definition->setAutoconfigured($defaults['autoconfigure']); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
$definition->setChanges($changes); |
484
|
|
|
|
485
|
|
|
if (isset($service['class'])) { |
486
|
|
|
$definition->setClass($service['class']); |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
if (isset($service['shared'])) { |
490
|
|
|
$definition->setShared($service['shared']); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
if (isset($service['synthetic'])) { |
494
|
|
|
$definition->setSynthetic($service['synthetic']); |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
if (isset($service['lazy'])) { |
498
|
|
|
$definition->setLazy((bool) $service['lazy']); |
499
|
|
|
if (\is_string($service['lazy'])) { |
500
|
|
|
$definition->addTag('proxy', ['interface' => $service['lazy']]); |
501
|
|
|
} |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
if (isset($service['public'])) { |
505
|
|
|
$definition->setPublic($service['public']); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
if (isset($service['abstract'])) { |
509
|
|
|
$definition->setAbstract($service['abstract']); |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
if (isset($service['deprecated'])) { |
513
|
|
|
$deprecation = \is_array($service['deprecated']) ? $service['deprecated'] : ['message' => $service['deprecated']]; |
514
|
|
|
|
515
|
|
|
if (!isset($deprecation['package'])) { |
516
|
|
|
throw new InvalidArgumentException(\sprintf('Missing attribute "package" of the "deprecated" option in "%s".', $file)); |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
if (!isset($deprecation['version'])) { |
520
|
|
|
throw new InvalidArgumentException(\sprintf('Missing attribute "version" of the "deprecated" option in "%s".', $file)); |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
$definition->setDeprecated($deprecation['package'], $deprecation['version'], $deprecation['message'] ?? ''); |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
if (isset($service['factory'])) { |
527
|
|
|
$definition->setFactory($this->parseCallable($service['factory'], 'factory', $id, $file)); |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
if (isset($service['constructor'])) { |
531
|
|
|
if (null !== $definition->getFactory()) { |
532
|
|
|
throw new LogicException(\sprintf('The "%s" service cannot declare a factory as well as a constructor.', $id)); |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
$definition->setFactory([null, $service['constructor']]); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
if (isset($service['file'])) { |
539
|
|
|
$definition->setFile($service['file']); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
if (isset($service['arguments'])) { |
543
|
|
|
$definition->setArguments($this->resolveServices($service['arguments'], $file)); |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
if (isset($service['properties'])) { |
547
|
|
|
$definition->setProperties($this->resolveServices($service['properties'], $file)); |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
if (isset($service['configurator'])) { |
551
|
|
|
$definition->setConfigurator($this->parseCallable($service['configurator'], 'configurator', $id, $file)); |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
if (isset($service['calls'])) { |
555
|
|
|
if (!\is_array($service['calls'])) { |
556
|
|
|
throw new InvalidArgumentException(\sprintf('Parameter "calls" must be an array for service "%s" in "%s". Check your YAML syntax.', $id, $file)); |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
foreach ($service['calls'] as $k => $call) { |
560
|
|
|
if (!\is_array($call) && (!\is_string($k) || !$call instanceof TaggedValue)) { |
561
|
|
|
throw new InvalidArgumentException(\sprintf('Invalid method call for service "%s": expected map or array, "%s" given in "%s".', $id, $call instanceof TaggedValue ? '!'.$call->getTag() : get_debug_type($call), $file)); |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
if (\is_string($k)) { |
565
|
|
|
throw new InvalidArgumentException(\sprintf('Invalid method call for service "%s", did you forget a leading dash before "%s: ..." in "%s"?', $id, $k, $file)); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
if (isset($call['method']) && \is_string($call['method'])) { |
569
|
|
|
$method = $call['method']; |
570
|
|
|
$args = $call['arguments'] ?? []; |
571
|
|
|
$returnsClone = $call['returns_clone'] ?? false; |
572
|
|
|
} else { |
573
|
|
|
if (1 === \count($call) && \is_string(key($call))) { |
574
|
|
|
$method = key($call); |
575
|
|
|
$args = $call[$method]; |
576
|
|
|
|
577
|
|
|
if ($args instanceof TaggedValue) { |
578
|
|
|
if ('returns_clone' !== $args->getTag()) { |
579
|
|
|
throw new InvalidArgumentException(\sprintf('Unsupported tag "!%s", did you mean "!returns_clone" for service "%s" in "%s"?', $args->getTag(), $id, $file)); |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
$returnsClone = true; |
583
|
|
|
$args = $args->getValue(); |
584
|
|
|
} else { |
585
|
|
|
$returnsClone = false; |
586
|
|
|
} |
587
|
|
|
} elseif (empty($call[0])) { |
588
|
|
|
throw new InvalidArgumentException(\sprintf('Invalid call for service "%s": the method must be defined as the first index of an array or as the only key of a map in "%s".', $id, $file)); |
589
|
|
|
} else { |
590
|
|
|
$method = $call[0]; |
591
|
|
|
$args = $call[1] ?? []; |
592
|
|
|
$returnsClone = $call[2] ?? false; |
593
|
|
|
} |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
if (!\is_array($args)) { |
597
|
|
|
throw new InvalidArgumentException(\sprintf('The second parameter for function call "%s" must be an array of its arguments for service "%s" in "%s". Check your YAML syntax.', $method, $id, $file)); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
$args = $this->resolveServices($args, $file); |
601
|
|
|
$definition->addMethodCall($method, $args, $returnsClone); |
602
|
|
|
} |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
$tags = $service['tags'] ?? []; |
606
|
|
|
if (!\is_array($tags)) { |
607
|
|
|
throw new InvalidArgumentException(\sprintf('Parameter "tags" must be an array for service "%s" in "%s". Check your YAML syntax.', $id, $file)); |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
if (isset($defaults['tags'])) { |
611
|
|
|
$tags = array_merge($tags, $defaults['tags']); |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
foreach ($tags as $tag) { |
615
|
|
|
if (!\is_array($tag)) { |
616
|
|
|
$tag = ['name' => $tag]; |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
if (1 === \count($tag) && \is_array(current($tag))) { |
620
|
|
|
$name = key($tag); |
621
|
|
|
$tag = current($tag); |
622
|
|
|
} else { |
623
|
|
|
if (!isset($tag['name'])) { |
624
|
|
|
throw new InvalidArgumentException(\sprintf('A "tags" entry is missing a "name" key for service "%s" in "%s".', $id, $file)); |
625
|
|
|
} |
626
|
|
|
$name = $tag['name']; |
627
|
|
|
unset($tag['name']); |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
if (!\is_string($name) || '' === $name) { |
631
|
|
|
throw new InvalidArgumentException(\sprintf('The tag name for service "%s" in "%s" must be a non-empty string.', $id, $file)); |
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
$this->validateAttributes(\sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in "%s". Check your YAML syntax.', $id, $name, '%s', $file), $tag); |
635
|
|
|
|
636
|
|
|
$definition->addTag($name, $tag); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
if (null !== $decorates = $service['decorates'] ?? null) { |
640
|
|
|
if ('' !== $decorates && '@' === $decorates[0]) { |
641
|
|
|
throw new InvalidArgumentException(\sprintf('The value of the "decorates" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $id, $service['decorates'], substr($decorates, 1))); |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
$decorationOnInvalid = \array_key_exists('decoration_on_invalid', $service) ? $service['decoration_on_invalid'] : 'exception'; |
645
|
|
|
if ('exception' === $decorationOnInvalid) { |
646
|
|
|
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE; |
647
|
|
|
} elseif ('ignore' === $decorationOnInvalid) { |
648
|
|
|
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE; |
649
|
|
|
} elseif (null === $decorationOnInvalid) { |
650
|
|
|
$invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE; |
651
|
|
|
} elseif ('null' === $decorationOnInvalid) { |
652
|
|
|
throw new InvalidArgumentException(\sprintf('Invalid value "%s" for attribute "decoration_on_invalid" on service "%s". Did you mean null (without quotes) in "%s"?', $decorationOnInvalid, $id, $file)); |
653
|
|
|
} else { |
654
|
|
|
throw new InvalidArgumentException(\sprintf('Invalid value "%s" for attribute "decoration_on_invalid" on service "%s". Did you mean "exception", "ignore" or null in "%s"?', $decorationOnInvalid, $id, $file)); |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
$renameId = $service['decoration_inner_name'] ?? null; |
658
|
|
|
$priority = $service['decoration_priority'] ?? 0; |
659
|
|
|
|
660
|
|
|
$definition->setDecoratedService($decorates, $renameId, $priority, $invalidBehavior); |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
if (isset($service['autowire'])) { |
664
|
|
|
$definition->setAutowired($service['autowire']); |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
if (isset($defaults['bind']) || isset($service['bind'])) { |
668
|
|
|
// deep clone, to avoid multiple process of the same instance in the passes |
669
|
|
|
$bindings = $definition->getBindings(); |
670
|
|
|
$bindings += isset($defaults['bind']) ? unserialize(serialize($defaults['bind'])) : []; |
671
|
|
|
|
672
|
|
|
if (isset($service['bind'])) { |
673
|
|
|
if (!\is_array($service['bind'])) { |
674
|
|
|
throw new InvalidArgumentException(\sprintf('Parameter "bind" must be an array for service "%s" in "%s". Check your YAML syntax.', $id, $file)); |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
$bindings = array_merge($bindings, $this->resolveServices($service['bind'], $file)); |
678
|
|
|
$bindingType = $this->isLoadingInstanceof ? BoundArgument::INSTANCEOF_BINDING : BoundArgument::SERVICE_BINDING; |
679
|
|
|
foreach ($bindings as $argument => $value) { |
680
|
|
|
if (!$value instanceof BoundArgument) { |
681
|
|
|
$bindings[$argument] = new BoundArgument($value, $trackBindings, $bindingType, $file); |
682
|
|
|
} |
683
|
|
|
} |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
$definition->setBindings($bindings); |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
if (isset($service['autoconfigure'])) { |
690
|
|
|
$definition->setAutoconfigured($service['autoconfigure']); |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
if (\array_key_exists('namespace', $service) && !\array_key_exists('resource', $service)) { |
694
|
|
|
throw new InvalidArgumentException(\sprintf('A "resource" attribute must be set when the "namespace" attribute is set for service "%s" in "%s". Check your YAML syntax.', $id, $file)); |
695
|
|
|
} |
696
|
|
|
|
697
|
|
|
if ($return) { |
698
|
|
|
if (\array_key_exists('resource', $service)) { |
699
|
|
|
throw new InvalidArgumentException(\sprintf('Invalid "resource" attribute found for service "%s" in "%s". Check your YAML syntax.', $id, $file)); |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
return $definition; |
703
|
|
|
} |
704
|
|
|
|
705
|
|
|
if (\array_key_exists('resource', $service)) { |
706
|
|
|
if (!\is_string($service['resource'])) { |
707
|
|
|
throw new InvalidArgumentException(\sprintf('A "resource" attribute must be of type string for service "%s" in "%s". Check your YAML syntax.', $id, $file)); |
708
|
|
|
} |
709
|
|
|
$exclude = $service['exclude'] ?? null; |
710
|
|
|
$namespace = $service['namespace'] ?? $id; |
711
|
|
|
$this->registerClasses($definition, $namespace, $service['resource'], $exclude, $file); |
712
|
|
|
} else { |
713
|
|
|
$this->setDefinition($id, $definition); |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
return null; |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
/** |
720
|
|
|
* @throws InvalidArgumentException When errors occur |
721
|
|
|
*/ |
722
|
|
|
private function parseCallable(mixed $callable, string $parameter, string $id, string $file): string|array|Reference |
723
|
|
|
{ |
724
|
|
|
if (\is_string($callable)) { |
725
|
|
|
if (str_starts_with($callable, '@=')) { |
726
|
|
|
if ('factory' !== $parameter) { |
727
|
|
|
throw new InvalidArgumentException(\sprintf('Using expressions in "%s" for the "%s" service is not supported in "%s".', $parameter, $id, $file)); |
728
|
|
|
} |
729
|
|
|
if (!class_exists(Expression::class)) { |
730
|
|
|
throw new \LogicException('The "@=" expression syntax cannot be used without the ExpressionLanguage component. Try running "composer require symfony/expression-language".'); |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
return $callable; |
734
|
|
|
} |
735
|
|
|
|
736
|
|
|
if ('' !== $callable && '@' === $callable[0]) { |
737
|
|
|
if (!str_contains($callable, ':')) { |
738
|
|
|
return [$this->resolveServices($callable, $file), '__invoke']; |
739
|
|
|
} |
740
|
|
|
|
741
|
|
|
throw new InvalidArgumentException(\sprintf('The value of the "%s" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s" in "%s").', $parameter, $id, $callable, substr($callable, 1), $file)); |
742
|
|
|
} |
743
|
|
|
|
744
|
|
|
return $callable; |
745
|
|
|
} |
746
|
|
|
|
747
|
|
|
if (\is_array($callable)) { |
748
|
|
|
if (isset($callable[0]) && isset($callable[1])) { |
749
|
|
|
return [$this->resolveServices($callable[0], $file), $callable[1]]; |
750
|
|
|
} |
751
|
|
|
|
752
|
|
|
if ('factory' === $parameter && isset($callable[1]) && null === $callable[0]) { |
753
|
|
|
return $callable; |
754
|
|
|
} |
755
|
|
|
|
756
|
|
|
throw new InvalidArgumentException(\sprintf('Parameter "%s" must contain an array with two elements for service "%s" in "%s". Check your YAML syntax.', $parameter, $id, $file)); |
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
throw new InvalidArgumentException(\sprintf('Parameter "%s" must be a string or an array for service "%s" in "%s". Check your YAML syntax.', $parameter, $id, $file)); |
760
|
|
|
} |
761
|
|
|
|
762
|
|
|
/** |
763
|
|
|
* Loads a YAML file. |
764
|
|
|
* |
765
|
|
|
* @throws InvalidArgumentException when the given file is not a local file or when it does not exist |
766
|
|
|
*/ |
767
|
|
|
protected function loadFile(string $file): ?array |
768
|
|
|
{ |
769
|
|
|
if (!class_exists(YamlParser::class)) { |
770
|
|
|
throw new RuntimeException('Unable to load YAML config files as the Symfony Yaml Component is not installed. Try running "composer require symfony/yaml".'); |
771
|
|
|
} |
772
|
|
|
|
773
|
|
|
if (!stream_is_local($file)) { |
774
|
|
|
throw new InvalidArgumentException(\sprintf('This is not a local file "%s".', $file)); |
775
|
|
|
} |
776
|
|
|
|
777
|
|
|
if (!is_file($file)) { |
778
|
|
|
throw new InvalidArgumentException(\sprintf('The file "%s" does not exist.', $file)); |
779
|
|
|
} |
780
|
|
|
|
781
|
|
|
$this->yamlParser ??= new YamlParser(); |
782
|
|
|
|
783
|
|
|
try { |
784
|
|
|
$configuration = $this->yamlParser->parseFile($file, Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS); |
785
|
|
|
} catch (ParseException $e) { |
786
|
|
|
throw new InvalidArgumentException(\sprintf('The file "%s" does not contain valid YAML: ', $file).$e->getMessage(), 0, $e); |
787
|
|
|
} |
788
|
|
|
|
789
|
|
|
return $this->validate($configuration, $file); |
790
|
|
|
} |
791
|
|
|
|
792
|
|
|
/** |
793
|
|
|
* Validates a YAML file. |
794
|
|
|
* |
795
|
|
|
* @throws InvalidArgumentException When service file is not valid |
796
|
|
|
*/ |
797
|
|
|
private function validate(mixed $content, string $file): ?array |
798
|
|
|
{ |
799
|
|
|
if (null === $content) { |
800
|
|
|
return $content; |
801
|
|
|
} |
802
|
|
|
|
803
|
|
|
if (!\is_array($content)) { |
804
|
|
|
throw new InvalidArgumentException(\sprintf('The service file "%s" is not valid. It should contain an array. Check your YAML syntax.', $file)); |
805
|
|
|
} |
806
|
|
|
|
807
|
|
|
foreach ($content as $namespace => $data) { |
808
|
|
|
if (\in_array($namespace, ['imports', 'parameters', 'services']) || str_starts_with($namespace, 'when@')) { |
809
|
|
|
continue; |
810
|
|
|
} |
811
|
|
|
|
812
|
|
|
if (!$this->prepend && !$this->container->hasExtension($namespace)) { |
813
|
|
|
$extensionNamespaces = array_filter(array_map(fn (ExtensionInterface $ext) => $ext->getAlias(), $this->container->getExtensions())); |
814
|
|
|
throw new InvalidArgumentException(UndefinedExtensionHandler::getErrorMessage($namespace, $file, $namespace, $extensionNamespaces)); |
815
|
|
|
} |
816
|
|
|
} |
817
|
|
|
|
818
|
|
|
return $content; |
819
|
|
|
} |
820
|
|
|
|
821
|
|
|
private function resolveServices(mixed $value, string $file, bool $isParameter = false): mixed |
822
|
|
|
{ |
823
|
|
|
if ($value instanceof TaggedValue) { |
824
|
|
|
$argument = $value->getValue(); |
825
|
|
|
|
826
|
|
|
if ('closure' === $value->getTag()) { |
827
|
|
|
$argument = $this->resolveServices($argument, $file, $isParameter); |
828
|
|
|
|
829
|
|
|
return (new Definition('Closure')) |
830
|
|
|
->setFactory(['Closure', 'fromCallable']) |
831
|
|
|
->addArgument($argument); |
832
|
|
|
} |
833
|
|
|
if ('iterator' === $value->getTag()) { |
834
|
|
|
if (!\is_array($argument)) { |
835
|
|
|
throw new InvalidArgumentException(\sprintf('"!iterator" tag only accepts sequences in "%s".', $file)); |
836
|
|
|
} |
837
|
|
|
$argument = $this->resolveServices($argument, $file, $isParameter); |
838
|
|
|
|
839
|
|
|
return new IteratorArgument($argument); |
840
|
|
|
} |
841
|
|
|
if ('service_closure' === $value->getTag()) { |
842
|
|
|
$argument = $this->resolveServices($argument, $file, $isParameter); |
843
|
|
|
|
844
|
|
|
return new ServiceClosureArgument($argument); |
845
|
|
|
} |
846
|
|
|
if ('service_locator' === $value->getTag()) { |
847
|
|
|
if (!\is_array($argument)) { |
848
|
|
|
throw new InvalidArgumentException(\sprintf('"!service_locator" tag only accepts maps in "%s".', $file)); |
849
|
|
|
} |
850
|
|
|
|
851
|
|
|
$argument = $this->resolveServices($argument, $file, $isParameter); |
852
|
|
|
|
853
|
|
|
return new ServiceLocatorArgument($argument); |
854
|
|
|
} |
855
|
|
|
if (\in_array($value->getTag(), ['tagged', 'tagged_iterator', 'tagged_locator'], true)) { |
856
|
|
|
if ('tagged' === $value->getTag()) { |
857
|
|
|
trigger_deprecation('symfony/dependency-injection', '7.2', 'Using "!tagged" is deprecated, use "!tagged_iterator" instead in "%s".', $file); |
858
|
|
|
} |
859
|
|
|
|
860
|
|
|
$forLocator = 'tagged_locator' === $value->getTag(); |
861
|
|
|
|
862
|
|
|
if (\is_array($argument) && isset($argument['tag']) && $argument['tag']) { |
863
|
|
|
if ($diff = array_diff(array_keys($argument), $supportedKeys = ['tag', 'index_by', 'default_index_method', 'default_priority_method', 'exclude', 'exclude_self'])) { |
864
|
|
|
throw new InvalidArgumentException(\sprintf('"!%s" tag contains unsupported key "%s"; supported ones are "%s".', $value->getTag(), implode('", "', $diff), implode('", "', $supportedKeys))); |
865
|
|
|
} |
866
|
|
|
|
867
|
|
|
$argument = new TaggedIteratorArgument($argument['tag'], $argument['index_by'] ?? null, $argument['default_index_method'] ?? null, $forLocator, $argument['default_priority_method'] ?? null, (array) ($argument['exclude'] ?? null), $argument['exclude_self'] ?? true); |
868
|
|
|
} elseif (\is_string($argument) && $argument) { |
869
|
|
|
$argument = new TaggedIteratorArgument($argument, null, null, $forLocator); |
870
|
|
|
} else { |
871
|
|
|
throw new InvalidArgumentException(\sprintf('"!%s" tags only accept a non empty string or an array with a key "tag" in "%s".', $value->getTag(), $file)); |
872
|
|
|
} |
873
|
|
|
|
874
|
|
|
if ($forLocator) { |
875
|
|
|
$argument = new ServiceLocatorArgument($argument); |
876
|
|
|
} |
877
|
|
|
|
878
|
|
|
return $argument; |
879
|
|
|
} |
880
|
|
|
if ('service' === $value->getTag()) { |
881
|
|
|
if ($isParameter) { |
882
|
|
|
throw new InvalidArgumentException(\sprintf('Using an anonymous service in a parameter is not allowed in "%s".', $file)); |
883
|
|
|
} |
884
|
|
|
|
885
|
|
|
$isLoadingInstanceof = $this->isLoadingInstanceof; |
886
|
|
|
$this->isLoadingInstanceof = false; |
887
|
|
|
$instanceof = $this->instanceof; |
888
|
|
|
$this->instanceof = []; |
889
|
|
|
|
890
|
|
|
$id = \sprintf('.%d_%s', ++$this->anonymousServicesCount, preg_replace('/^.*\\\\/', '', $argument['class'] ?? '').$this->anonymousServicesSuffix); |
891
|
|
|
$this->parseDefinition($id, $argument, $file, []); |
892
|
|
|
|
893
|
|
|
if (!$this->container->hasDefinition($id)) { |
894
|
|
|
throw new InvalidArgumentException(\sprintf('Creating an alias using the tag "!service" is not allowed in "%s".', $file)); |
895
|
|
|
} |
896
|
|
|
|
897
|
|
|
$this->container->getDefinition($id); |
898
|
|
|
|
899
|
|
|
$this->isLoadingInstanceof = $isLoadingInstanceof; |
900
|
|
|
$this->instanceof = $instanceof; |
901
|
|
|
|
902
|
|
|
return new Reference($id); |
903
|
|
|
} |
904
|
|
|
if ('abstract' === $value->getTag()) { |
905
|
|
|
return new AbstractArgument($value->getValue()); |
906
|
|
|
} |
907
|
|
|
|
908
|
|
|
throw new InvalidArgumentException(\sprintf('Unsupported tag "!%s".', $value->getTag())); |
909
|
|
|
} |
910
|
|
|
|
911
|
|
|
if (\is_array($value)) { |
912
|
|
|
foreach ($value as $k => $v) { |
913
|
|
|
$value[$k] = $this->resolveServices($v, $file, $isParameter); |
914
|
|
|
} |
915
|
|
|
} elseif (\is_string($value) && str_starts_with($value, '@=')) { |
916
|
|
|
if ($isParameter) { |
917
|
|
|
throw new InvalidArgumentException(\sprintf('Using expressions in parameters is not allowed in "%s".', $file)); |
918
|
|
|
} |
919
|
|
|
|
920
|
|
|
if (!class_exists(Expression::class)) { |
921
|
|
|
throw new \LogicException('The "@=" expression syntax cannot be used without the ExpressionLanguage component. Try running "composer require symfony/expression-language".'); |
922
|
|
|
} |
923
|
|
|
|
924
|
|
|
return new Expression(substr($value, 2)); |
925
|
|
|
} elseif (\is_string($value) && str_starts_with($value, '@')) { |
926
|
|
|
if (str_starts_with($value, '@@')) { |
927
|
|
|
$value = substr($value, 1); |
928
|
|
|
$invalidBehavior = null; |
929
|
|
|
} elseif (str_starts_with($value, '@!')) { |
930
|
|
|
$value = substr($value, 2); |
931
|
|
|
$invalidBehavior = ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE; |
932
|
|
|
} elseif (str_starts_with($value, '@?')) { |
933
|
|
|
$value = substr($value, 2); |
934
|
|
|
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE; |
935
|
|
|
} else { |
936
|
|
|
$value = substr($value, 1); |
937
|
|
|
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE; |
938
|
|
|
} |
939
|
|
|
|
940
|
|
|
if (null !== $invalidBehavior) { |
941
|
|
|
$value = new Reference($value, $invalidBehavior); |
942
|
|
|
} |
943
|
|
|
} |
944
|
|
|
|
945
|
|
|
return $value; |
946
|
|
|
} |
947
|
|
|
|
948
|
|
|
private function loadFromExtensions(array $content): void |
949
|
|
|
{ |
950
|
|
|
foreach ($content as $namespace => $values) { |
951
|
|
|
if (\in_array($namespace, ['imports', 'parameters', 'services']) || str_starts_with($namespace, 'when@')) { |
952
|
|
|
continue; |
953
|
|
|
} |
954
|
|
|
|
955
|
|
|
if (!\is_array($values)) { |
956
|
|
|
$values = []; |
957
|
|
|
} |
958
|
|
|
|
959
|
|
|
$this->loadExtensionConfig($namespace, $values); |
960
|
|
|
} |
961
|
|
|
|
962
|
|
|
$this->loadExtensionConfigs(); |
963
|
|
|
} |
964
|
|
|
|
965
|
|
|
private function checkDefinition(string $id, array $definition, string $file): void |
966
|
|
|
{ |
967
|
|
|
if ($this->isLoadingInstanceof) { |
968
|
|
|
$keywords = self::INSTANCEOF_KEYWORDS; |
969
|
|
|
} elseif (isset($definition['resource']) || isset($definition['namespace'])) { |
970
|
|
|
$keywords = self::PROTOTYPE_KEYWORDS; |
971
|
|
|
} else { |
972
|
|
|
$keywords = self::SERVICE_KEYWORDS; |
973
|
|
|
} |
974
|
|
|
|
975
|
|
|
foreach ($definition as $key => $value) { |
976
|
|
|
if (!isset($keywords[$key])) { |
977
|
|
|
throw new InvalidArgumentException(\sprintf('The configuration key "%s" is unsupported for definition "%s" in "%s". Allowed configuration keys are "%s".', $key, $id, $file, implode('", "', $keywords))); |
978
|
|
|
} |
979
|
|
|
} |
980
|
|
|
} |
981
|
|
|
|
982
|
|
|
private function validateAttributes(string $message, array $attributes, array $path = []): void |
983
|
|
|
{ |
984
|
|
|
foreach ($attributes as $name => $value) { |
985
|
|
|
if (\is_array($value)) { |
986
|
|
|
$this->validateAttributes($message, $value, [...$path, $name]); |
987
|
|
|
} elseif (!\is_scalar($value ?? '')) { |
988
|
|
|
$name = implode('.', [...$path, $name]); |
989
|
|
|
throw new InvalidArgumentException(\sprintf($message, $name)); |
990
|
|
|
} |
991
|
|
|
} |
992
|
|
|
} |
993
|
|
|
} |
994
|
|
|
|