function YamlFileLoader::parseDefinition
Same name in other branches
- 8.9.x core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php \Drupal\Core\DependencyInjection\YamlFileLoader::parseDefinition()
- 10 core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php \Drupal\Core\DependencyInjection\YamlFileLoader::parseDefinition()
- 11.x core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php \Drupal\Core\DependencyInjection\YamlFileLoader::parseDefinition()
Parses a definition.
Parameters
string $id:
array $service:
string $file:
array $defaults:
Throws
InvalidArgumentException When tags are invalid.
1 call to YamlFileLoader::parseDefinition()
- YamlFileLoader::parseDefinitions in core/
lib/ Drupal/ Core/ DependencyInjection/ YamlFileLoader.php - Parses definitions
File
-
core/
lib/ Drupal/ Core/ DependencyInjection/ YamlFileLoader.php, line 206
Class
- YamlFileLoader
- YamlFileLoader loads YAML files service definitions.
Namespace
Drupal\Core\DependencyInjectionCode
private function parseDefinition(string $id, $service, string $file, array $defaults) {
if (\is_string($service) && 0 === strpos($service, '@')) {
$this->container
->setAlias($id, $alias = new Alias(substr($service, 1)));
if (isset($defaults['public'])) {
$alias->setPublic($defaults['public']);
}
return;
}
if (null === $service) {
$service = [];
}
if (!is_array($service)) {
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.', gettype($service), $id, $file));
}
if (isset($service['alias'])) {
$this->container
->setAlias($id, $alias = new Alias($service['alias']));
if (isset($service['public'])) {
$alias->setPublic($service['public']);
}
elseif (isset($defaults['public'])) {
$alias->setPublic($defaults['public']);
}
if (array_key_exists('deprecated', $service)) {
if (method_exists($alias, 'getDeprecation')) {
$deprecation = \is_array($service['deprecated']) ? $service['deprecated'] : [
'message' => $service['deprecated'],
];
$alias->setDeprecated($deprecation['package'] ?? '', $deprecation['version'] ?? '', $deprecation['message']);
}
else {
// @todo Remove when we no longer support Symfony 4 in
// https://www.drupal.org/project/drupal/issues/3197729
$alias->setDeprecated(true, $service['deprecated']);
}
}
return;
}
if (isset($service['parent'])) {
$definition = new ChildDefinition($service['parent']);
}
else {
$definition = new Definition();
// Drupal services are public by default.
$definition->setPublic(true);
if (isset($defaults['public'])) {
$definition->setPublic($defaults['public']);
}
if (isset($defaults['autowire'])) {
$definition->setAutowired($defaults['autowire']);
}
$definition->setChanges([]);
}
if (isset($service['class'])) {
$definition->setClass($service['class']);
}
if (isset($service['shared'])) {
$definition->setShared($service['shared']);
}
if (isset($service['synthetic'])) {
$definition->setSynthetic($service['synthetic']);
}
if (isset($service['lazy'])) {
$definition->setLazy($service['lazy']);
}
if (isset($service['public'])) {
$definition->setPublic($service['public']);
}
if (isset($service['abstract'])) {
$definition->setAbstract($service['abstract']);
}
if (array_key_exists('deprecated', $service)) {
if (method_exists($definition, 'getDeprecation')) {
$deprecation = \is_array($service['deprecated']) ? $service['deprecated'] : [
'message' => $service['deprecated'],
];
$definition->setDeprecated($deprecation['package'] ?? '', $deprecation['version'] ?? '', $deprecation['message']);
}
else {
// @todo Remove when we no longer support Symfony 4 in
// https://www.drupal.org/project/drupal/issues/3197729
$definition->setDeprecated(true, $service['deprecated']);
}
}
if (isset($service['factory'])) {
if (is_string($service['factory'])) {
if (strpos($service['factory'], ':') !== false && strpos($service['factory'], '::') === false) {
$parts = explode(':', $service['factory']);
$definition->setFactory(array(
$this->resolveServices('@' . $parts[0]),
$parts[1],
));
}
else {
$definition->setFactory($service['factory']);
}
}
else {
$definition->setFactory(array(
$this->resolveServices($service['factory'][0]),
$service['factory'][1],
));
}
}
if (isset($service['factory_class'])) {
$definition->setFactory($service['factory_class']);
}
if (isset($service['factory_method'])) {
$definition->setFactory($service['factory_method']);
}
if (isset($service['factory_service'])) {
$definition->setFactory($service['factory_service']);
}
if (isset($service['file'])) {
$definition->setFile($service['file']);
}
if (isset($service['arguments'])) {
$definition->setArguments($this->resolveServices($service['arguments']));
}
if (isset($service['properties'])) {
$definition->setProperties($this->resolveServices($service['properties']));
}
if (isset($service['configurator'])) {
if (is_string($service['configurator'])) {
$definition->setConfigurator($service['configurator']);
}
else {
$definition->setConfigurator(array(
$this->resolveServices($service['configurator'][0]),
$service['configurator'][1],
));
}
}
if (isset($service['calls'])) {
if (!is_array($service['calls'])) {
throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
}
foreach ($service['calls'] as $call) {
if (isset($call['method'])) {
$method = $call['method'];
$args = isset($call['arguments']) ? $this->resolveServices($call['arguments']) : array();
}
else {
$method = $call[0];
$args = isset($call[1]) ? $this->resolveServices($call[1]) : array();
}
$definition->addMethodCall($method, $args);
}
}
$tags = $service['tags'] ?? [];
if (!\is_array($tags)) {
throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in "%s". Check your YAML syntax.', $id, $file));
}
if (isset($defaults['tags'])) {
$tags = array_merge($tags, $defaults['tags']);
}
foreach ($tags as $tag) {
if (!\is_array($tag)) {
$tag = [
'name' => $tag,
];
}
if (!isset($tag['name'])) {
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in "%s".', $id, $file));
}
$name = $tag['name'];
unset($tag['name']);
if (!\is_string($name) || '' === $name) {
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in "%s" must be a non-empty string.', $id, $file));
}
foreach ($tag as $attribute => $value) {
if (!is_scalar($value) && null !== $value) {
throw new InvalidArgumentException(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, $attribute, $file));
}
}
$definition->addTag($name, $tag);
}
if (isset($service['decorates'])) {
$renameId = $service['decoration_inner_name'] ?? null;
$priority = $service['decoration_priority'] ?? 0;
$definition->setDecoratedService($service['decorates'], $renameId, $priority);
}
if (isset($service['autowire'])) {
$definition->setAutowired($service['autowire']);
}
$this->container
->setDefinition($id, $definition);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.