private function PhpDumper::addServiceInlinedDefinitions

Generates the inline definition of a service.

Parameters

string $id:

Definition $definition:

Return value

string

Throws

RuntimeException When the factory definition is incomplete

ServiceCircularReferenceException When a circular reference is detected

1 call to PhpDumper::addServiceInlinedDefinitions()
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 246

Class

PhpDumper
PhpDumper dumps a service container as a PHP class.

Namespace

Symfony\Component\DependencyInjection\Dumper

Code

private function addServiceInlinedDefinitions($id, $definition) {
  $code = '';
  $variableMap = $this->definitionVariables;
  $nbOccurrences = new \SplObjectStorage();
  $processed = new \SplObjectStorage();
  $inlinedDefinitions = $this
    ->getInlinedDefinitions($definition);
  foreach ($inlinedDefinitions as $definition) {
    if (false === $nbOccurrences
      ->contains($definition)) {
      $nbOccurrences
        ->offsetSet($definition, 1);
    }
    else {
      $i = $nbOccurrences
        ->offsetGet($definition);
      $nbOccurrences
        ->offsetSet($definition, $i + 1);
    }
  }
  foreach ($inlinedDefinitions as $sDefinition) {
    if ($processed
      ->contains($sDefinition)) {
      continue;
    }
    $processed
      ->offsetSet($sDefinition);
    $class = $this
      ->dumpValue($sDefinition
      ->getClass());
    if ($nbOccurrences
      ->offsetGet($sDefinition) > 1 || $sDefinition
      ->getMethodCalls() || $sDefinition
      ->getProperties() || null !== $sDefinition
      ->getConfigurator() || false !== strpos($class, '$')) {
      $name = $this
        ->getNextVariableName();
      $variableMap
        ->offsetSet($sDefinition, new Variable($name));

      // a construct like:
      // $a = new ServiceA(ServiceB $b); $b = new ServiceB(ServiceA $a);
      // this is an indication for a wrong implementation, you can circumvent this problem
      // by setting up your service structure like this:
      // $b = new ServiceB();
      // $a = new ServiceA(ServiceB $b);
      // $b->setServiceA(ServiceA $a);
      if ($this
        ->hasReference($id, $sDefinition
        ->getArguments())) {
        throw new ServiceCircularReferenceException($id, array(
          $id,
        ));
      }
      $code .= $this
        ->addNewInstance($id, $sDefinition, '$' . $name, ' = ');
      if (!$this
        ->hasReference($id, $sDefinition
        ->getMethodCalls(), true) && !$this
        ->hasReference($id, $sDefinition
        ->getProperties(), true)) {
        $code .= $this
          ->addServiceMethodCalls(null, $sDefinition, $name);
        $code .= $this
          ->addServiceProperties(null, $sDefinition, $name);
        $code .= $this
          ->addServiceConfigurator(null, $sDefinition, $name);
      }
      $code .= "\n";
    }
  }
  return $code;
}