Skip to content

Commit 5a305c5

Browse files
krazostrolucky
authored andcommitted
Fix Entity manager is closed on worker mode and PHP 8.4
1 parent 34673de commit 5a305c5

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/Registry.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
use Doctrine\ORM\ORMException;
77
use Doctrine\Persistence\Proxy;
88
use ProxyManager\Proxy\LazyLoadingInterface;
9+
use ReflectionClass;
910
use Symfony\Bridge\Doctrine\ManagerRegistry;
1011
use Symfony\Component\DependencyInjection\Container;
1112
use Symfony\Component\VarExporter\LazyObjectInterface;
1213
use Symfony\Contracts\Service\ResetInterface;
1314

1415
use function array_keys;
1516
use function assert;
17+
use function method_exists;
18+
19+
use const PHP_VERSION_ID;
1620

1721
/**
1822
* References all Doctrine connections and entity managers in a given Container.
@@ -79,10 +83,27 @@ private function resetOrClearManager(string $managerName, string $serviceId): vo
7983

8084
assert($manager instanceof EntityManagerInterface);
8185

82-
if ((! $manager instanceof LazyLoadingInterface && ! $manager instanceof LazyObjectInterface) || $manager->isOpen()) {
83-
$manager->clear();
86+
// Determine if the version of symfony/dependency-injection is >= 7.3
87+
/** @phpstan-ignore function.alreadyNarrowedType */
88+
$sfNativeLazyObjects = method_exists('Symfony\Component\DependencyInjection\ContainerBuilder', 'findTaggedResourceIds');
8489

85-
return;
90+
if (PHP_VERSION_ID < 80400 || ! $sfNativeLazyObjects) {
91+
if ((! $manager instanceof LazyLoadingInterface && ! $manager instanceof LazyObjectInterface) || $manager->isOpen()) {
92+
$manager->clear();
93+
94+
return;
95+
}
96+
} else {
97+
$r = new ReflectionClass($manager);
98+
if ($r->isUninitializedLazyObject($manager)) {
99+
return;
100+
}
101+
102+
if ($manager->isOpen()) {
103+
$manager->clear();
104+
105+
return;
106+
}
86107
}
87108

88109
$this->resetManager($managerName);

0 commit comments

Comments
 (0)