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

Breadcrumb

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

class LazyServiceDumper

@author Nicolas Grekas <p@tchwork.com>

Hierarchy

  • class \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\LazyServiceDumper implements \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface

Expanded class hierarchy of LazyServiceDumper

2 files declare their use of LazyServiceDumper
LazyServiceInstantiator.php in vendor/symfony/dependency-injection/LazyProxy/Instantiator/LazyServiceInstantiator.php
PhpDumper.php in vendor/symfony/dependency-injection/Dumper/PhpDumper.php

File

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

Namespace

Symfony\Component\DependencyInjection\LazyProxy\PhpDumper
View source
final class LazyServiceDumper implements DumperInterface {
    public function __construct(string $salt = '') {
    }
    public function isProxyCandidate(Definition $definition, ?bool &$asGhostObject = null, ?string $id = null) : bool {
        $asGhostObject = false;
        if ($definition->hasTag('proxy')) {
            if (!$definition->isLazy()) {
                throw new InvalidArgumentException(\sprintf('Invalid definition for service "%s": setting the "proxy" tag on a service requires it to be "lazy".', $id ?? $definition->getClass()));
            }
            return true;
        }
        if (!$definition->isLazy()) {
            return false;
        }
        if (!($class = $definition->getClass()) || !(class_exists($class) || interface_exists($class, false))) {
            return false;
        }
        if ($definition->getFactory()) {
            return true;
        }
        foreach ($definition->getMethodCalls() as $call) {
            if ($call[2] ?? false) {
                return true;
            }
        }
        try {
            $asGhostObject = (bool) ProxyHelper::generateLazyGhost(new \ReflectionClass($class));
        } catch (LogicException) {
        }
        return true;
    }
    public function getProxyFactoryCode(Definition $definition, string $id, string $factoryCode) : string {
        $instantiation = 'return';
        if ($definition->isShared()) {
            $instantiation .= \sprintf(' $container->%s[%s] =', $definition->isPublic() && !$definition->isPrivate() ? 'services' : 'privates', var_export($id, true));
        }
        $asGhostObject = str_contains($factoryCode, '$proxy');
        $proxyClass = $this->getProxyClass($definition, $asGhostObject);
        if (!$asGhostObject) {
            return <<<EOF
        if (true === \$lazyLoad) {
            {<span class="php-variable">$instantiation</span>} \$container->createProxy('{<span class="php-variable">$proxyClass</span>}', static fn () => \\{<span class="php-variable">$proxyClass</span>}::createLazyProxy(static fn () => {<span class="php-variable">$factoryCode</span>}));
        }


EOF;
        }
        $factoryCode = \sprintf('static fn ($proxy) => %s', $factoryCode);
        return <<<EOF
        if (true === \$lazyLoad) {
            {<span class="php-variable">$instantiation</span>} \$container->createProxy('{<span class="php-variable">$proxyClass</span>}', static fn () => \\{<span class="php-variable">$proxyClass</span>}::createLazyGhost({<span class="php-variable">$factoryCode</span>}));
        }


EOF;
    }
    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);
        }
    }
    public function getProxyClass(Definition $definition, bool $asGhostObject, ?\ReflectionClass &$class = null) : string {
        $class = 'object' !== $definition->getClass() ? $definition->getClass() : 'stdClass';
        $class = new \ReflectionClass($class);
        return preg_replace('/^.*\\\\/', '', $definition->getClass()) . ($asGhostObject ? 'Ghost' : 'Proxy') . ucfirst(substr(hash('xxh128', $this->salt . '+' . $class->name . '+' . serialize($definition->getTag('proxy'))), -7));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
LazyServiceDumper::getProxyClass public function
LazyServiceDumper::getProxyCode public function Generates the code for the lazy proxy. Overrides DumperInterface::getProxyCode
LazyServiceDumper::getProxyFactoryCode public function Generates the code to be used to instantiate a proxy in the dumped factory code. Overrides DumperInterface::getProxyFactoryCode
LazyServiceDumper::isProxyCandidate public function Inspects whether the given definitions should produce proxy instantiation logic in the dumped container. Overrides DumperInterface::isProxyCandidate
LazyServiceDumper::__construct public function
RSS feed
Powered by Drupal