|
16 | 16 | use Tempest\Reflection\MethodReflector; |
17 | 17 | use Tempest\Reflection\ParameterReflector; |
18 | 18 | use Tempest\Reflection\TypeReflector; |
| 19 | +use Tempest\Support\Arr; |
19 | 20 | use Throwable; |
20 | 21 |
|
21 | 22 | final class GenericContainer implements Container |
@@ -44,6 +45,13 @@ public function setDefinitions(array $definitions): self |
44 | 45 | return $this; |
45 | 46 | } |
46 | 47 |
|
| 48 | + public function setSingletons(array $singletons): self |
| 49 | + { |
| 50 | + $this->singletons = new ArrayIterator($singletons); |
| 51 | + |
| 52 | + return $this; |
| 53 | + } |
| 54 | + |
47 | 55 | public function setInitializers(array $initializers): self |
48 | 56 | { |
49 | 57 | $this->initializers = new ArrayIterator($initializers); |
@@ -85,10 +93,19 @@ public function register(string $className, callable $definition): self |
85 | 93 | return $this; |
86 | 94 | } |
87 | 95 |
|
88 | | - public function unregister(string $className): self |
| 96 | + public function unregister(string $className, bool $tagged = false): self |
89 | 97 | { |
90 | 98 | unset($this->definitions[$className], $this->singletons[$className]); |
91 | 99 |
|
| 100 | + if ($tagged) { |
| 101 | + $singletons = Arr\filter( |
| 102 | + array: $this->getSingletons(), |
| 103 | + filter: static fn (mixed $_, string $key) => ! str_starts_with($key, "{$className}#"), |
| 104 | + ); |
| 105 | + |
| 106 | + $this->setSingletons($singletons); |
| 107 | + } |
| 108 | + |
92 | 109 | return $this; |
93 | 110 | } |
94 | 111 |
|
@@ -241,6 +258,28 @@ public function addInitializer(ClassReflector|string $initializerClass): Contain |
241 | 258 | return $this; |
242 | 259 | } |
243 | 260 |
|
| 261 | + public function removeInitializer(ClassReflector|string $initializerClass): Container |
| 262 | + { |
| 263 | + if (! ($initializerClass instanceof ClassReflector)) { |
| 264 | + $initializerClass = new ClassReflector($initializerClass); |
| 265 | + } |
| 266 | + |
| 267 | + if ($initializerClass->getType()->matches(DynamicInitializer::class)) { |
| 268 | + $index = array_find_key( |
| 269 | + array: $this->dynamicInitializers->getArrayCopy(), |
| 270 | + callback: static fn (string $className) => $className === $initializerClass->getName(), |
| 271 | + ); |
| 272 | + |
| 273 | + unset($this->dynamicInitializers[$index]); |
| 274 | + |
| 275 | + return $this; |
| 276 | + } |
| 277 | + |
| 278 | + unset($this->initializers[$initializerClass->getName()]); |
| 279 | + |
| 280 | + return $this; |
| 281 | + } |
| 282 | + |
244 | 283 | private function resolve(string $className, ?string $tag = null, mixed ...$params): ?object |
245 | 284 | { |
246 | 285 | $class = new ClassReflector($className); |
|
0 commit comments