You are here

public function ContainerBuilder::createService in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/dependency-injection/ContainerBuilder.php \Symfony\Component\DependencyInjection\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:

bool $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 vendor/symfony/dependency-injection/ContainerBuilder.php
Gets a service.
ContainerBuilder::resolveServices in vendor/symfony/dependency-injection/ContainerBuilder.php
Replaces service references by the real service instance and evaluates expressions.

File

vendor/symfony/dependency-injection/ContainerBuilder.php, line 840

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 !== ($factory = $definition
    ->getFactory())) {
    if (is_array($factory)) {
      $factory = array(
        $this
          ->resolveServices($parameterBag
          ->resolveValue($factory[0])),
        $factory[1],
      );
    }
    elseif (!is_string($factory)) {
      throw new RuntimeException(sprintf('Cannot create service "%s" because of invalid factory', $id));
    }
    $service = call_user_func_array($factory, $arguments);
  }
  elseif (null !== $definition
    ->getFactoryMethod(false)) {
    if (null !== $definition
      ->getFactoryClass(false)) {
      $factory = $parameterBag
        ->resolveValue($definition
        ->getFactoryClass(false));
    }
    elseif (null !== $definition
      ->getFactoryService(false)) {
      $factory = $this
        ->get($parameterBag
        ->resolveValue($definition
        ->getFactoryService(false)));
    }
    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(false),
    ), $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] = $parameterBag
        ->resolveValue($callable[0]);
      if ($callable[0] instanceof Reference) {
        $callable[0] = $this
          ->get((string) $callable[0], $callable[0]
          ->getInvalidBehavior());
      }
      elseif ($callable[0] instanceof Definition) {
        $callable[0] = $this
          ->createService($callable[0], null);
      }
    }
    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;
}