diff options
author | Côme Chilliet <come.chilliet@nextcloud.com> | 2025-04-29 16:47:32 +0200 |
---|---|---|
committer | Côme Chilliet <come.chilliet@nextcloud.com> | 2025-05-20 11:29:14 +0200 |
commit | c4e78ae685d44b497b0149bd3a80e58e7b128242 (patch) | |
tree | 77fb9324959884827e12404eb314d3608d475f71 | |
parent | 88af91e7c47a708199fb4401307bfc45e70168f1 (diff) | |
download | nextcloud-server-c4e78ae685d44b497b0149bd3a80e58e7b128242.tar.gz nextcloud-server-c4e78ae685d44b497b0149bd3a80e58e7b128242.zip |
fix: Only use Lazy objects if PHP is 8.4 or higher
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
-rw-r--r-- | lib/private/AppFramework/Utility/SimpleContainer.php | 76 |
1 files changed, 42 insertions, 34 deletions
diff --git a/lib/private/AppFramework/Utility/SimpleContainer.php b/lib/private/AppFramework/Utility/SimpleContainer.php index 119c5018443..9110f43f288 100644 --- a/lib/private/AppFramework/Utility/SimpleContainer.php +++ b/lib/private/AppFramework/Utility/SimpleContainer.php @@ -12,6 +12,7 @@ use Closure; use OCP\AppFramework\QueryException; use OCP\IContainer; use Pimple\Container; +use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; use ReflectionClass; use ReflectionException; @@ -57,47 +58,54 @@ class SimpleContainer implements ArrayAccess, ContainerInterface, IContainer { /* No constructor, return a instance directly */ return $class->newInstance(); } - return $class->newLazyGhost(function (object $object) use ($constructor): void { - /** @psalm-suppress DirectConstructorCall For lazy ghosts we have to call the constructor directly */ - $object->__construct(...array_map(function (ReflectionParameter $parameter) { - $parameterType = $parameter->getType(); + if (PHP_VERSION_ID >= 80400) { + /* For PHP>=8.4, use a lazy ghost to delay constructor and dependency resolving */ + /** @psalm-suppress UndefinedMethod */ + return $class->newLazyGhost(function (object $object) use ($constructor): void { + /** @psalm-suppress DirectConstructorCall For lazy ghosts we have to call the constructor directly */ + $object->__construct(...$this->buildClassConstructorParameters($constructor)); + }); + } else { + return $class->newInstanceArgs($this->buildClassConstructorParameters($constructor)); + } + } - $resolveName = $parameter->getName(); + private function buildClassConstructorParameters(\ReflectionMethod $constructor): array { + return array_map(function (ReflectionParameter $parameter) { + $parameterType = $parameter->getType(); - // try to find out if it is a class or a simple parameter - if ($parameterType !== null && ($parameterType instanceof ReflectionNamedType) && !$parameterType->isBuiltin()) { - $resolveName = $parameterType->getName(); - } + $resolveName = $parameter->getName(); - try { - $builtIn = $parameterType !== null && ($parameterType instanceof ReflectionNamedType) - && $parameterType->isBuiltin(); - return $this->query($resolveName, !$builtIn); - } catch (QueryException $e) { - // Service not found, use the default value when available - if ($parameter->isDefaultValueAvailable()) { - return $parameter->getDefaultValue(); - } + // try to find out if it is a class or a simple parameter + if ($parameterType !== null && ($parameterType instanceof ReflectionNamedType) && !$parameterType->isBuiltin()) { + $resolveName = $parameterType->getName(); + } + + try { + $builtIn = $parameterType !== null && ($parameterType instanceof ReflectionNamedType) + && $parameterType->isBuiltin(); + return $this->query($resolveName, !$builtIn); + } catch (ContainerExceptionInterface $e) { + // Service not found, use the default value when available + if ($parameter->isDefaultValueAvailable()) { + return $parameter->getDefaultValue(); + } - if ($parameterType !== null && ($parameterType instanceof ReflectionNamedType) && !$parameterType->isBuiltin()) { - $resolveName = $parameter->getName(); - try { - return $this->query($resolveName); - } catch (QueryException $e2) { - // Pass null if typed and nullable - if ($parameter->allowsNull() && ($parameterType instanceof ReflectionNamedType)) { - return null; - } - - // don't lose the error we got while trying to query by type - throw new QueryException($e->getMessage(), (int)$e->getCode(), $e); + if ($parameterType !== null && ($parameterType instanceof ReflectionNamedType) && !$parameterType->isBuiltin()) { + $resolveName = $parameter->getName(); + try { + return $this->query($resolveName); + } catch (ContainerExceptionInterface $e2) { + // Pass null if typed and nullable + if ($parameter->allowsNull() && ($parameterType instanceof ReflectionNamedType)) { + return null; } } - - throw $e; } - }, $constructor->getParameters())); - }); + + throw $e; + } + }, $constructor->getParameters()); } public function resolve($name) { |