public function ContainerBuilder::createService

Creates a service for a service definition.

@internal this method is public because of PHP 5.3 limitations, do not use it explicitly in your code

Parameters

Definition $definition A service definition instance:

string $id The service identifier:

Boolean $tryProxy Whether to try proxying the service with a lazy proxy:

Return value

object The service described by the service definition

Throws

RuntimeException When the scope is inactive

RuntimeException When the factory definition is incomplete

RuntimeException When the service is a synthetic service

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 919

Class

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

Namespace

Symfony\Component\DependencyInjection

Code

public function createService(Definition $definition, $id, $tryProxy = true) {
  if ($definition
    ->isSynthetic()) {
    throw new RuntimeException(sprintf('You have requested a synthetic service ("%s"). The DIC does not know how to construct this service.', $id));
  }
  if ($tryProxy && $definition
    ->isLazy()) {
    $container = $this;
    $proxy = $this
      ->getProxyInstantiator()
      ->instantiateProxy($container, $definition, $id, function () use ($definition, $id, $container) {
      return $container
        ->createService($definition, $id, false);
    });
    $this
      ->shareService($definition, $proxy, $id);
    return $proxy;
  }
  $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(sprintf('Cannot create service "%s" from factory method without a factory service or factory class.', $id));
    }
    $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 ($tryProxy || !$definition
    ->isLazy()) {

    // share only if proxying failed, or if not a proxy
    $this
      ->shareService($definition, $service, $id);
  }
  foreach ($definition
    ->getMethodCalls() as $call) {
    $this
      ->callMethod($service, $call);
  }
  $properties = $this
    ->resolveServices($parameterBag
    ->resolveValue($definition
    ->getProperties()));
  foreach ($properties as $name => $value) {
    $service->{$name} = $value;
  }
  if ($callable = $definition
    ->getConfigurator()) {
    if (is_array($callable)) {
      $callable[0] = $callable[0] instanceof Reference ? $this
        ->get((string) $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;
}