function YamlFileLoader::resolveServices

Same name in other branches
  1. 9 core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php \Drupal\Core\DependencyInjection\YamlFileLoader::resolveServices()
  2. 8.9.x core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php \Drupal\Core\DependencyInjection\YamlFileLoader::resolveServices()
  3. 10 core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php \Drupal\Core\DependencyInjection\YamlFileLoader::resolveServices()

Resolves services.

Parameters

string|array $value:

Return value

array|string|Reference

2 calls to YamlFileLoader::resolveServices()
YamlFileLoader::load in core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php
Loads a Yaml file.
YamlFileLoader::parseDefinition in core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php
Parses a definition.

File

core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php, line 471

Class

YamlFileLoader
YamlFileLoader loads YAML files service definitions.

Namespace

Drupal\Core\DependencyInjection

Code

private function resolveServices(mixed $value) : mixed {
    if ($value instanceof TaggedValue) {
        $argument = $value->getValue();
        if (\in_array($value->getTag(), [
            'tagged',
            'tagged_iterator',
            'tagged_locator',
        ], true)) {
            $forLocator = 'tagged_locator' === $value->getTag();
            if (\is_array($argument) && isset($argument['tag']) && $argument['tag']) {
                if ($diff = array_diff(array_keys($argument), $supportedKeys = [
                    'tag',
                    'index_by',
                    'default_index_method',
                    'default_priority_method',
                    'exclude',
                    'exclude_self',
                ])) {
                    throw new InvalidArgumentException(sprintf('"!%s" tag contains unsupported key "%s"; supported ones are "%s".', $value->getTag(), implode('", "', $diff), implode('", "', $supportedKeys)));
                }
                $argument = new TaggedIteratorArgument($argument['tag'], $argument['index_by'] ?? null, $argument['default_index_method'] ?? null, $forLocator, $argument['default_priority_method'] ?? null, (array) ($argument['exclude'] ?? null), $argument['exclude_self'] ?? true);
            }
            elseif (\is_string($argument) && $argument) {
                $argument = new TaggedIteratorArgument($argument, null, null, $forLocator);
            }
            else {
                throw new InvalidArgumentException(sprintf('"!%s" tags only accept a non empty string or an array with a key "tag"".', $value->getTag()));
            }
            if ($forLocator) {
                $argument = new ServiceLocatorArgument($argument);
            }
            return $argument;
        }
        if ($value->getTag() === 'service_closure') {
            return new ServiceClosureArgument($this->resolveServices($argument));
        }
    }
    if (is_array($value)) {
        $value = array_map(array(
            $this,
            'resolveServices',
        ), $value);
    }
    elseif (is_string($value) && str_starts_with($value, '@=')) {
        // Not supported.
        
        //return new Expression(substr($value, 2));
        throw new InvalidArgumentException(sprintf("'%s' is an Expression, but expressions are not supported.", $value));
    }
    elseif (is_string($value) && str_starts_with($value, '@')) {
        if (str_starts_with($value, '@@')) {
            $value = substr($value, 1);
            $invalidBehavior = null;
        }
        elseif (str_starts_with($value, '@?')) {
            $value = substr($value, 2);
            $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
        }
        else {
            $value = substr($value, 1);
            $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
        }
        if (str_ends_with($value, '=')) {
            $value = substr($value, 0, -1);
        }
        if (null !== $invalidBehavior) {
            $value = new Reference($value, $invalidBehavior);
        }
    }
    return $value;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.