Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ae21520

Browse files
[DI] Throw useful exception on bad XML argument tags
1 parent d696b39 commit ae21520

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/assets.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222

2323
<service id="assets.path_package" class="Symfony\Component\Asset\PathPackage" abstract="true">
2424
<argument /> <!-- base path -->
25-
<argument type="service" /> <!-- version strategy -->
25+
<argument /> <!-- version strategy -->
2626
<argument type="service" id="assets.context" />
2727
</service>
2828

2929
<service id="assets.url_package" class="Symfony\Component\Asset\UrlPackage" abstract="true">
3030
<argument /> <!-- base URLs -->
31-
<argument type="service" /> <!-- version strategy -->
31+
<argument /> <!-- version strategy -->
3232
<argument type="service" id="assets.context" />
3333
</service>
3434

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

@@ -266,8 +267,8 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults =
266267
$definition->setDeprecated(true, $deprecated[0]->nodeValue ?: null);
267268
}
268269

269-
$definition->setArguments($this->getArgumentsAsPhp($service, 'argument', false, $definition instanceof ChildDefinition));
270-
$definition->setProperties($this->getArgumentsAsPhp($service, 'property'));
270+
$definition->setArguments($this->getArgumentsAsPhp($service, 'argument', $file, false, $definition instanceof ChildDefinition));
271+
$definition->setProperties($this->getArgumentsAsPhp($service, 'property', $file));
271272

272273
if ($factories = $this->getChildren($service, 'factory')) {
273274
$factory = $factories[0];
@@ -300,7 +301,7 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults =
300301
}
301302

302303
foreach ($this->getChildren($service, 'call') as $call) {
303-
$definition->addMethodCall($call->getAttribute('method'), $this->getArgumentsAsPhp($call, 'argument'));
304+
$definition->addMethodCall($call->getAttribute('method'), $this->getArgumentsAsPhp($call, 'argument', $file));
304305
}
305306

306307
$tags = $this->getChildren($service, 'tag');
@@ -434,11 +435,12 @@ private function processAnonymousServices(\DOMDocument $xml, $file)
434435
*
435436
* @param \DOMElement $node
436437
* @param string $name
438+
* @param string $file
437439
* @param bool $lowercase
438440
*
439441
* @return mixed
440442
*/
441-
private function getArgumentsAsPhp(\DOMElement $node, $name, $lowercase = true, $isChildDefinition = false)
443+
private function getArgumentsAsPhp(\DOMElement $node, $name, $file, $lowercase = true, $isChildDefinition = false)
442444
{
443445
$arguments = array();
444446
foreach ($this->getChildren($node, $name) as $arg) {
@@ -474,6 +476,9 @@ private function getArgumentsAsPhp(\DOMElement $node, $name, $lowercase = true,
474476

475477
switch ($arg->getAttribute('type')) {
476478
case 'service':
479+
if (!$arg->getAttribute('id')) {
480+
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="service" is missing an "id" attribute in "%s".', $name, $file));
481+
}
477482
if ($arg->hasAttribute('strict')) {
478483
@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);
479484
}
@@ -484,17 +489,23 @@ private function getArgumentsAsPhp(\DOMElement $node, $name, $lowercase = true,
484489
$arguments[$key] = new Expression($arg->nodeValue);
485490
break;
486491
case 'closure-proxy':
492+
if (!$arg->getAttribute('id')) {
493+
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="closure-proxy" is missing an "id" attribute in "%s".', $name, $file));
494+
}
495+
if (!$arg->getAttribute('method')) {
496+
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="closure-proxy" is missing a "method" attribute in "%s".', $name, $file));
497+
}
487498
$arguments[$key] = new ClosureProxyArgument($arg->getAttribute('id'), $arg->getAttribute('method'), $invalidBehavior);
488499
break;
489500
case 'collection':
490-
$arguments[$key] = $this->getArgumentsAsPhp($arg, $name, false);
501+
$arguments[$key] = $this->getArgumentsAsPhp($arg, $name, $file, false);
491502
break;
492503
case 'iterator':
493-
$arg = $this->getArgumentsAsPhp($arg, $name, false);
504+
$arg = $this->getArgumentsAsPhp($arg, $name, $file, false);
494505
try {
495506
$arguments[$key] = new IteratorArgument($arg);
496507
} catch (InvalidArgumentException $e) {
497-
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="iterator" only accepts collections of type="service" references.', $name));
508+
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="iterator" only accepts collections of type="service" references in "%s".', $name, $file));
498509
}
499510
break;
500511
case 'string':

0 commit comments

Comments
 (0)