You are here

private function PhpDumper::addServiceInlinedDefinitions in Service Container 7.2

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

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