From 0dd23fdaa80b00d84283a25f952fdd950d323512 Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Sat, 8 Apr 2023 11:34:26 -0500 Subject: [PATCH] Update Container.php --- src/Container.php | 59 +++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/src/Container.php b/src/Container.php index 2f3d3ac..4e1c976 100644 --- a/src/Container.php +++ b/src/Container.php @@ -100,11 +100,7 @@ public function __unserialize(array $data): void public function alias(string $abstract, string $concrete): void { - if (trim($abstract) === '') { - throw $this->throwServiceIdMustBeNonEmptyString(); - } - - if (trim($concrete) === '') { + if (trim($abstract) === '' || trim($concrete) === '') { throw $this->throwServiceIdMustBeNonEmptyString(); } @@ -112,10 +108,6 @@ public function alias(string $abstract, string $concrete): void throw $this->throwInvalidArgument('Service "%s" can not use an alias with the same name.', $concrete); } - if (! $this->has($concrete)) { - throw $this->throwNotFoundException('Service "%s" was not found.', $concrete); - } - self::$instance->cache[self::ALIASES][$abstract] = $concrete; } @@ -216,9 +208,7 @@ public function call(callable|string $invokable, array $arguments = []): mixed public function extend(string $class, callable $extension): void { - if (trim($class) === '') { - throw $this->throwServiceIdMustBeNonEmptyString(); - } + $class = $this->resolve($class); $factories = self::$instance->cache[self::FACTORIES]; $extensions = self::$instance->cache[self::EXTENSIONS]; @@ -262,7 +252,7 @@ public function get(string $id): mixed throw $this->throwNotFoundException('Service "%s" was not found.', $id); } - $serviceFactory = $factories[$id] ?? static fn (Container $container): object => $container->build($id); + $serviceFactory = $factories[$id] ?? static fn (self $container): object => $container->build($id); $extensions = self::$instance->cache[self::EXTENSIONS]; @@ -276,13 +266,15 @@ public static function getInstance(): self return self::$instance ??= new self(); } + /** + * @throws ContainerExceptionInterface + */ public function has(string $id): bool { $id = $this->resolve($id); - return array_key_exists($id, self::$instance->cache[self::SERVICES]) || - array_key_exists($id, self::$instance->cache[self::FACTORIES]) || - array_key_exists($id, self::$instance->cache[self::ALIASES]); + return array_key_exists($id, self::$instance->cache[self::SERVICES]) + || array_key_exists($id, self::$instance->cache[self::FACTORIES]); } public function register(string $serviceProvider): void @@ -317,6 +309,9 @@ public function remove(string $id): void ); } + /** + * @throws ContainerExceptionInterface + */ public function replace(string $id, mixed $value, array $tags = []): void { $this->remove($id); @@ -337,21 +332,19 @@ public function resolve(string $id): string return $id; } + /** + * @throws ContainerExceptionInterface + */ public function set(string $id, mixed $value, array $tags = []): void { if (trim($id) === '') { throw $this->throwServiceIdMustBeNonEmptyString(); } - if (array_key_exists($id, self::$instance->cache[self::SERVICES])) { - throw $this->throwServiceAlreadyRegisteredException($id); - } - - if (array_key_exists($id, self::$instance->cache[self::FACTORIES])) { - throw $this->throwServiceAlreadyRegisteredException($id); - } - - if (array_key_exists($id, self::$instance->cache[self::ALIASES])) { + if (array_key_exists($id, self::$instance->cache[self::SERVICES]) || + array_key_exists($id, self::$instance->cache[self::FACTORIES]) || + array_key_exists($id, self::$instance->cache[self::ALIASES]) + ) { throw $this->throwServiceAlreadyRegisteredException($id); } @@ -391,10 +384,15 @@ public function tag(string $id, array $tags): void } /** + * @template TObject of object + * @template TMixed + * + * @param class-string $tag + * * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface * - * @return Generator + * @return Generator */ public function tagged(string $tag): Generator { @@ -410,6 +408,7 @@ private function buildParameters(array $reflectionParameters, array $arguments): /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface + * @throws ReflectionException */ function (ReflectionParameter $reflectionParameter) use (&$arguments) { $parameterName = $reflectionParameter->getName(); @@ -425,15 +424,15 @@ function (ReflectionParameter $reflectionParameter) use (&$arguments) { return $parameter; } - if ($reflectionParameter->isDefaultValueAvailable()) { - return $reflectionParameter->getDefaultValue(); - } - $reflectionType = $reflectionParameter->getType(); if ($reflectionType instanceof ReflectionNamedType && ! $reflectionType->isBuiltin()) { return $this->get($reflectionType->getName()); } + if ($reflectionParameter->isDefaultValueAvailable()) { + return $reflectionParameter->getDefaultValue(); + } + $name = $reflectionParameter->getDeclaringFunction() ->getName(); $isFunction = is_callable($name);