[DependencyInjection] YAML: reduce the complexity of the _defaults parser

This commit is contained in:
Kévin Dunglas 2017-01-19 11:47:23 +01:00
parent 4491eb6463
commit 130fa1f4f7
No known key found for this signature in database
GPG Key ID: 4D04EBEF06AAF3A6

View File

@ -157,14 +157,35 @@ class YamlFileLoader extends FileLoader
if (!is_array($content['services'])) {
throw new InvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file));
}
if (isset($content['services']['_defaults'])) {
$defaults = $this->parseDefaults($content, $file);
foreach ($content['services'] as $id => $service) {
$this->parseDefinition($id, $service, $file, $defaults);
}
}
/**
* @param array $content
* @param string $file
*
* @return array
*
* @throws InvalidArgumentException
*/
private function parseDefaults(array &$content, $file)
{
if (!isset($content['services']['_defaults'])) {
return $content;
}
if (!is_array($defaults = $content['services']['_defaults'])) {
throw new InvalidArgumentException(sprintf('Service defaults must be an array, "%s" given in "%s".', gettype($defaults), $file));
}
if (isset($defaults['alias']) || isset($defaults['class']) || isset($defaults['factory'])) {
@trigger_error('Giving a service the "_defaults" name is deprecated since Symfony 3.3 and will be forbidden in 4.0. Rename your service.', E_USER_DEPRECATED);
$defaults = array();
} else {
return $content;
}
$defaultKeys = array('public', 'tags', 'inherit_tags', 'autowire');
unset($content['services']['_defaults']);
@ -173,7 +194,9 @@ class YamlFileLoader extends FileLoader
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('", "', $defaultKeys)));
}
}
if (isset($defaults['tags'])) {
if (!isset($defaults['tags'])) {
return $defaults;
}
if (!is_array($tags = $defaults['tags'])) {
throw new InvalidArgumentException(sprintf('Parameter "tags" in "_defaults" must be an array in %s. Check your YAML syntax.', $file));
}
@ -199,15 +222,8 @@ class YamlFileLoader extends FileLoader
}
}
}
}
}
} else {
$defaults = array();
}
foreach ($content['services'] as $id => $service) {
$this->parseDefinition($id, $service, $file, $defaults);
}
return $defaults;
}
/**