You are here

public function ContainerBuilder::createService in Service Container 7

Same name and namespace in other branches
  1. 7.2 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/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 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/ContainerBuilder.php
Gets a service.
ContainerBuilder::resolveServices in modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/ContainerBuilder.php
Replaces service references by the real service instance and evaluates expressions.

File

modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/ContainerBuilder.php, line 914

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] = $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;
}