This commit is contained in:
1 parent
f7c532a648
commit
640bd67556
5801 files changed
+484532
-55857
No files matched your search
+16
-5
@@ -15,6 +15,7 @@ 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\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
|
||||
/**
|
||||
@@ -38,12 +39,17 @@ final class Psr4DirectoryLoader extends Loader implements DirectoryAwareLoaderIn
|
||||
*/
|
||||
public function load(mixed $resource, ?string $type = null): ?RouteCollection
|
||||
{
|
||||
$excluded = $resource['_excluded'] ?? [];
|
||||
$path = $this->locator->locate($resource['path'], $this->currentDirectory);
|
||||
if (!is_dir($path)) {
|
||||
return new RouteCollection();
|
||||
}
|
||||
|
||||
return $this->loadFromDirectory($path, trim($resource['namespace'], '\\'));
|
||||
if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\)++$/', trim($resource['namespace'], '\\').'\\')) {
|
||||
throw new InvalidArgumentException(\sprintf('Namespace "%s" is not a valid PSR-4 prefix.', $resource['namespace']));
|
||||
}
|
||||
|
||||
return $this->loadFromDirectory($path, trim($resource['namespace'], '\\'), $excluded);
|
||||
}
|
||||
|
||||
public function supports(mixed $resource, ?string $type = null): bool
|
||||
@@ -59,23 +65,28 @@ final class Psr4DirectoryLoader extends Loader implements DirectoryAwareLoaderIn
|
||||
return $loader;
|
||||
}
|
||||
|
||||
private function loadFromDirectory(string $directory, string $psr4Prefix): RouteCollection
|
||||
private function loadFromDirectory(string $directory, string $psr4Prefix, array $excluded = []): RouteCollection
|
||||
{
|
||||
$collection = new RouteCollection();
|
||||
$collection->addResource(new DirectoryResource($directory, '/\.php$/'));
|
||||
$files = iterator_to_array(new \RecursiveIteratorIterator(
|
||||
new \RecursiveCallbackFilterIterator(
|
||||
new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
|
||||
fn (\SplFileInfo $current) => !str_starts_with($current->getBasename(), '.')
|
||||
static fn (\SplFileInfo $current) => !str_starts_with($current->getBasename(), '.')
|
||||
),
|
||||
\RecursiveIteratorIterator::SELF_FIRST
|
||||
));
|
||||
usort($files, fn (\SplFileInfo $a, \SplFileInfo $b) => (string) $a > (string) $b ? 1 : -1);
|
||||
usort($files, static fn (\SplFileInfo $a, \SplFileInfo $b) => (string) $a > (string) $b ? 1 : -1);
|
||||
|
||||
/** @var \SplFileInfo $file */
|
||||
foreach ($files as $file) {
|
||||
$normalizedPath = rtrim(str_replace('\\', '/', $file->getPathname()), '/');
|
||||
if (isset($excluded[$normalizedPath])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($file->isDir()) {
|
||||
$collection->addCollection($this->loadFromDirectory($file->getPathname(), $psr4Prefix.'\\'.$file->getFilename()));
|
||||
$collection->addCollection($this->loadFromDirectory($file->getPathname(), $psr4Prefix.'\\'.$file->getFilename(), $excluded));
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user