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

Breadcrumb

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

function PhpDumper::addServiceInstance

Throws

InvalidArgumentException

RuntimeException

1 call to PhpDumper::addServiceInstance()
PhpDumper::addInlineService in vendor/symfony/dependency-injection/Dumper/PhpDumper.php

File

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

Class

PhpDumper
PhpDumper dumps a service container as a PHP class.

Namespace

Symfony\Component\DependencyInjection\Dumper

Code

private function addServiceInstance(string $id, Definition $definition, bool $isSimpleInstance) : string {
    $class = $this->dumpValue($definition->getClass());
    if (str_starts_with($class, "'") && !str_contains($class, '$') && !preg_match('/^\'(?:\\\\{2})?[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*(?:\\\\{2}[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)*\'$/', $class)) {
        throw new InvalidArgumentException(\sprintf('"%s" is not a valid class name for the "%s" service.', $class, $id));
    }
    $asGhostObject = false;
    $isProxyCandidate = $this->isProxyCandidate($definition, $asGhostObject, $id);
    $instantiation = '';
    $lastWitherIndex = null;
    foreach ($definition->getMethodCalls() as $k => $call) {
        if ($call[2] ?? false) {
            $lastWitherIndex = $k;
        }
    }
    if (!$isProxyCandidate && $definition->isShared() && !isset($this->singleUsePrivateIds[$id]) && null === $lastWitherIndex) {
        $instantiation = \sprintf('$container->%s[%s] = %s', $this->container
            ->getDefinition($id)
            ->isPublic() ? 'services' : 'privates', $this->doExport($id), $isSimpleInstance ? '' : '$instance');
    }
    elseif (!$isSimpleInstance) {
        $instantiation = '$instance';
    }
    $return = '';
    if ($isSimpleInstance) {
        $return = 'return ';
    }
    else {
        $instantiation .= ' = ';
    }
    return $this->addNewInstance($definition, '        ' . $return . $instantiation, $id, $asGhostObject);
}
RSS feed
Powered by Drupal