private function PhpDumper::addServiceInstance

Generates the service instance.

Parameters

string $id:

Definition $definition:

Return value

string

Throws

InvalidArgumentException

RuntimeException

1 call to PhpDumper::addServiceInstance()
PhpDumper::addService in drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Adds a service

File

drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php, line 328

Class

PhpDumper
PhpDumper dumps a service container as a PHP class.

Namespace

Symfony\Component\DependencyInjection\Dumper

Code

private function addServiceInstance($id, $definition) {
  $class = $this
    ->dumpValue($definition
    ->getClass());
  if (0 === strpos($class, "'") && !preg_match('/^\'[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*(\\\\{2}[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)*\'$/', $class)) {
    throw new InvalidArgumentException(sprintf('"%s" is not a valid class name for the "%s" service.', $class, $id));
  }
  $simple = $this
    ->isSimpleInstance($id, $definition);
  $isProxyCandidate = $this
    ->getProxyDumper()
    ->isProxyCandidate($definition);
  $instantiation = '';
  if (!$isProxyCandidate && ContainerInterface::SCOPE_CONTAINER === $definition
    ->getScope()) {
    $instantiation = "\$this->services['{$id}'] = " . ($simple ? '' : '$instance');
  }
  elseif (!$isProxyCandidate && ContainerInterface::SCOPE_PROTOTYPE !== ($scope = $definition
    ->getScope())) {
    $instantiation = "\$this->services['{$id}'] = \$this->scopedServices['{$scope}']['{$id}'] = " . ($simple ? '' : '$instance');
  }
  elseif (!$simple) {
    $instantiation = '$instance';
  }
  $return = '';
  if ($simple) {
    $return = 'return ';
  }
  else {
    $instantiation .= ' = ';
  }
  $code = $this
    ->addNewInstance($id, $definition, $return, $instantiation);
  if (!$simple) {
    $code .= "\n";
  }
  return $code;
}