@@ -44,6 +44,13 @@ public function setDefinitions(array $definitions): self
4444 return $ this ;
4545 }
4646
47+ public function setSingletons (array $ singletons ): self
48+ {
49+ $ this ->singletons = new ArrayIterator ($ singletons );
50+
51+ return $ this ;
52+ }
53+
4754 public function setInitializers (array $ initializers ): self
4855 {
4956 $ this ->initializers = new ArrayIterator ($ initializers );
@@ -85,10 +92,20 @@ public function register(string $className, callable $definition): self
8592 return $ this ;
8693 }
8794
88- public function unregister (string $ className ): self
95+ public function unregister (string $ className, bool $ tagged = false ): self
8996 {
9097 unset($ this ->definitions [$ className ], $ this ->singletons [$ className ]);
9198
99+ if ($ tagged ) {
100+ $ singletons = array_filter (
101+ array: $ this ->getSingletons (),
102+ callback: static fn (mixed $ _ , string $ key ) => ! str_starts_with ($ key , "{$ className }# " ),
103+ mode: \ARRAY_FILTER_USE_BOTH ,
104+ );
105+
106+ $ this ->setSingletons ($ singletons );
107+ }
108+
92109 return $ this ;
93110 }
94111
@@ -241,6 +258,28 @@ public function addInitializer(ClassReflector|string $initializerClass): Contain
241258 return $ this ;
242259 }
243260
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+
244283 private function resolve (string $ className , ?string $ tag = null , mixed ...$ params ): ?object
245284 {
246285 $ class = new ClassReflector ($ className );
0 commit comments