File tree 4 files changed +43
-0
lines changed
4 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Spiral \Core \Exception \Container ;
6
+
7
+ /**
8
+ * Recursion can occur due to improper container configuration or
9
+ * an unplanned exit from the scope by the execution thread.
10
+ */
11
+ class RecursiveProxyException extends ContainerException
12
+ {
13
+ }
Original file line number Diff line number Diff line change @@ -86,4 +86,9 @@ public static function create(
86
86
87
87
return $ instance ;
88
88
}
89
+
90
+ public static function isProxy (object $ object ): bool
91
+ {
92
+ return \in_array ($ object ::class, self ::$ classes , true );
93
+ }
89
94
}
Original file line number Diff line number Diff line change 7
7
use Psr \Container \ContainerInterface ;
8
8
use Spiral \Core \ContainerScope ;
9
9
use Spiral \Core \Exception \Container \ContainerException ;
10
+ use Spiral \Core \Exception \Container \RecursiveProxyException ;
11
+ use Spiral \Core \Internal \Proxy ;
10
12
11
13
/**
12
14
* @internal
@@ -32,6 +34,12 @@ public static function resolve(
32
34
);
33
35
}
34
36
37
+ if (Proxy::isProxy ($ result )) {
38
+ throw new RecursiveProxyException (
39
+ \sprintf ('Recursive proxy detected for `%s`. ' , $ alias ),
40
+ );
41
+ }
42
+
35
43
return $ result ;
36
44
}
37
45
}
Original file line number Diff line number Diff line change 9
9
use Spiral \Core \Attribute \Proxy ;
10
10
use Spiral \Core \Container ;
11
11
use Spiral \Core \Container \InjectorInterface ;
12
+ use Spiral \Core \Exception \Container \RecursiveProxyException ;
12
13
use Spiral \Core \Scope ;
13
14
use Spiral \Tests \Core \Scope \Stub \Context ;
14
15
use Spiral \Tests \Core \Scope \Stub \ContextInterface ;
@@ -296,6 +297,22 @@ public function __toString(): string
296
297
);
297
298
}
298
299
300
+ /**
301
+ * Proxy gets a proxy of the same type.
302
+ */
303
+ public function testRecursiveProxy (): void
304
+ {
305
+ $ root = new Container ();
306
+ $ root ->bind (UserInterface::class, new \Spiral \Core \Config \Proxy (UserInterface::class));
307
+
308
+ $ this ->expectException (RecursiveProxyException::class);
309
+
310
+ $ root ->runScope (
311
+ new Scope (),
312
+ fn (#[Proxy] UserInterface $ user ) => $ user ->getName (),
313
+ );
314
+ }
315
+
299
316
/*
300
317
// Proxy::$attachContainer=true tests
301
318
You can’t perform that action at this time.
0 commit comments