Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DependencyInjection] Optimize out "current()" when it's used as service factory #49523

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
Feb 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,9 @@ private function addNewInstance(Definition $definition, string $return = '', str
if (null !== $definition->getFactory()) {
$callable = $definition->getFactory();

if ('current' === $callable && [0] === array_keys($definition->getArguments()) && \is_array($value) && [0] === array_keys($value)) {
return $return.$this->dumpValue($value[0]).$tail;
}
if (['Closure', 'fromCallable'] === $callable && [0] === array_keys($definition->getArguments())) {
$callable = $definition->getArgument(0);
if ($callable instanceof ServiceClosureArgument) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,37 @@ public function testWitherWithStaticReturnType()
$this->assertInstanceOf(Foo::class, $wither->foo);
}

public function testCurrentFactoryInlining()
{
$container = new ContainerBuilder();
$container->register(Foo::class);

$container
->register('inlined_current', Foo::class)
->setFactory('current')
->setPublic(true)
->setArguments([[new Reference(Foo::class)]]);

$container
->register('not_inlined_current', Foo::class)
->setFactory('current')
->setPublic(true)
->setArguments([[new Reference(Foo::class), 123]]);

$container->compile();
$dumper = new PhpDumper($container);
$dump = $dumper->dump(['class' => 'Symfony_DI_PhpDumper_Service_CurrentFactoryInlining']);
file_put_contents(self::$fixturesPath.'/php/services_current_factory_inlining.php', $dump);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas I think this was left here by mistake.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, fixed in 29054b1

$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_current_factory_inlining.php', $dump);
eval('?>'.$dump);

$container = new \Symfony_DI_PhpDumper_Service_CurrentFactoryInlining();

$foo = $container->get('inlined_current');
$this->assertInstanceOf(Foo::class, $foo);
$this->assertSame($foo, $container->get('not_inlined_current'));
}

public function testDumpServiceWithAbstractArgument()
{
$this->expectException(RuntimeException::class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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_Service_CurrentFactoryInlining extends Container
{
protected $parameters = [];
protected readonly \WeakReference $ref;

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

$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 [
'Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo' => true,
];
}

/**
* Gets the public 'inlined_current' shared service.
*
* @return \Symfony\Component\DependencyInjection\Tests\Compiler\Foo
*/
protected static function getInlinedCurrentService($container)
{
return $container->services['inlined_current'] = ($container->privates['Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo'] ??= new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo());
}

/**
* Gets the public 'not_inlined_current' shared service.
*
* @return \Symfony\Component\DependencyInjection\Tests\Compiler\Foo
*/
protected static function getNotInlinedCurrentService($container)
{
return $container->services['not_inlined_current'] = \current([($container->privates['Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo'] ??= new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo()), 123]);
}
}