Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DependencyInjection] Add support for generating lazy closures #49639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions 60 src/Symfony/Component/DependencyInjection/Argument/LazyClosure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Argument;

use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\VarExporter\ProxyHelper;

/**
* @author Nicolas Grekas <p@tchwork.com>
*
* @internal
*/
class LazyClosure
{
public readonly object $service;

public function __construct(
private \Closure $initializer,
) {
unset($this->service);
}

public function __get(mixed $name): mixed
{
if ('service' !== $name) {
throw new InvalidArgumentException(sprintf('Cannot read property "%s" from a lazy closure.', $name));
}

if (isset($this->initializer)) {
$this->service = ($this->initializer)();
unset($this->initializer);
}

return $this->service;
}

public static function getCode(string $initializer, ?\ReflectionClass $r, string $method, ?string $id): string
{
if (!$r || !$r->hasMethod($method)) {
throw new RuntimeException(sprintf('Cannot create lazy closure for service "%s" because its corresponding callable is invalid.', $id));
}

$signature = ProxyHelper::exportSignature($r->getMethod($method));
$signature = preg_replace('/: static$/', ': \\'.$r->name, $signature);

return '(new class('.$initializer.') extends \\'.self::class.' { '
.$signature.' { return $this->service->'.$method.'(...\func_get_args()); } '
.'})->'.$method.'(...)';
}
}
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CHANGELOG
* Allow to trim XML service parameters value by using `trim="true"` attribute
* Allow extending the `Autowire` attribute
* Add `#[Exclude]` to skip autoregistering a class
* Add support for generating lazy closures
* Add support for autowiring services as closures using `#[AutowireCallable]` or `#[AutowireServiceClosure]`
* Deprecate `#[MapDecorated]`, use `#[AutowireDecorated]` instead
* Deprecate the `@required` annotation, use the `Symfony\Contracts\Service\Attribute\Required` attribute instead
Expand Down
34 changes: 31 additions & 3 deletions 34 src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Config\Resource\ResourceInterface;
use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\LazyClosure;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocator;
Expand Down Expand Up @@ -1050,13 +1051,40 @@ private function createService(Definition $definition, array &$inlineServices, b
}

$parameterBag = $this->getParameterBag();
$class = ($parameterBag->resolveValue($definition->getClass()) ?: (['Closure', 'fromCallable'] === $definition->getFactory() ? 'Closure' : null));

if (true === $tryProxy && $definition->isLazy() && !$tryProxy = !($proxy = $this->proxyInstantiator ??= new LazyServiceInstantiator()) || $proxy instanceof RealServiceInstantiator) {
if ('Closure' === $class && $definition->isLazy() && ['Closure', 'fromCallable'] === $definition->getFactory()) {
$callable = $parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArgument(0)));

if ($callable instanceof Reference || $callable instanceof Definition) {
$callable = [$callable, '__invoke'];
}

if (\is_array($callable) && (
$callable[0] instanceof Reference
|| $callable[0] instanceof Definition && !isset($inlineServices[spl_object_hash($callable[0])])
)) {
$containerRef = $this->containerRef ??= \WeakReference::create($this);
$class = ($callable[0] instanceof Reference ? $this->findDefinition($callable[0]) : $callable[0])->getClass();
$initializer = static function () use ($containerRef, $callable, &$inlineServices) {
return $containerRef->get()->doResolveServices($callable[0], $inlineServices);
};

$proxy = eval('return '.LazyClosure::getCode('$initializer', $this->getReflectionClass($class), $callable[1], $id).';');
$this->shareService($definition, $proxy, $id, $inlineServices);

return $proxy;
}
}

if (true === $tryProxy && $definition->isLazy() && 'Closure' !== $class
&& !$tryProxy = !($proxy = $this->proxyInstantiator ??= new LazyServiceInstantiator()) || $proxy instanceof RealServiceInstantiator
) {
$containerRef = $this->containerRef ??= \WeakReference::create($this);
$proxy = $proxy->instantiateProxy(
$this,
(clone $definition)
->setClass($parameterBag->resolveValue($definition->getClass()))
->setClass($class)
->setTags(($definition->hasTag('proxy') ? ['proxy' => $parameterBag->resolveValue($definition->getTag('proxy'))] : []) + $definition->getTags()),
$id, static function ($proxy = false) use ($containerRef, $definition, &$inlineServices, $id) {
return $containerRef->get()->createService($definition, $inlineServices, true, $id, $proxy);
Expand Down Expand Up @@ -1105,7 +1133,7 @@ private function createService(Definition $definition, array &$inlineServices, b
}
}
} else {
$r = new \ReflectionClass($parameterBag->resolveValue($definition->getClass()));
$r = new \ReflectionClass($class);

if (\is_object($tryProxy)) {
if ($r->getConstructor()) {
Expand Down
21 changes: 21 additions & 0 deletions 21 src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\LazyClosure;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocator;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
Expand Down Expand Up @@ -1179,6 +1180,22 @@ private function addNewInstance(Definition $definition, string $return = '', str
throw new RuntimeException(sprintf('Cannot dump definition because of invalid factory method (%s).', $callable[1] ?: 'n/a'));
}

if (['...'] === $arguments && $definition->isLazy() && 'Closure' === ($definition->getClass() ?? 'Closure') && (
$callable[0] instanceof Reference
|| ($callable[0] instanceof Definition && !$this->definitionVariables->contains($callable[0]))
)) {
$class = ($callable[0] instanceof Reference ? $this->container->findDefinition($callable[0]) : $callable[0])->getClass();

if (str_contains($initializer = $this->dumpValue($callable[0]), '$container')) {
$this->addContainerRef = true;
$initializer = sprintf('function () use ($containerRef) { $container = $containerRef; return %s; }', $initializer);
} else {
$initializer = 'fn () => '.$initializer;
}

return $return.LazyClosure::getCode($initializer, $this->container->getReflectionClass($class), $callable[1], $id).$tail;
}

if ($callable[0] instanceof Reference
|| ($callable[0] instanceof Definition && $this->definitionVariables->contains($callable[0]))
) {
Expand Down Expand Up @@ -2327,6 +2344,10 @@ private function isProxyCandidate(Definition $definition, ?bool &$asGhostObject,
{
$asGhostObject = false;

if ('Closure' === ($definition->getClass() ?: (['Closure', 'fromCallable'] === $definition->getFactory() ? 'Closure' : null))) {
return null;
}

if (!$definition->isLazy() || !$this->hasProxyDumper) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1985,6 +1985,25 @@ public function testNamedArgumentBeforeCompile()

$this->assertSame(1, $e->first);
}

public function testLazyClosure()
{
$container = new ContainerBuilder();
$container->register('closure', 'Closure')
->setPublic('true')
->setFactory(['Closure', 'fromCallable'])
->setLazy(true)
->setArguments([[new Reference('foo'), 'cloneFoo']]);
$container->register('foo', Foo::class);
$container->compile();

$cloned = Foo::$counter;
$this->assertInstanceOf(\Closure::class, $container->get('closure'));
$this->assertSame($cloned, Foo::$counter);
$this->assertInstanceOf(Foo::class, $container->get('closure')());
$this->assertSame(1 + $cloned, Foo::$counter);
$this->assertSame(1, (new \ReflectionFunction($container->get('closure')))->getNumberOfParameters());
}
}

class FooClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,32 @@ public function testAutowireClosure()
$this->assertInstanceOf(Foo::class, $fooClone = ($bar->buz)());
$this->assertNotSame($container->get('foo'), $fooClone);
}

public function testLazyClosure()
{
$container = new ContainerBuilder();
$container->register('closure', 'Closure')
->setPublic('true')
->setFactory(['Closure', 'fromCallable'])
->setLazy(true)
->setArguments([[new Reference('foo'), 'cloneFoo']]);
$container->register('foo', Foo::class);
$container->compile();
$dumper = new PhpDumper($container);

$this->assertStringEqualsFile(self::$fixturesPath.'/php/lazy_closure.php', $dumper->dump(['class' => 'Symfony_DI_PhpDumper_Test_Lazy_Closure']));

require self::$fixturesPath.'/php/lazy_closure.php';

$container = new \Symfony_DI_PhpDumper_Test_Lazy_Closure();

$cloned = Foo::$counter;
$this->assertInstanceOf(\Closure::class, $container->get('closure'));
$this->assertSame($cloned, Foo::$counter);
$this->assertInstanceOf(Foo::class, $container->get('closure')());
$this->assertSame(1 + $cloned, Foo::$counter);
$this->assertSame(1, (new \ReflectionFunction($container->get('closure')))->getNumberOfParameters());
}
}

class Rot13EnvVarProcessor implements EnvVarProcessorInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ public function cloneFoo(): static

class Foo
{
public static int $counter = 0;

#[Required]
public function cloneFoo(): static
public function cloneFoo(\stdClass $bar = null): static
{
++self::$counter;

return clone $this;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

/**
* @internal This class has been auto-generated by the Symfony Dependency Injection Component.
*/
class Symfony_DI_PhpDumper_Test_Lazy_Closure extends Container
{
protected $parameters = [];
protected readonly \WeakReference $ref;

public function __construct()
{
$this->ref = \WeakReference::create($this);
$this->services = $this->privates = [];
$this->methodMap = [
'closure' => 'getClosureService',
];

$this->aliases = [];
}

public function compile(): void
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}

public function isCompiled(): bool
{
return true;
}

public function getRemovedIds(): array
{
return [
'foo' => true,
];
}

protected function createProxy($class, \Closure $factory)
{
return $factory();
}

/**
* Gets the public 'closure' shared service.
*
* @return \Closure
*/
protected static function getClosureService($container, $lazyLoad = true)
{
return $container->services['closure'] = (new class(fn () => new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo()) extends \Symfony\Component\DependencyInjection\Argument\LazyClosure { public function cloneFoo(?\stdClass $bar = null): \Symfony\Component\DependencyInjection\Tests\Compiler\Foo { return $this->service->cloneFoo(...\func_get_args()); } })->cloneFoo(...);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.