@@ -73,7 +73,6 @@ class DIContainer implements DIContainerInterface
7373 private array $ bindings = [];
7474 private array $ exclude = [];
7575 private array $ named = [];
76- private array $ map = [];
7776
7877 public function __construct (DIModule ...$ modules )
7978 {
@@ -88,7 +87,6 @@ public function __clone()
8887 $ this ->inProgress = [];
8988 $ this ->singletons = [];
9089 $ this ->named = [];
91- $ this ->map = [];
9290 }
9391
9492 public function __destruct ()
@@ -97,7 +95,6 @@ public function __destruct()
9795 $ this ->bindings = [];
9896 $ this ->exclude = [];
9997 $ this ->named = [];
100- $ this ->map = [];
10198 }
10299
103100 /**
@@ -163,8 +160,8 @@ public function singleton(string $class, array $arguments = []): object
163160 public function share (object $ instance , array $ exclude = []): DIContainerInterface
164161 {
165162 $ class = $ instance ::class;
166- $ this ->bindInterfaces ($ class , $ class );
167163 $ this ->singletons [$ class ] = $ instance ;
164+ $ this ->bindInterfaces ($ class , $ class );
168165 foreach ($ exclude as $ name ) {
169166 $ this ->exclude [$ name ][$ class ] = $ class ;
170167 }
@@ -190,7 +187,8 @@ public function bind(string $interface, string $class = ''): DIContainerInterfac
190187 $ class && $ this ->bindings [$ class ] = $ interface ;
191188 return $ this ;
192189 }
193- return $ this ->bindInterfaces ($ interface , $ class );
190+ $ this ->bindInterfaces ($ interface , $ class );
191+ return $ this ;
194192 }
195193
196194 /**
@@ -255,25 +253,28 @@ private function getNameFromBindings(string $dependency): string
255253 return $ this ->bindings [$ dependency ] ?? $ dependency ;
256254 }
257255
258- private function bindInterfaces (string $ dependency , string $ class ): static
256+ private function bindInterfaces (string $ dependency , string $ class ): void
259257 {
260- if (empty ($ class )) {
261- foreach (\class_implements ($ dependency ) as $ interface ) {
262- $ this ->map [$ interface ] = $ dependency ;
263- }
264- return $ this ;
258+ if (\interface_exists ($ class )) {
259+ throw DIException::forInterfaceBinding ($ dependency , $ class );
265260 }
266261 $ this ->bindings [$ dependency ] = $ class ;
262+ foreach ($ this ->bindings as $ dependency => $ class ) {
263+ $ this ->mapDeferred ($ dependency , $ class );
264+ }
265+ }
266+
267+ private function mapDeferred (string $ dependency , string $ class ): void
268+ {
267269 foreach (\class_implements ($ dependency ) as $ interface ) {
268- if (isset ($ this ->bindings [$ interface ])) {
270+ if (false === isset ($ this ->bindings [$ interface ])) {
271+ continue ;
272+ }
273+ if (false === empty ($ class )) {
269274 $ this ->bindings [$ interface ] = $ class ;
270- break ;
271275 }
276+ $ this ->bindings [$ dependency ] = $ this ->bindings [$ interface ];
277+ break ;
272278 }
273- if (isset ($ this ->map [$ dependency ])) {
274- $ this ->bindings [$ this ->map [$ dependency ]] = $ class ;
275- unset($ this ->map [$ dependency ]);
276- }
277- return $ this ;
278279 }
279280}
0 commit comments