You are here

private function PhpDumper::addServiceInstance in Service Container 7

Same name and namespace in other branches
  1. 7.2 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php \Symfony\Component\DependencyInjection\Dumper\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 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Adds a service.

File

modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php, line 373

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