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

Breadcrumb

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

function LazyServiceDumper::getProxyCode

Overrides DumperInterface::getProxyCode

File

vendor/symfony/dependency-injection/LazyProxy/PhpDumper/LazyServiceDumper.php, line 99

Class

LazyServiceDumper
@author Nicolas Grekas <p@tchwork.com>

Namespace

Symfony\Component\DependencyInjection\LazyProxy\PhpDumper

Code

public function getProxyCode(Definition $definition, ?string $id = null) : string {
    if (!$this->isProxyCandidate($definition, $asGhostObject, $id)) {
        throw new InvalidArgumentException(\sprintf('Cannot instantiate lazy proxy for service "%s".', $id ?? $definition->getClass()));
    }
    $proxyClass = $this->getProxyClass($definition, $asGhostObject, $class);
    if ($asGhostObject) {
        try {
            return ($class?->isReadOnly() ? 'readonly ' : '') . 'class ' . $proxyClass . ProxyHelper::generateLazyGhost($class);
        } catch (LogicException $e) {
            throw new InvalidArgumentException(\sprintf('Cannot generate lazy ghost for service "%s".', $id ?? $definition->getClass()), 0, $e);
        }
    }
    $interfaces = [];
    if ($definition->hasTag('proxy')) {
        foreach ($definition->getTag('proxy') as $tag) {
            if (!isset($tag['interface'])) {
                throw new InvalidArgumentException(\sprintf('Invalid definition for service "%s": the "interface" attribute is missing on a "proxy" tag.', $id ?? $definition->getClass()));
            }
            if (!interface_exists($tag['interface']) && !class_exists($tag['interface'], false)) {
                throw new InvalidArgumentException(\sprintf('Invalid definition for service "%s": several "proxy" tags found but "%s" is not an interface.', $id ?? $definition->getClass(), $tag['interface']));
            }
            if ('object' !== $definition->getClass() && !is_a($class->name, $tag['interface'], true)) {
                throw new InvalidArgumentException(\sprintf('Invalid "proxy" tag for service "%s": class "%s" doesn\'t implement "%s".', $id ?? $definition->getClass(), $definition->getClass(), $tag['interface']));
            }
            $interfaces[] = new \ReflectionClass($tag['interface']);
        }
        $class = 1 === \count($interfaces) && !$interfaces[0]->isInterface() ? array_pop($interfaces) : null;
    }
    elseif ($class->isInterface()) {
        $interfaces = [
            $class,
        ];
        $class = null;
    }
    try {
        return ($class?->isReadOnly() ? 'readonly ' : '') . 'class ' . $proxyClass . ProxyHelper::generateLazyProxy($class, $interfaces);
    } catch (LogicException $e) {
        throw new InvalidArgumentException(\sprintf('Cannot generate lazy proxy for service "%s".', $id ?? $definition->getClass()), 0, $e);
    }
}
RSS feed
Powered by Drupal