Skip to content

Commit bfa5878

Browse files
[DependencyInjection] Leverage native lazy objects for lazy services
1 parent 2f0894b commit bfa5878

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Reset the manager registry using native lazy objects when applicable
8+
49
7.2
510
---
611

ManagerRegistry.php

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,60 @@ protected function resetService($name): void
4545

4646
return;
4747
}
48-
if (!$manager instanceof LazyLoadingInterface) {
49-
throw new \LogicException(\sprintf('Resetting a non-lazy manager service is not supported. Declare the "%s" service as lazy.', $name));
48+
if (\PHP_VERSION_ID < 80400) {
49+
if (!$manager instanceof LazyLoadingInterface) {
50+
throw new \LogicException(\sprintf('Resetting a non-lazy manager service is not supported. Declare the "%s" service as lazy.', $name));
51+
}
52+
trigger_deprecation('symfony/doctrine-bridge', '7.3', 'Support for proxy-manager is deprecated.');
53+
54+
if ($manager instanceof GhostObjectInterface) {
55+
throw new \LogicException('Resetting a lazy-ghost-object manager service is not supported.');
56+
}
57+
$manager->setProxyInitializer(\Closure::bind(
58+
function (&$wrappedInstance, LazyLoadingInterface $manager) use ($name) {
59+
$name = $this->aliases[$name] ?? $name;
60+
$wrappedInstance = match (true) {
61+
isset($this->fileMap[$name]) => $this->load($this->fileMap[$name], false),
62+
(new \ReflectionMethod($this, $method = $this->methodMap[$name]))->isStatic() => $this->{$method}($this, false),
63+
default => $this->{$method}(false),
64+
};
65+
$manager->setProxyInitializer(null);
66+
67+
return true;
68+
},
69+
$this->container,
70+
Container::class
71+
));
72+
73+
return;
5074
}
51-
if ($manager instanceof GhostObjectInterface) {
52-
throw new \LogicException('Resetting a lazy-ghost-object manager service is not supported.');
75+
76+
$r = new \ReflectionClass($manager);
77+
78+
if ($r->isUninitializedLazyObject($manager)) {
79+
return;
80+
}
81+
82+
try {
83+
$r->resetAsLazyProxy($manager, \Closure::bind(
84+
function () use ($name) {
85+
$name = $this->aliases[$name] ?? $name;
86+
87+
return match (true) {
88+
isset($this->fileMap[$name]) => $this->load($this->fileMap[$name], false),
89+
(new \ReflectionMethod($this, $method = $this->methodMap[$name]))->isStatic() => $this->{$method}($this, false),
90+
default => $this->{$method}(false),
91+
};
92+
},
93+
$this->container,
94+
Container::class
95+
));
96+
} catch (\Error $e) {
97+
if (__FILE__ !== $e->getFile()) {
98+
throw $e;
99+
}
100+
101+
throw new \LogicException(\sprintf('Resetting a non-lazy manager service is not supported. Declare the "%s" service as lazy.', $name), 0, $e);
53102
}
54-
$manager->setProxyInitializer(\Closure::bind(
55-
function (&$wrappedInstance, LazyLoadingInterface $manager) use ($name) {
56-
if (isset($this->aliases[$name])) {
57-
$name = $this->aliases[$name];
58-
}
59-
if (isset($this->fileMap[$name])) {
60-
$wrappedInstance = $this->load($this->fileMap[$name], false);
61-
} elseif ((new \ReflectionMethod($this, $this->methodMap[$name]))->isStatic()) {
62-
$wrappedInstance = $this->{$this->methodMap[$name]}($this, false);
63-
} else {
64-
$wrappedInstance = $this->{$this->methodMap[$name]}(false);
65-
}
66-
67-
$manager->setProxyInitializer(null);
68-
69-
return true;
70-
},
71-
$this->container,
72-
Container::class
73-
));
74103
}
75104
}

0 commit comments

Comments
 (0)