Container::resolveServicesAndParameters

protected Container::resolveServicesAndParameters($arguments)

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

Parameters

array|\stdClass $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.

File

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

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));
    }
    // In case there is nothing to resolve, we are done here.
    if (!$arguments->resolve) {
      return $arguments->value;
    }
    $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;
      }
      // Create private service.
      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;
      }
      // Check for collection.
      elseif ($type == 'collection') {
        $value = $argument->value;

        // Does this collection need resolving?
        if ($argument->resolve) {
          $arguments[$key] = $this->resolveServicesAndParameters($value);
        }
        else {
          $arguments[$key] = $value;
        }

        continue;
      }

      if ($type !== NULL) {
        throw new InvalidArgumentException(sprintf('Undefined type "%s" while resolving parameters and services.', $type));
      }
    }
  }

  return $arguments;
}
doc_Drupal
2016-10-29 08:56:39
Comments
Leave a Comment

Please login to continue.