Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. PhpDumper.php

function PhpDumper::getServiceCall

3 calls to PhpDumper::getServiceCall()
PhpDumper::addInlineReference in vendor/symfony/dependency-injection/Dumper/PhpDumper.php
PhpDumper::dumpValue in vendor/symfony/dependency-injection/Dumper/PhpDumper.php
PhpDumper::getExpressionLanguage in vendor/symfony/dependency-injection/Dumper/PhpDumper.php

File

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

Class

PhpDumper
PhpDumper dumps a service container as a PHP class.

Namespace

Symfony\Component\DependencyInjection\Dumper

Code

private function getServiceCall(string $id, ?Reference $reference = null) : string {
    while ($this->container
        ->hasAlias($id)) {
        $id = (string) $this->container
            ->getAlias($id);
    }
    if ('service_container' === $id) {
        return '$container';
    }
    if ($this->container
        ->hasDefinition($id) && ($definition = $this->container
        ->getDefinition($id))) {
        if ($definition->isSynthetic()) {
            $code = \sprintf('$container->get(%s%s)', $this->doExport($id), null !== $reference ? ', ' . $reference->getInvalidBehavior() : '');
        }
        elseif (null !== $reference && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $reference->getInvalidBehavior()) {
            $code = 'null';
            if (!$definition->isShared()) {
                return $code;
            }
        }
        elseif ($this->isTrivialInstance($definition)) {
            if ($definition->hasErrors() && ($e = $definition->getErrors())) {
                return \sprintf('throw new RuntimeException(%s)', $this->export(reset($e)));
            }
            $code = $this->addNewInstance($definition, '', $id);
            if ($definition->isShared() && !isset($this->singleUsePrivateIds[$id])) {
                return \sprintf('($container->%s[%s] ??= %s)', $definition->isPublic() ? 'services' : 'privates', $this->doExport($id), $code);
            }
            $code = "({$code})";
        }
        else {
            $code = $this->asFiles && !$this->inlineFactories && !$this->isHotPath($definition) ? "\$container->load('%s')" : 'self::%s($container)';
            $code = \sprintf($code, $this->generateMethodName($id));
            if (!$definition->isShared()) {
                $factory = \sprintf('$container->factories%s[%s]', $definition->isPublic() ? '' : "['service_container']", $this->doExport($id));
                $code = \sprintf('(isset(%s) ? %1$s($container) : %s)', $factory, $code);
            }
        }
        if ($definition->isShared() && !isset($this->singleUsePrivateIds[$id])) {
            $code = \sprintf('($container->%s[%s] ?? %s)', $definition->isPublic() ? 'services' : 'privates', $this->doExport($id), $code);
        }
        return $code;
    }
    if (null !== $reference && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $reference->getInvalidBehavior()) {
        return 'null';
    }
    if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE < $reference->getInvalidBehavior()) {
        $code = \sprintf('$container->get(%s, ContainerInterface::NULL_ON_INVALID_REFERENCE)', $this->doExport($id));
    }
    else {
        $code = \sprintf('$container->get(%s)', $this->doExport($id));
    }
    return \sprintf('($container->services[%s] ?? %s)', $this->doExport($id), $code);
}
RSS feed
Powered by Drupal