diff --git a/src/Core/src/Exception/Container/RecursiveProxyException.php b/src/Core/src/Exception/Container/RecursiveProxyException.php new file mode 100644 index 000000000..821975fca --- /dev/null +++ b/src/Core/src/Exception/Container/RecursiveProxyException.php @@ -0,0 +1,13 @@ + $name ?? 'null', + Introspector::scopeNames($c), + ))); + } } diff --git a/src/Core/tests/Scope/ProxyTest.php b/src/Core/tests/Scope/ProxyTest.php index 08e3d317f..00a17de14 100644 --- a/src/Core/tests/Scope/ProxyTest.php +++ b/src/Core/tests/Scope/ProxyTest.php @@ -9,6 +9,7 @@ use Spiral\Core\Attribute\Proxy; use Spiral\Core\Container; use Spiral\Core\Container\InjectorInterface; +use Spiral\Core\Exception\Container\RecursiveProxyException; use Spiral\Core\Scope; use Spiral\Tests\Core\Scope\Stub\Context; use Spiral\Tests\Core\Scope\Stub\ContextInterface; @@ -296,6 +297,25 @@ public function __toString(): string ); } + /** + * Proxy gets a proxy of the same type. + */ + public function testRecursiveProxy(): void + { + $root = new Container(); + $root->bind(UserInterface::class, new \Spiral\Core\Config\Proxy(UserInterface::class)); + + $this->expectException(RecursiveProxyException::class); + $this->expectExceptionMessage( + 'Recursive proxy detected for `Spiral\Tests\Core\Scope\Stub\UserInterface` in `root.null` scope.', + ); + + $root->runScope( + new Scope(), + fn(#[Proxy] UserInterface $user) => $user->getName(), + ); + } + /* // Proxy::$attachContainer=true tests