Skip to content

Commit 5ab3a3e

Browse files
NeilPeyssardnicolas-grekas
authored andcommitted
[Config][Routing] Fix exclude option being ignored for non-glob and PSR-4 resources
1 parent 0dc6253 commit 5ab3a3e

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

Loader/Psr4DirectoryLoader.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ public function __construct(
3838
*/
3939
public function load(mixed $resource, ?string $type = null): ?RouteCollection
4040
{
41+
$excluded = $resource['_excluded'] ?? [];
4142
$path = $this->locator->locate($resource['path'], $this->currentDirectory);
4243
if (!is_dir($path)) {
4344
return new RouteCollection();
4445
}
4546

46-
return $this->loadFromDirectory($path, trim($resource['namespace'], '\\'));
47+
return $this->loadFromDirectory($path, trim($resource['namespace'], '\\'), $excluded);
4748
}
4849

4950
public function supports(mixed $resource, ?string $type = null): bool
@@ -59,7 +60,7 @@ public function forDirectory(string $currentDirectory): static
5960
return $loader;
6061
}
6162

62-
private function loadFromDirectory(string $directory, string $psr4Prefix): RouteCollection
63+
private function loadFromDirectory(string $directory, string $psr4Prefix, array $excluded = []): RouteCollection
6364
{
6465
$collection = new RouteCollection();
6566
$collection->addResource(new DirectoryResource($directory, '/\.php$/'));
@@ -74,8 +75,13 @@ private function loadFromDirectory(string $directory, string $psr4Prefix): Route
7475

7576
/** @var \SplFileInfo $file */
7677
foreach ($files as $file) {
78+
$normalizedPath = rtrim(str_replace('\\', '/', $file->getPathname()), '/');
79+
if (isset($excluded[$normalizedPath])) {
80+
continue;
81+
}
82+
7783
if ($file->isDir()) {
78-
$collection->addCollection($this->loadFromDirectory($file->getPathname(), $psr4Prefix.'\\'.$file->getFilename()));
84+
$collection->addCollection($this->loadFromDirectory($file->getPathname(), $psr4Prefix.'\\'.$file->getFilename(), $excluded));
7985

8086
continue;
8187
}

Tests/Loader/Psr4DirectoryLoaderTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,38 @@ public function testAbstractController()
6565
$this->assertSame(MyChildController::class.'::someAction', $route->getDefault('_controller'));
6666
}
6767

68+
public function testExcludeSubNamespace()
69+
{
70+
$fixturesPath = \dirname(__DIR__).'/Fixtures';
71+
$excluded = [
72+
rtrim(str_replace('\\', '/', $fixturesPath.'/Psr4Controllers/SubNamespace'), '/') => true,
73+
];
74+
$collection = $this->getLoader()->load(
75+
['path' => 'Psr4Controllers', 'namespace' => 'Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers', '_excluded' => $excluded],
76+
'attribute'
77+
);
78+
79+
$this->assertNotNull($collection->get('my_route'));
80+
$this->assertNull($collection->get('my_other_controller_one'));
81+
$this->assertNull($collection->get('my_controller_with_a_trait'));
82+
$this->assertNull($collection->get('my_child_controller_from_abstract'));
83+
}
84+
85+
public function testExcludeSingleFile()
86+
{
87+
$fixturesPath = \dirname(__DIR__).'/Fixtures';
88+
$excluded = [
89+
rtrim(str_replace('\\', '/', $fixturesPath.'/Psr4Controllers/MyController.php'), '/') => true,
90+
];
91+
$collection = $this->getLoader()->load(
92+
['path' => 'Psr4Controllers', 'namespace' => 'Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers', '_excluded' => $excluded],
93+
'attribute'
94+
);
95+
96+
$this->assertNull($collection->get('my_route'));
97+
$this->assertNotNull($collection->get('my_other_controller_one'));
98+
}
99+
68100
/**
69101
* @dataProvider provideNamespacesThatNeedTrimming
70102
*/

0 commit comments

Comments
 (0)