-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Config][Routing] Fix delegating to PSR-4 loader from subdirectory #47953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Config\Loader; | ||
|
||
/** | ||
* A loader that can be scoped to a given filesystem directory. | ||
* | ||
* @author Alexander M. Turek <me@derrabus.de> | ||
*/ | ||
interface DirectoryAwareLoaderInterface | ||
{ | ||
public function forDirectory(string $currentDirectory): static; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,16 +12,20 @@ | |
namespace Symfony\Component\Routing\Loader; | ||
|
||
use Symfony\Component\Config\FileLocatorInterface; | ||
use Symfony\Component\Config\Loader\DirectoryAwareLoaderInterface; | ||
use Symfony\Component\Config\Loader\Loader; | ||
use Symfony\Component\Config\Resource\DirectoryResource; | ||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
/** | ||
* A loader that discovers controller classes in a directory that follows PSR-4. | ||
* | ||
* @author Alexander M. Turek <me@derrabus.de> | ||
*/ | ||
final class Psr4DirectoryLoader extends Loader | ||
final class Psr4DirectoryLoader extends Loader implements DirectoryAwareLoaderInterface | ||
{ | ||
private ?string $currentDirectory = null; | ||
|
||
public function __construct( | ||
private readonly FileLocatorInterface $locator, | ||
) { | ||
|
@@ -34,7 +38,7 @@ public function __construct( | |
*/ | ||
public function load(mixed $resource, string $type = null): ?RouteCollection | ||
{ | ||
$path = $this->locator->locate($resource['path']); | ||
$path = $this->locator->locate($resource['path'], $this->currentDirectory); | ||
if (!is_dir($path)) { | ||
return new RouteCollection(); | ||
} | ||
|
@@ -47,12 +51,33 @@ public function supports(mixed $resource, string $type = null): bool | |
return ('attribute' === $type || 'annotation' === $type) && \is_array($resource) && isset($resource['path'], $resource['namespace']); | ||
} | ||
|
||
public function forDirectory(string $currentDirectory): static | ||
{ | ||
$loader = clone $this; | ||
$loader->currentDirectory = $currentDirectory; | ||
|
||
return $loader; | ||
} | ||
|
||
private function loadFromDirectory(string $directory, string $psr4Prefix): RouteCollection | ||
{ | ||
$collection = new RouteCollection(); | ||
$collection->addResource(new DirectoryResource($directory, '/\.php$/')); | ||
$files = iterator_to_array(new \RecursiveIteratorIterator( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've copy/pasted the logic from the annotation directory loader to keep the same file order. If not, the routes won't be registered in the same order and that could be a BC break. |
||
new \RecursiveCallbackFilterIterator( | ||
new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS), | ||
function (\SplFileInfo $current) { | ||
return !str_starts_with($current->getBasename(), '.'); | ||
} | ||
), | ||
\RecursiveIteratorIterator::LEAVES_ONLY | ||
)); | ||
usort($files, function (\SplFileInfo $a, \SplFileInfo $b) { | ||
return (string) $a > (string) $b ? 1 : -1; | ||
}); | ||
|
||
/** @var \SplFileInfo $file */ | ||
foreach (new \FilesystemIterator($directory) as $file) { | ||
foreach ($files as $file) { | ||
if ($file->isDir()) { | ||
$collection->addCollection($this->loadFromDirectory($file->getPathname(), $psr4Prefix.'\\'.$file->getFilename())); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<routes xmlns="http://symfony.com/schema/routing" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/routing | ||
https://symfony.com/schema/routing/routing-1.0.xsd"> | ||
|
||
<import resource="psr4-controllers-redirection/psr4-attributes.xml" /> | ||
</routes> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
controllers: | ||
resource: psr4-controllers-redirection/psr4-attributes.yaml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<routes xmlns="http://symfony.com/schema/routing" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/routing | ||
https://symfony.com/schema/routing/routing-1.0.xsd"> | ||
|
||
<import prefix="/my-prefix" type="attribute"> | ||
<resource path="../Psr4Controllers" namespace="Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers" /> | ||
</import> | ||
</routes> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
my_controllers: | ||
resource: | ||
path: ../Psr4Controllers | ||
namespace: Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers | ||
type: attribute | ||
prefix: /my-prefix |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was missing