1010use function is_int ;
1111use function is_string ;
1212use Psr \Container \ContainerInterface ;
13+ use Webmozart \Assert \Assert ;
1314use WebProject \PhpOpenApiMockServer \Container \Exception \ContainerException ;
1415use WebProject \PhpOpenApiMockServer \Container \Exception \NotFoundException ;
1516
@@ -33,7 +34,8 @@ final class SimpleContainer implements ContainerInterface
3334
3435 public function get (string $ id ): mixed
3536 {
36- $ resolved = $ this ->resolveAlias ($ id );
37+ $ resolved = $ this ->resolveAlias ($ id , true );
38+ Assert::string ($ resolved );
3739
3840 if (array_key_exists ($ resolved , $ this ->services )) {
3941 return $ this ->services [$ resolved ];
@@ -78,7 +80,11 @@ public function get(string $id): mixed
7880
7981 public function has (string $ id ): bool
8082 {
81- $ resolved = $ this ->resolveAlias ($ id );
83+ $ resolved = $ this ->resolveAlias ($ id , false );
84+
85+ if (null === $ resolved ) {
86+ return false ;
87+ }
8288
8389 return array_key_exists ($ resolved , $ this ->services ) || isset ($ this ->factories [$ resolved ]);
8490 }
@@ -142,13 +148,17 @@ public function setInvokableClass(string $name, string $class): void
142148 $ this ->factories [$ name ] = static fn (): object => new $ class ();
143149 }
144150
145- private function resolveAlias (string $ id ): string
151+ private function resolveAlias (string $ id, bool $ throwOnCircular = true ): ? string
146152 {
147153 $ seen = [];
148154
149155 while (isset ($ this ->aliases [$ id ])) {
150156 if (isset ($ seen [$ id ])) {
151- throw new ContainerException ("Circular alias detected for ' {$ id }'. " );
157+ if ($ throwOnCircular ) {
158+ throw new ContainerException ("Circular alias detected for ' {$ id }'. " );
159+ }
160+
161+ return null ;
152162 }
153163
154164 $ seen [$ id ] = true ;
0 commit comments