diff --git a/src/Symfony/Component/Config/Exception/LogicException.php b/src/Symfony/Component/Config/Exception/LogicException.php new file mode 100644 index 0000000000000..d227aece4b3b2 --- /dev/null +++ b/src/Symfony/Component/Config/Exception/LogicException.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Config\Exception; + +class LogicException extends \LogicException +{ +} diff --git a/src/Symfony/Component/Config/Loader/Loader.php b/src/Symfony/Component/Config/Loader/Loader.php index ad1c809324fec..30f45e11edeec 100644 --- a/src/Symfony/Component/Config/Loader/Loader.php +++ b/src/Symfony/Component/Config/Loader/Loader.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Config\Loader; use Symfony\Component\Config\Exception\LoaderLoadException; +use Symfony\Component\Config\Exception\LogicException; /** * Loader is the abstract class used by all built-in loaders. @@ -20,7 +21,7 @@ */ abstract class Loader implements LoaderInterface { - protected LoaderResolverInterface $resolver; + protected ?LoaderResolverInterface $resolver = null; protected ?string $env; public function __construct(?string $env = null) @@ -30,6 +31,10 @@ public function __construct(?string $env = null) public function getResolver(): LoaderResolverInterface { + if (null === $this->resolver) { + throw new LogicException('Cannot get a resolver if none was set.'); + } + return $this->resolver; } diff --git a/src/Symfony/Component/Config/Tests/Loader/LoaderTest.php b/src/Symfony/Component/Config/Tests/Loader/LoaderTest.php index 70bfb8fc15005..263bde26de04a 100644 --- a/src/Symfony/Component/Config/Tests/Loader/LoaderTest.php +++ b/src/Symfony/Component/Config/Tests/Loader/LoaderTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Config\Exception\LoaderLoadException; +use Symfony\Component\Config\Exception\LogicException; use Symfony\Component\Config\Loader\Loader; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\Config\Loader\LoaderResolverInterface; @@ -29,6 +30,14 @@ public function testGetSetResolver() $this->assertSame($resolver, $loader->getResolver(), '->setResolver() sets the resolver loader'); } + public function testGetResolverWithoutSetResolver() + { + $this->expectException(LogicException::class); + + $loader = new ProjectLoader1(); + $loader->getResolver(); + } + public function testResolve() { $resolvedLoader = $this->createMock(LoaderInterface::class); @@ -46,6 +55,14 @@ public function testResolve() $this->assertSame($resolvedLoader, $loader->resolve('foo.xml'), '->resolve() finds a loader'); } + public function testResolveWithoutSetResolver() + { + $this->expectException(LoaderLoadException::class); + + $loader = new ProjectLoader1(); + $loader->resolve('foo.xml'); + } + public function testResolveWhenResolverCannotFindLoader() { $resolver = $this->createMock(LoaderResolverInterface::class); diff --git a/src/Symfony/Component/Routing/Exception/LogicException.php b/src/Symfony/Component/Routing/Exception/LogicException.php new file mode 100644 index 0000000000000..16ed58eefe379 --- /dev/null +++ b/src/Symfony/Component/Routing/Exception/LogicException.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Routing\Exception; + +class LogicException extends \LogicException +{ +} diff --git a/src/Symfony/Component/Routing/Loader/AttributeClassLoader.php b/src/Symfony/Component/Routing/Loader/AttributeClassLoader.php index 3b895a9f8006e..8372d90ae9b49 100644 --- a/src/Symfony/Component/Routing/Loader/AttributeClassLoader.php +++ b/src/Symfony/Component/Routing/Loader/AttributeClassLoader.php @@ -15,6 +15,7 @@ use Symfony\Component\Config\Loader\LoaderResolverInterface; use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\Routing\Attribute\Route as RouteAnnotation; +use Symfony\Component\Routing\Exception\LogicException; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; @@ -226,6 +227,7 @@ public function setResolver(LoaderResolverInterface $resolver): void public function getResolver(): LoaderResolverInterface { + throw new LogicException(sprintf('The "%s()" method must not be called.', __METHOD__)); } /** diff --git a/src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php index 9b8c5d27397ed..ad65f09c253cc 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Routing\Alias; +use Symfony\Component\Routing\Exception\LogicException; use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\AbstractClassController; use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\ActionPathController; use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\BazClass; @@ -54,6 +55,14 @@ protected function setUp(?string $env = null): void $this->loader = new TraceableAttributeClassLoader($env); } + public function testGetResolver() + { + $this->expectException(LogicException::class); + + $loader = new TraceableAttributeClassLoader(); + $loader->getResolver(); + } + /** * @dataProvider provideTestSupportsChecksResource */