|
17 | 17 | use Tempest\Reflection\ParameterReflector; |
18 | 18 | use Tempest\Reflection\TypeReflector; |
19 | 19 | use Throwable; |
| 20 | +use UnitEnum; |
20 | 21 |
|
21 | 22 | final class GenericContainer implements Container |
22 | 23 | { |
@@ -122,12 +123,12 @@ public function unregister(string $className, bool $tagged = false): self |
122 | 123 | return $this; |
123 | 124 | } |
124 | 125 |
|
125 | | - public function has(string $className, ?string $tag = null): bool |
| 126 | + public function has(string $className, null|string|UnitEnum $tag = null): bool |
126 | 127 | { |
127 | 128 | return isset($this->definitions[$className]) || isset($this->singletons[$this->resolveTaggedName($className, $tag)]); |
128 | 129 | } |
129 | 130 |
|
130 | | - public function singleton(string $className, mixed $definition, ?string $tag = null): self |
| 131 | + public function singleton(string $className, mixed $definition, null|string|UnitEnum $tag = null): self |
131 | 132 | { |
132 | 133 | if ($definition instanceof HasTag) { |
133 | 134 | $tag = $definition->tag; |
@@ -156,7 +157,7 @@ public function config(object $config): self |
156 | 157 | * @param class-string<TClassName> $className |
157 | 158 | * @return null|TClassName |
158 | 159 | */ |
159 | | - public function get(string $className, ?string $tag = null, mixed ...$params): ?object |
| 160 | + public function get(string $className, null|string|UnitEnum $tag = null, mixed ...$params): ?object |
160 | 161 | { |
161 | 162 | $this->resolveChain(); |
162 | 163 |
|
@@ -298,7 +299,7 @@ public function removeInitializer(ClassReflector|string $initializerClass): Cont |
298 | 299 | return $this; |
299 | 300 | } |
300 | 301 |
|
301 | | - private function resolve(string $className, ?string $tag = null, mixed ...$params): ?object |
| 302 | + private function resolve(string $className, null|string|UnitEnum $tag = null, mixed ...$params): ?object |
302 | 303 | { |
303 | 304 | $class = new ClassReflector($className); |
304 | 305 |
|
@@ -361,7 +362,7 @@ private function initializerForBuiltin(TypeReflector $target, string $tag): ?Ini |
361 | 362 | return null; |
362 | 363 | } |
363 | 364 |
|
364 | | - private function initializerForClass(ClassReflector $target, ?string $tag = null): null|Initializer|DynamicInitializer |
| 365 | + private function initializerForClass(ClassReflector $target, null|string|UnitEnum $tag = null): null|Initializer|DynamicInitializer |
365 | 366 | { |
366 | 367 | // Initializers themselves can't be initialized, |
367 | 368 | // otherwise you'd end up with infinite loops |
@@ -454,7 +455,7 @@ private function autowireDependencies(MethodReflector|FunctionReflector $method, |
454 | 455 | return $dependencies; |
455 | 456 | } |
456 | 457 |
|
457 | | - private function autowireDependency(ParameterReflector $parameter, ?string $tag, mixed $providedValue = null): mixed |
| 458 | + private function autowireDependency(ParameterReflector $parameter, null|string|UnitEnum $tag, mixed $providedValue = null): mixed |
458 | 459 | { |
459 | 460 | $parameterType = $parameter->getType(); |
460 | 461 |
|
@@ -494,7 +495,7 @@ private function autowireDependency(ParameterReflector $parameter, ?string $tag, |
494 | 495 | throw $lastThrowable ?? new CannotAutowireException($this->chain, new Dependency($parameter)); |
495 | 496 | } |
496 | 497 |
|
497 | | - private function autowireObjectDependency(TypeReflector $type, ?string $tag, mixed $providedValue, bool $lazy): mixed |
| 498 | + private function autowireObjectDependency(TypeReflector $type, null|string|UnitEnum $tag, mixed $providedValue, bool $lazy): mixed |
498 | 499 | { |
499 | 500 | // If the provided value is of the right type, |
500 | 501 | // don't waste time autowiring, return it! |
@@ -591,8 +592,12 @@ public function __clone(): void |
591 | 592 | $this->chain = $this->chain?->clone(); |
592 | 593 | } |
593 | 594 |
|
594 | | - private function resolveTaggedName(string $className, ?string $tag): string |
| 595 | + private function resolveTaggedName(string $className, null|string|UnitEnum $tag): string |
595 | 596 | { |
| 597 | + if ($tag instanceof UnitEnum) { |
| 598 | + $tag = $tag->name; |
| 599 | + } |
| 600 | + |
596 | 601 | return $tag |
597 | 602 | ? "{$className}#{$tag}" |
598 | 603 | : $className; |
|
0 commit comments