private function ContainerBuilder::createService

Creates a service for a service definition.

Parameters

Definition $definition A service definition instance:

string $id The service identifier:

Return value

object The service described by the service definition

Throws

RuntimeException When factory specification is incomplete or scope is inactive

InvalidArgumentException When configure callable is not callable

2 calls to ContainerBuilder::createService()
ContainerBuilder::get in drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php
Gets a service.
ContainerBuilder::resolveServices in drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php
Replaces service references by the real service instance.

File

drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/ContainerBuilder.php, line 729

Class

ContainerBuilder
ContainerBuilder is a DI container that provides an API to easily describe services.

Namespace

Symfony\Component\DependencyInjection

Code

private function createService(Definition $definition, $id) {
  $parameterBag = $this
    ->getParameterBag();
  if (null !== $definition
    ->getFile()) {
    require_once $parameterBag
      ->resolveValue($definition
      ->getFile());
  }
  $arguments = $this
    ->resolveServices($parameterBag
    ->unescapeValue($parameterBag
    ->resolveValue($definition
    ->getArguments())));
  if (null !== $definition
    ->getFactoryMethod()) {
    if (null !== $definition
      ->getFactoryClass()) {
      $factory = $parameterBag
        ->resolveValue($definition
        ->getFactoryClass());
    }
    elseif (null !== $definition
      ->getFactoryService()) {
      $factory = $this
        ->get($parameterBag
        ->resolveValue($definition
        ->getFactoryService()));
    }
    else {
      throw new RuntimeException('Cannot create service from factory method without a factory service or factory class.');
    }
    $service = call_user_func_array(array(
      $factory,
      $definition
        ->getFactoryMethod(),
    ), $arguments);
  }
  else {
    $r = new \ReflectionClass($parameterBag
      ->resolveValue($definition
      ->getClass()));
    $service = null === $r
      ->getConstructor() ? $r
      ->newInstance() : $r
      ->newInstanceArgs($arguments);
  }
  if (self::SCOPE_PROTOTYPE !== ($scope = $definition
    ->getScope())) {
    if (self::SCOPE_CONTAINER !== $scope && !isset($this->scopedServices[$scope])) {
      throw new RuntimeException('You tried to create a service of an inactive scope.');
    }
    $this->services[$lowerId = strtolower($id)] = $service;
    if (self::SCOPE_CONTAINER !== $scope) {
      $this->scopedServices[$scope][$lowerId] = $service;
    }
  }
  foreach ($definition
    ->getMethodCalls() as $call) {
    $services = self::getServiceConditionals($call[1]);
    $ok = true;
    foreach ($services as $s) {
      if (!$this
        ->has($s)) {
        $ok = false;
        break;
      }
    }
    if ($ok) {
      call_user_func_array(array(
        $service,
        $call[0],
      ), $this
        ->resolveServices($parameterBag
        ->resolveValue($call[1])));
    }
  }
  $properties = $this
    ->resolveServices($parameterBag
    ->resolveValue($definition
    ->getProperties()));
  foreach ($properties as $name => $value) {
    $service->{$name} = $value;
  }
  if ($callable = $definition
    ->getConfigurator()) {
    if (is_array($callable) && is_object($callable[0]) && $callable[0] instanceof Reference) {
      $callable[0] = $this
        ->get((string) $callable[0]);
    }
    elseif (is_array($callable)) {
      $callable[0] = $parameterBag
        ->resolveValue($callable[0]);
    }
    if (!is_callable($callable)) {
      throw new InvalidArgumentException(sprintf('The configure callable for class "%s" is not a callable.', get_class($service)));
    }
    call_user_func($callable, $service);
  }
  return $service;
}