Skip to content

[DoctrineBridge] Fix resetting the manager when using native lazy objects #60535

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 25, 2025
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
42 changes: 28 additions & 14 deletions src/Symfony/Bridge/Doctrine/ManagerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,35 @@ function (&$wrappedInstance, LazyLoadingInterface $manager) use ($name) {
return;
}

try {
$r->resetAsLazyProxy($manager, \Closure::bind(
function () use ($name) {
$name = $this->aliases[$name] ?? $name;
$asProxy = $r->initializeLazyObject($manager) !== $manager;
$initializer = \Closure::bind(
function ($manager) use ($name, $asProxy) {
$name = $this->aliases[$name] ?? $name;
if ($asProxy) {
$manager = false;
}

$manager = match (true) {
isset($this->fileMap[$name]) => $this->load($this->fileMap[$name], $manager),
!$method = $this->methodMap[$name] ?? null => throw new \LogicException(\sprintf('The "%s" service is synthetic and cannot be reset.', $name)),
(new \ReflectionMethod($this, $method))->isStatic() => $this->{$method}($this, $manager),
default => $this->{$method}($manager),
};

if ($asProxy) {
return $manager;
}
},
$this->container,
Container::class
);

return match (true) {
isset($this->fileMap[$name]) => $this->load($this->fileMap[$name], false),
!$method = $this->methodMap[$name] ?? null => throw new \LogicException(\sprintf('The "%s" service is synthetic and cannot be reset.', $name)),
(new \ReflectionMethod($this, $method))->isStatic() => $this->{$method}($this, false),
default => $this->{$method}(false),
};
},
$this->container,
Container::class
));
try {
if ($asProxy) {
$r->resetAsLazyProxy($manager, $initializer);
} else {
$r->resetAsLazyGhost($manager, $initializer);
}
} catch (\Error $e) {
if (__FILE__ !== $e->getFile()) {
throw $e;
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Fixtures/DummyManager.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?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\Bridge\Doctrine\Tests\Fixtures;

use Doctrine\Persistence\Mapping\ClassMetadata;
Expand All @@ -11,6 +20,10 @@ class DummyManager implements ObjectManager
{
public $bar;

public function __construct()
{
}

public function find($className, $id): ?object
{
}
Expand Down
62 changes: 58 additions & 4 deletions src/Symfony/Bridge/Doctrine/Tests/ManagerRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class ManagerRegistryTest extends TestCase
{
public static function setUpBeforeClass(): void
public function testResetService()
{
$container = new ContainerBuilder();

Expand All @@ -32,10 +32,7 @@ public static function setUpBeforeClass(): void

$dumper = new PhpDumper($container);
eval('?>'.$dumper->dump(['class' => 'LazyServiceDoctrineBridgeContainer']));
}

public function testResetService()
{
$container = new \LazyServiceDoctrineBridgeContainer();

$registry = new TestManagerRegistry('name', [], ['defaultManager' => 'foo'], 'defaultConnection', 'defaultManager', 'proxyInterfaceName');
Expand All @@ -52,6 +49,63 @@ public function testResetService()
$this->assertFalse(isset($foo->bar));
}

/**
* @requires PHP 8.4
*
* @dataProvider provideResetServiceWithNativeLazyObjectsCases
*/
public function testResetServiceWithNativeLazyObjects(string $class)
{
$container = new $class();

$registry = new TestManagerRegistry(
'irrelevant',
[],
['defaultManager' => 'foo'],
'irrelevant',
'defaultManager',
'irrelevant',
);
$registry->setTestContainer($container);

$foo = $container->get('foo');
self::assertSame(DummyManager::class, $foo::class);

$foo->bar = 123;
self::assertTrue(isset($foo->bar));

$registry->resetManager();

self::assertSame($foo, $container->get('foo'));
self::assertSame(DummyManager::class, $foo::class);
self::assertFalse(isset($foo->bar));
}

public static function provideResetServiceWithNativeLazyObjectsCases(): iterable
{
$container = new ContainerBuilder();

$container->register('foo', DummyManager::class)->setPublic(true);
$container->getDefinition('foo')->setLazy(true);
$container->compile();

$dumper = new PhpDumper($container);

eval('?>'.$dumper->dump(['class' => 'NativeLazyServiceDoctrineBridgeContainer']));

yield ['NativeLazyServiceDoctrineBridgeContainer'];

$dumps = $dumper->dump(['class' => 'NativeLazyServiceDoctrineBridgeContainerAsFiles', 'as_files' => true]);

$lastDump = array_pop($dumps);
foreach (array_reverse($dumps) as $dump) {
eval('?>'.$dump);
}
eval('?>'.$lastDump);

yield ['NativeLazyServiceDoctrineBridgeContainerAsFiles'];
}

/**
* When performing an entity manager lazy service reset, the reset operations may re-use the container
* to create a "fresh" service: when doing so, it can happen that the "fresh" service is itself a proxy.
Expand Down
Loading