private function PhpDumper::addServiceInlinedDefinitions

Generates the inline definition of a service.

Parameters

string $id:

Definition $definition:

Return value

string

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 190

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 || count($sDefinition
      ->getMethodCalls()) > 0 || $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,
        ));
      }
      $arguments = array();
      foreach ($sDefinition
        ->getArguments() as $argument) {
        $arguments[] = $this
          ->dumpValue($argument);
      }
      if (null !== $sDefinition
        ->getFactoryMethod()) {
        if (null !== $sDefinition
          ->getFactoryClass()) {
          $code .= sprintf("        \$%s = call_user_func(array(%s, '%s')%s);\n", $name, $this
            ->dumpValue($sDefinition
            ->getFactoryClass()), $sDefinition
            ->getFactoryMethod(), count($arguments) > 0 ? ', ' . implode(', ', $arguments) : '');
        }
        elseif (null !== $sDefinition
          ->getFactoryService()) {
          $code .= sprintf("        \$%s = %s->%s(%s);\n", $name, $this
            ->getServiceCall($sDefinition
            ->getFactoryService()), $sDefinition
            ->getFactoryMethod(), implode(', ', $arguments));
        }
        else {
          throw new RuntimeException('Factory service or factory class must be defined in service definition for ' . $id);
        }
      }
      elseif (false !== strpos($class, '$')) {
        $code .= sprintf("        \$class = %s;\n        \$%s = new \$class(%s);\n", $class, $name, implode(', ', $arguments));
      }
      else {
        $code .= sprintf("        \$%s = new \\%s(%s);\n", $name, substr(str_replace('\\\\', '\\', $class), 1, -1), implode(', ', $arguments));
      }
      if (!$this
        ->hasReference($id, $sDefinition
        ->getMethodCalls()) && !$this
        ->hasReference($id, $sDefinition
        ->getProperties())) {
        $code .= $this
          ->addServiceMethodCalls(null, $sDefinition, $name);
        $code .= $this
          ->addServiceProperties(null, $sDefinition, $name);
        $code .= $this
          ->addServiceConfigurator(null, $sDefinition, $name);
      }
      $code .= "\n";
    }
  }
  return $code;
}