W3cubDocs

/Drupal 8

public function ContainerBuilder::createService

public ContainerBuilder::createService(Definition $definition, $id, $tryProxy = true)

Creates a service for a service definition.

Overrides the parent implementation, but just changes one line about deprecations, see below.

Parameters

\Symfony\Component\DependencyInjection\Definition $definition:

string $id:

bool|true $tryProxy:

Return value

mixed|object

Overrides ContainerBuilder::createService

File

core/lib/Drupal/Core/DependencyInjection/ContainerBuilder.php, line 50

Class

ContainerBuilder
Drupal's dependency injection container builder.

Namespace

Drupal\Core\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 ($definition->isDeprecated()) {
    // Suppress deprecation warnings when a service is marked as
    // 'deprecated: %service_id%-no-warning'
    if ($definition->getDeprecationMessage($id) != ($id . '-no-warning')) {
      @trigger_error($definition->getDeprecationMessage($id), E_USER_DEPRECATED);
    }
  }

  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);

    if (!$definition->isDeprecated() && is_array($factory) && is_string($factory[0])) {
      $r = new \ReflectionClass($factory[0]);

      if (0 < strpos($r->getDocComment(), "\n * @deprecated ")) {
        @trigger_error(sprintf('The "%s" service relies on the deprecated "%s" factory class. It should either be deprecated or its factory upgraded.', $id, $r->name), E_USER_DEPRECATED);
      }
    }
  }
  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 (!$definition->isDeprecated() && 0 < strpos($r->getDocComment(), "\n * @deprecated ")) {
      // Skip deprecation notices for deprecations which opt out.
      @trigger_error(sprintf('The "%s" service relies on the deprecated "%s" class. It should either be deprecated or its implementation upgraded.', $id, $r->name), E_USER_DEPRECATED);
    }
  }

  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->unescapeValue($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;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!DependencyInjection!ContainerBuilder.php/function/ContainerBuilder::createService/8.1.x