Skip to content

[DependencyInjection] Fix dumping non-shared factories with TaggedIteratorArgument #50262

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
May 12, 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 @@ -945,8 +945,9 @@ protected static function {$methodName}(\$container$lazyInitialization)
if (!$isProxyCandidate && !$definition->isShared()) {
$c = implode("\n", array_map(fn ($line) => $line ? ' '.$line : $line, explode("\n", $c)));
$lazyloadInitialization = $definition->isLazy() ? ', $lazyLoad = true' : '';
$useContainerRef = $this->addContainerRef ? ' use ($containerRef)' : '';

$c = sprintf(" %s = function (\$container%s) {\n%s };\n\n return %1\$s(\$container);\n", $factory, $lazyloadInitialization, $c);
$c = sprintf(" %s = function (\$container%s)%s {\n%s };\n\n return %1\$s(\$container);\n", $factory, $lazyloadInitialization, $useContainerRef, $c);
}

$code .= $c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocator as ArgumentServiceLocator;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\AutowireCallable;
use Symfony\Component\DependencyInjection\Attribute\AutowireServiceClosure;
Expand Down Expand Up @@ -285,6 +286,35 @@ public function testDumpAsFilesWithFactoriesInlined()
$this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services9_inlined_factories.txt', $dump);
}

public function testDumpAsFilesWithFactoriesInlinedWithTaggedIterator()
{
$container = new ContainerBuilder();
$container
->register('foo', FooClass::class)
->addMethodCall('setOtherInstances', [new TaggedIteratorArgument('foo')])
->setShared(false)
->setPublic(true);

$container
->register('Bar', 'Bar')
->addTag('foo');

$container
->register('stdClass', '\stdClass')
->addTag('foo');

$container->compile();

$dumper = new PhpDumper($container);
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot', 'build_time' => 1563381341, 'inline_factories' => true, 'inline_class_loader' => true]), true);

if ('\\' === \DIRECTORY_SEPARATOR) {
$dump = str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump);
}

$this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services9_inlined_factories_with_tagged_iterrator.txt', $dump);
}

public function testDumpAsFilesWithLazyFactoriesInlined()
{
$container = new ContainerBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class FooClass
public $qux;
public $foo;
public $moo;
public $otherInstances;

public $bar = null;
public $initialized = false;
Expand Down Expand Up @@ -41,4 +42,9 @@ public function setBar($value = null)
{
$this->bar = $value;
}

public function setOtherInstances($otherInstances)
{
$this->otherInstances = $otherInstances;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
Array
(
[Container%s/removed-ids.php] => <?php

namespace Container%s;

return [
'Bar' => true,
'stdClass' => true,
];

[Container%s/ProjectServiceContainer.php] => <?php

namespace Container%s;

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 ProjectServiceContainer extends Container
{
protected $targetDir;
protected $parameters = [];
protected readonly \WeakReference $ref;

public function __construct(private array $buildParameters = [], protected string $containerDir = __DIR__)
{
$this->ref = \WeakReference::create($this);
$this->targetDir = \dirname($containerDir);
$this->services = $this->privates = [];
$this->methodMap = [
'foo' => 'getFooService',
];

$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 require $this->containerDir.\DIRECTORY_SEPARATOR.'removed-ids.php';
}

/**
* Gets the public 'foo' service.
*
* @return \Symfony\Component\DependencyInjection\Tests\Dumper\FooClass
*/
protected static function getFooService($container)
{
$containerRef = $container->ref;

$container->factories['foo'] = function ($container) use ($containerRef) {
$instance = new \Symfony\Component\DependencyInjection\Tests\Dumper\FooClass();

$instance->setOtherInstances(new RewindableGenerator(function () use ($containerRef) {
$container = $containerRef->get();

yield 0 => ($container->privates['Bar'] ??= new \Bar());
yield 1 => ($container->privates['stdClass'] ??= new \stdClass());
}, 2));

return $instance;
};

return $container->factories['foo']($container);
}
}

[ProjectServiceContainer.preload.php] => <?php

// This file has been auto-generated by the Symfony Dependency Injection Component
// You can reference it in the "opcache.preload" php.ini setting on PHP >= 7.4 when preloading is desired

use Symfony\Component\DependencyInjection\Dumper\Preloader;

if (in_array(PHP_SAPI, ['cli', 'phpdbg'], true)) {
return;
}

require dirname(__DIR__, %d).'/vendor/autoload.php';
(require __DIR__.'/ProjectServiceContainer.php')->set(\Container%s\ProjectServiceContainer::class, null);

$classes = [];
$classes[] = 'Bar';
$classes[] = 'Symfony\Component\DependencyInjection\Tests\Dumper\FooClass';
$classes[] = 'Symfony\Component\DependencyInjection\ContainerInterface';

$preloaded = Preloader::preload($classes);

[ProjectServiceContainer.php] => <?php

// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.

if (\class_exists(\Container%s\ProjectServiceContainer::class, false)) {
// no-op
} elseif (!include __DIR__.'/Container%s/ProjectServiceContainer.php') {
touch(__DIR__.'/Container%s.legacy');

return;
}

if (!\class_exists(ProjectServiceContainer::class, false)) {
\class_alias(\Container%s\ProjectServiceContainer::class, ProjectServiceContainer::class, false);
}

return new \Container%s\ProjectServiceContainer([
'container.build_hash' => '%s',
'container.build_id' => '3f6e2bc2',
'container.build_time' => 1563381341,
], __DIR__.\DIRECTORY_SEPARATOR.'Container%s');

)