10000 feature #22527 [DI] Throw useful exception on bad XML argument tags (… · symfony/symfony@8872833 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8872833

Browse files
committed
feature #22527 [DI] Throw useful exception on bad XML argument tags (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] Throw useful exception on bad XML argument tags | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22525 | License | MIT | Doc PR | - I still think that the feature request in #22525 would make things better. But at least, let's make thing fail loudly, instead of silently today, with the associated usual wtfs :) Commits ------- 91828ec [DI] Throw useful exception on bad XML argument tags
2 parents 35608f5 + 91828ec commit 8872833

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function load($resource, $type = null)
5050
$this->parseImports($xml, $path);
5151

5252
// parameters
53-
$this->parseParameters($xml);
53+
$this->parseParameters($xml, $path);
5454

5555
// extensions
5656
$this->loadFromExtensions($xml);
@@ -83,11 +83,12 @@ public function supports($resource, $type = null)
8383
* Parses parameters.
8484
*
8585
* @param \DOMDocument $xml
86+
* @param string $file
8687
*/
87-
private function parseParameters(\DOMDocument $xml)
88+
private function parseParameters(\DOMDocument $xml, $file)
8889
{
8990
if ($parameters = $this->getChildren($xml->documentElement, 'parameters')) {
90-
$this->container->getParameterBag()->add($this->getArgumentsAsPhp($parameters[0], 'parameter'));
91+
$this->container->getParameterBag()->add($this->getArgumentsAsPhp($parameters[0], 'parameter', $file));
9192
}
9293
}
9394

@@ -278,8 +279,8 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults =
278279
$definition->setDeprecated(true, $deprecated[0]->nodeValue ?: null);
279280
}
280281

281-
$definition->setArguments($this->getArgumentsAsPhp($service, 'argument', false, $definition instanceof ChildDefinition));
282-
$definition->setProperties($this->getArgumentsAsPhp($service, 'property'));
282+
$definition->setArguments($this->getArgumentsAsPhp($service, 'argument', $file, false, $definition instanceof ChildDefinition));
283+
$definition->setProperties($this->getArgumentsAsPhp($service, 'property', $file));
283284

284285
if ($factories = $this->getChildren($service, 'factory')) {
285286
$factory = $factories[0];
@@ -312,7 +313,7 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults =
312313
}
313314

314315
foreach ($this->getChildren($service, 'call') as $call) {
315-
$definition->addMethodCall($call->getAttribute('method'), $this->getArgumentsAsPhp($call, 'argument'));
316+
$definition->addMethodCall($call->getAttribute('method'), $this->getArgumentsAsPhp($call, 'argument', $file));
316317
}
317318

318319
$tags = $this->getChildren($service, 'tag');
@@ -446,11 +447,12 @@ private function processAnonymousServices(\DOMDocument $xml, $file)
446447
*
447448
* @param \DOMElement $node
448449
* @param string $name
450+
* @param string $file
449451
* @param bool $lowercase
450452
*
451453
* @return mixed
452454
*/
453-
private function getArgumentsAsPhp(\DOMElement $node, $name, $lowercase = true, $isChildDefinition = false)
455+
private function getArgumentsAsPhp(\DOMElement $node, $name, $file, $lowercase = true, $isChildDefinition = false)
454456
{
455457
$arguments = array();
456458
foreach ($this->getChildren($node, $name) as $arg) {
@@ -486,6 +488,9 @@ private function getArgumentsAsPhp(\DOMElement $node, $name, $lowercase = true,
486488

487489
switch ($arg->getAttribute('type')) {
488490
case 'service':
491+
if (!$arg->getAttribute('id')) {
492+
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="service" has no or empty "id" attribute in "%s".', $name, $file));
493+
}
489494
if ($arg->hasAttribute('strict')) {
490495
@trigger_error(sprintf('The "strict" attribute used when referencing the "%s" service is deprecated since version 3.3 and will be removed in 4.0.', $arg->getAttribute('id')), E_USER_DEPRECATED);
491496
}
@@ -496,17 +501,23 @@ private function getArgumentsAsPhp(\DOMElement $node, $name, $lowercase = true,
496501
$arguments[$key] = new Expression($arg->nodeValue);
497502
break;
498503
case 'closure-proxy':
504+
if (!$arg->getAttribute('id')) {
505+
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="closure-proxy" has no or empty "id" attribute in "%s".', $name, $file));
506+
}
507+
if (!$arg->getAttribute('method')) {
508+
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="closure-proxy" has no or empty "method" attribute in "%s".', $name, $file));
509+
}
499510
$arguments[$key] = new ClosureProxyArgument($arg->getAttribute('id'), $arg->getAttribute('method'), $invalidBehavior);
500511
break;
501512
case 'collection':
502-
$arguments[$key] = $this->getArgumentsAsPhp($arg, $name, false);
513+
$arguments[$key] = $this->getArgumentsAsPhp($arg, $name, $file, false);
503514
break;
504515
case 'iterator':
505-
$arg = $this->getArgumentsAsPhp($arg, $name, false);
516+
$arg = $this->getArgumentsAsPhp($arg, $name, $file, false);
506517
try {
507518
$arguments[$key] = new IteratorArgument($arg);
508519
} catch (InvalidArgumentException $e) {
509-
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="iterator" only accepts collections of type="service" references.', $name));
520+
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="iterator" only accepts collections of type="service" references in "%s".', $name, $file));
510521
}
511522
break;
512523
case 'string':

0 commit comments

Comments
 (0)
0