From f984be42e1f3a0f1a643e2cd41747a426758b793 Mon Sep 17 00:00:00 2001 From: Max Mercuriev Date: Sun, 9 Jun 2024 12:51:55 +0800 Subject: [PATCH] reflection factory resolves array $options --- .../ReflectionBasedAbstractFactory.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/AbstractFactory/ReflectionBasedAbstractFactory.php b/src/AbstractFactory/ReflectionBasedAbstractFactory.php index 4f4654bd..88c04567 100644 --- a/src/AbstractFactory/ReflectionBasedAbstractFactory.php +++ b/src/AbstractFactory/ReflectionBasedAbstractFactory.php @@ -123,8 +123,8 @@ public function __invoke(ContainerInterface $container, $requestedName, ?array $ } $resolver = $container->has('config') - ? $this->resolveParameterWithConfigService($container, $requestedName) - : $this->resolveParameterWithoutConfigService($container, $requestedName); + ? $this->resolveParameterWithConfigService($container, $requestedName, $options) + : $this->resolveParameterWithoutConfigService($container, $requestedName, $options); $parameters = array_map($resolver, $reflectionParameters); @@ -153,7 +153,7 @@ private function canCallConstructor(string $requestedName): bool * @param string $requestedName * @return callable */ - private function resolveParameterWithoutConfigService(ContainerInterface $container, $requestedName) + private function resolveParameterWithoutConfigService(ContainerInterface $container, $requestedName, $options) { /** * @param ReflectionParameter $parameter @@ -162,7 +162,7 @@ private function resolveParameterWithoutConfigService(ContainerInterface $contai * resolved to a service in the container. * @psalm-suppress MissingClosureReturnType */ - return fn(ReflectionParameter $parameter) => $this->resolveParameter($parameter, $container, $requestedName); + return fn(ReflectionParameter $parameter) => $this->resolveParameter($parameter, $container, $requestedName, $options); } /** @@ -174,7 +174,7 @@ private function resolveParameterWithoutConfigService(ContainerInterface $contai * @param string $requestedName * @return callable */ - private function resolveParameterWithConfigService(ContainerInterface $container, $requestedName) + private function resolveParameterWithConfigService(ContainerInterface $container, $requestedName, $options) { /** * @param ReflectionParameter $parameter @@ -182,14 +182,14 @@ private function resolveParameterWithConfigService(ContainerInterface $container * @throws ServiceNotFoundException If type-hinted parameter cannot be * resolved to a service in the container. */ - return function (ReflectionParameter $parameter) use ($container, $requestedName) { + return function (ReflectionParameter $parameter) use ($container, $requestedName, $options) { if ($parameter->getName() === 'config') { $type = $parameter->getType(); if ($type instanceof ReflectionNamedType && $type->getName() === 'array') { return $container->get('config'); } } - return $this->resolveParameter($parameter, $container, $requestedName); + return $this->resolveParameter($parameter, $container, $requestedName, $options); }; } @@ -201,12 +201,15 @@ private function resolveParameterWithConfigService(ContainerInterface $container * @throws ServiceNotFoundException If type-hinted parameter cannot be * resolved to a service in the container. */ - private function resolveParameter(ReflectionParameter $parameter, ContainerInterface $container, $requestedName) + private function resolveParameter(ReflectionParameter $parameter, ContainerInterface $container, $requestedName, $options) { $type = $parameter->getType(); $type = $type instanceof ReflectionNamedType ? $type->getName() : null; if ($type === 'array') { + if ($parameter->getName() === 'options') { + return $options ?? []; + } return []; }