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

Skip to content

[DI] Throw useful exception on bad XML argument tags #22527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function load($resource, $type = null)
$this->parseImports($xml, $path);

// parameters
$this->parseParameters($xml);
$this->parseParameters($xml, $path);

// extensions
$this->loadFromExtensions($xml);
Expand Down Expand Up @@ -83,11 +83,12 @@ public function supports($resource, $type = null)
* Parses parameters.
*
* @param \DOMDocument $xml
* @param string $file
*/
private function parseParameters(\DOMDocument $xml)
private function parseParameters(\DOMDocument $xml, $file)
{
if ($parameters = $this->getChildren($xml->documentElement, 'parameters')) {
$this->container->getParameterBag()->add($this->getArgumentsAsPhp($parameters[0], 'parameter'));
$this->container->getParameterBag()->add($this->getArgumentsAsPhp($parameters[0], 'parameter', $file));
}
}

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

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

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

foreach ($this->getChildren($service, 'call') as $call) {
$definition->addMethodCall($call->getAttribute('method'), $this->getArgumentsAsPhp($call, 'argument'));
$definition->addMethodCall($call->getAttribute('method'), $this->getArgumentsAsPhp($call, 'argument', $file));
}

$tags = $this->getChildren($service, 'tag');
Expand Down Expand Up @@ -434,11 +435,12 @@ private function processAnonymousServices(\DOMDocument $xml, $file)
*
* @param \DOMElement $node
* @param string $name
* @param string $file
* @param bool $lowercase
*
* @return mixed
*/
private function getArgumentsAsPhp(\DOMElement $node, $name, $lowercase = true, $isChildDefinition = false)
private function getArgumentsAsPhp(\DOMElement $node, $name, $file, $lowercase = true, $isChildDefinition = false)
{
$arguments = array();
foreach ($this->getChildren($node, $name) as $arg) {
Expand Down Expand Up @@ -474,6 +476,9 @@ private function getArgumentsAsPhp(\DOMElement $node, $name, $lowercase = true,

switch ($arg->getAttribute('type')) {
case 'service':
if (!$arg->getAttribute('id')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasAttribute?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An empty attribute is also invalid, so better as is IMHO

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok 👍

throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="service" has no or empty "id" attribute in "%s".', $name, $file));
}
if ($arg->hasAttribute('strict')) {
@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);
}
Expand All @@ -484,17 +489,23 @@ private function getArgumentsAsPhp(\DOMElement $node, $name, $lowercase = true,
$arguments[$key] = new Expression($arg->nodeValue);
break;
case 'closure-proxy':
if (!$arg->getAttribute('id')) {
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="closure-proxy" has no or empty "id" attribute in "%s".', $name, $file));
}
if (!$arg->getAttribute('method')) {
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="closure-proxy" has no or empty "method" attribute in "%s".', $name, $file));
}
$arguments[$key] = new ClosureProxyArgument($arg->getAttribute('id'), $arg->getAttribute('method'), $invalidBehavior);
break;
case 'collection':
$arguments[$key] = $this->getArgumentsAsPhp($arg, $name, false);
$arguments[$key] = $this->getArgumentsAsPhp($arg, $name, $file, false);
break;
case 'iterator':
$arg = $this->getArgumentsAsPhp($arg, $name, false);
$arg = $this->getArgumentsAsPhp($arg, $name, $file, false);
try {
$arguments[$key] = new IteratorArgument($arg);
} catch (InvalidArgumentException $e) {
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="iterator" only accepts collections of type="service" references.', $name));
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="iterator" only accepts collections of type="service" references in "%s".', $name, $file));
}
break;
case 'string':
Expand Down