Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aefeb67

Browse files
committed
gracefully handle cases when no resolver is set
1 parent 1edfce7 commit aefeb67

File tree

6 files changed

+66
-1
lines changed

6 files changed

+66
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Config\Exception;
13+
14+
class LogicException extends \LogicException
15+
{
16+
}

src/Symfony/Component/Config/Loader/Loader.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Config\Loader;
1313

1414
use Symfony\Component\Config\Exception\LoaderLoadException;
15+
use Symfony\Component\Config\Exception\LogicException;
1516

1617
/**
1718
* Loader is the abstract class used by all built-in loaders.
@@ -20,7 +21,7 @@
2021
*/
2122
abstract class Loader implements LoaderInterface
2223
{
23-
protected LoaderResolverInterface $resolver;
24+
protected ?LoaderResolverInterface $resolver = null;
2425
protected ?string $env;
2526

2627
public function __construct(?string $env = null)
@@ -30,6 +31,10 @@ public function __construct(?string $env = null)
3031

3132
public function getResolver(): LoaderResolverInterface
3233
{
34+
if (null === $this->resolver) {
35+
throw new LogicException('Cannot get a resolver if none was set.');
36+
}
37+
3338
return $this->resolver;
3439
}
3540

src/Symfony/Component/Config/Tests/Loader/LoaderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Config\Exception\LoaderLoadException;
16+
use Symfony\Component\Config\Exception\LogicException;
1617
use Symfony\Component\Config\Loader\Loader;
1718
use Symfony\Component\Config\Loader\LoaderInterface;
1819
use Symfony\Component\Config\Loader\LoaderResolverInterface;
@@ -29,6 +30,14 @@ public function testGetSetResolver()
2930
$this->assertSame($resolver, $loader->getResolver(), '->setResolver() sets the resolver loader');
3031
}
3132

33+
public function testGetResolverWithoutSetResolver()
34+
{
35+
$this->expectException(LogicException::class);
36+
37+
$loader = new ProjectLoader1();
38+
$loader->getResolver();
39+
}
40+
3241
public function testResolve()
3342
{
3443
$resolvedLoader = $this->createMock(LoaderInterface::class);
@@ -46,6 +55,14 @@ public function testResolve()
4655
$this->assertSame($resolvedLoader, $loader->resolve('foo.xml'), '->resolve() finds a loader');
4756
}
4857

58+
public function testResolveWithoutSetResolver()
59+
{
60+
$this->expectException(LoaderLoadException::class);
61+
62+
$loader = new ProjectLoader1();
63+
$loader->resolve('foo.xml');
64+
}
65+
4966
public function testResolveWhenResolverCannotFindLoader()
5067
{
5168
$resolver = $this->createMock(LoaderResolverInterface::class);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Exception;
13+
14+
class LogicException extends \LogicException
15+
{
16+
}

src/Symfony/Component/Routing/Loader/AttributeClassLoader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Config\Loader\LoaderResolverInterface;
1616
use Symfony\Component\Config\Resource\FileResource;
1717
use Symfony\Component\Routing\Attribute\Route as RouteAnnotation;
18+
use Symfony\Component\Routing\Exception\LogicException;
1819
use Symfony\Component\Routing\Route;
1920
use Symfony\Component\Routing\RouteCollection;
2021

@@ -226,6 +227,7 @@ public function setResolver(LoaderResolverInterface $resolver): void
226227

227228
public function getResolver(): LoaderResolverInterface
228229
{
230+
throw new LogicException(sprintf('The "%s()" method must not be called.', __METHOD__));
229231
}
230232

231233
/**

src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Routing\Alias;
16+
use Symfony\Component\Routing\Exception\LogicException;
1617
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\AbstractClassController;
1718
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\ActionPathController;
1819
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\BazClass;
@@ -54,6 +55,14 @@ protected function setUp(?string $env = null): void
5455
$this->loader = new TraceableAttributeClassLoader($env);
5556
}
5657

58+
public function testGetResolver()
59+
{
60+
$this->expectException(LogicException::class);
61+
62+
$loader = new TraceableAttributeClassLoader();
63+
$loader->getResolver();
64+
}
65+
5766
/**
5867
* @dataProvider provideTestSupportsChecksResource
5968
*/

0 commit comments

Comments
 (0)