function Container::resolveServicesAndParameters

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/DependencyInjection/Container.php \Drupal\Component\DependencyInjection\Container::resolveServicesAndParameters()
  2. 8.9.x core/lib/Drupal/Component/DependencyInjection/Container.php \Drupal\Component\DependencyInjection\Container::resolveServicesAndParameters()
  3. 10 core/lib/Drupal/Component/DependencyInjection/Container.php \Drupal\Component\DependencyInjection\Container::resolveServicesAndParameters()

Resolves arguments that represent services or variables to the real values.

Parameters

array|object $arguments: The arguments to resolve.

Return value

array The resolved arguments.

Throws

\Symfony\Component\DependencyInjection\Exception\RuntimeException If a parameter/service could not be resolved.

\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException If an unknown type is met while resolving parameters and services.

1 call to Container::resolveServicesAndParameters()
Container::createService in core/lib/Drupal/Component/DependencyInjection/Container.php
Creates a service from a service definition.
1 method overrides Container::resolveServicesAndParameters()
PhpArrayContainer::resolveServicesAndParameters in core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php
Resolves arguments that represent services or variables to the real values.

File

core/lib/Drupal/Component/DependencyInjection/Container.php, line 377

Class

Container
Provides a container optimized for Drupal's needs.

Namespace

Drupal\Component\DependencyInjection

Code

protected function resolveServicesAndParameters($arguments) {
    // Check if this collection needs to be resolved.
    if ($arguments instanceof \stdClass) {
        if ($arguments->type !== 'collection') {
            throw new InvalidArgumentException(sprintf('Undefined type "%s" while resolving parameters and services.', $arguments->type));
        }
        $arguments = $arguments->value;
    }
    // Process the arguments.
    foreach ($arguments as $key => $argument) {
        // For this machine-optimized format, only \stdClass arguments are
        // processed and resolved. All other values are kept as is.
        if ($argument instanceof \stdClass) {
            $type = $argument->type;
            // Check for parameter.
            if ($type == 'parameter') {
                $name = $argument->name;
                if (!isset($this->parameters[$name])) {
                    $arguments[$key] = $this->getParameter($name);
                    // This can never be reached as getParameter() throws an Exception,
                    // because we already checked that the parameter is not set above.
                }
                // Update argument.
                $argument = $arguments[$key] = $this->parameters[$name];
                // In case there is not a machine readable value (e.g. a service)
                // behind this resolved parameter, continue.
                if (!$argument instanceof \stdClass) {
                    continue;
                }
                // Fall through.
                $type = $argument->type;
            }
            // Create a service.
            if ($type == 'service') {
                $id = $argument->id;
                // Does the service already exist?
                if (isset($this->aliases[$id])) {
                    $id = $this->aliases[$id];
                }
                if (isset($this->services[$id])) {
                    $arguments[$key] = $this->services[$id];
                    continue;
                }
                // Return the service.
                $arguments[$key] = $this->get($id, $argument->invalidBehavior);
                continue;
            }
            elseif ($type == 'private_service') {
                $id = $argument->id;
                // Does the private service already exist.
                if (isset($this->privateServices[$id])) {
                    $arguments[$key] = $this->privateServices[$id];
                    continue;
                }
                // Create the private service.
                $arguments[$key] = $this->createService($argument->value, $id);
                if ($argument->shared) {
                    $this->privateServices[$id] = $arguments[$key];
                }
                continue;
            }
            elseif ($type == 'service_closure') {
                $arguments[$key] = function () use ($argument) {
                    return $this->get($argument->id, $argument->invalidBehavior);
                };
                continue;
            }
            elseif ($type == 'iterator') {
                $services = $argument->value;
                $arguments[$key] = new RewindableGenerator(function () use ($services) {
                    foreach ($services as $key => $service) {
                        (yield $key => $this->resolveServicesAndParameters([
                            $service,
                        ])[0]);
                    }
                }, count($services));
                continue;
            }
            elseif ($type == 'collection') {
                $arguments[$key] = $this->resolveServicesAndParameters($argument->value);
                continue;
            }
            elseif ($type == 'raw') {
                $arguments[$key] = $argument->value;
                continue;
            }
            if ($type !== NULL) {
                throw new InvalidArgumentException(sprintf('Undefined type "%s" while resolving parameters and services.', $type));
            }
        }
    }
    return $arguments;
}

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