You are here

private function PhpDumper::addNewInstance in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/dependency-injection/Dumper/PhpDumper.php \Symfony\Component\DependencyInjection\Dumper\PhpDumper::addNewInstance()
2 calls to PhpDumper::addNewInstance()
PhpDumper::addServiceInlinedDefinitions in vendor/symfony/dependency-injection/Dumper/PhpDumper.php
Generates the inline definition of a service.
PhpDumper::addServiceInstance in vendor/symfony/dependency-injection/Dumper/PhpDumper.php
Generates the service instance.

File

vendor/symfony/dependency-injection/Dumper/PhpDumper.php, line 750

Class

PhpDumper
PhpDumper dumps a service container as a PHP class.

Namespace

Symfony\Component\DependencyInjection\Dumper

Code

private function addNewInstance($id, Definition $definition, $return, $instantiation) {
  $class = $this
    ->dumpValue($definition
    ->getClass());
  $arguments = array();
  foreach ($definition
    ->getArguments() as $value) {
    $arguments[] = $this
      ->dumpValue($value);
  }
  if (null !== $definition
    ->getFactory()) {
    $callable = $definition
      ->getFactory();
    if (is_array($callable)) {
      if ($callable[0] instanceof Reference || $callable[0] instanceof Definition && $this->definitionVariables
        ->contains($callable[0])) {
        return sprintf("        {$return}{$instantiation}%s->%s(%s);\n", $this
          ->dumpValue($callable[0]), $callable[1], $arguments ? implode(', ', $arguments) : '');
      }
      $class = $this
        ->dumpValue($callable[0]);

      // If the class is a string we can optimize call_user_func away
      if (strpos($class, "'") === 0) {
        return sprintf("        {$return}{$instantiation}%s::%s(%s);\n", $this
          ->dumpLiteralClass($class), $callable[1], $arguments ? implode(', ', $arguments) : '');
      }
      return sprintf("        {$return}{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this
        ->dumpValue($callable[0]), $callable[1], $arguments ? ', ' . implode(', ', $arguments) : '');
    }
    return sprintf("        {$return}{$instantiation}\\%s(%s);\n", $callable, $arguments ? implode(', ', $arguments) : '');
  }
  elseif (null !== $definition
    ->getFactoryMethod(false)) {
    if (null !== $definition
      ->getFactoryClass(false)) {
      $class = $this
        ->dumpValue($definition
        ->getFactoryClass(false));

      // If the class is a string we can optimize call_user_func away
      if (strpos($class, "'") === 0) {
        return sprintf("        {$return}{$instantiation}%s::%s(%s);\n", $this
          ->dumpLiteralClass($class), $definition
          ->getFactoryMethod(false), $arguments ? implode(', ', $arguments) : '');
      }
      return sprintf("        {$return}{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this
        ->dumpValue($definition
        ->getFactoryClass(false)), $definition
        ->getFactoryMethod(false), $arguments ? ', ' . implode(', ', $arguments) : '');
    }
    if (null !== $definition
      ->getFactoryService(false)) {
      return sprintf("        {$return}{$instantiation}%s->%s(%s);\n", $this
        ->getServiceCall($definition
        ->getFactoryService(false)), $definition
        ->getFactoryMethod(false), implode(', ', $arguments));
    }
    throw new RuntimeException(sprintf('Factory method requires a factory service or factory class in service definition for %s', $id));
  }
  if (false !== strpos($class, '$')) {
    return sprintf("        \$class = %s;\n\n        {$return}{$instantiation}new \$class(%s);\n", $class, implode(', ', $arguments));
  }
  return sprintf("        {$return}{$instantiation}new %s(%s);\n", $this
    ->dumpLiteralClass($class), implode(', ', $arguments));
}