| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
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\Loader;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Config\Resource\DirectoryResource;
|
|
|
15 |
use Symfony\Component\Routing\RouteCollection;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* AnnotationDirectoryLoader loads routing information from annotations set
|
|
|
19 |
* on PHP classes and methods.
|
|
|
20 |
*
|
|
|
21 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
22 |
*/
|
|
|
23 |
class AnnotationDirectoryLoader extends AnnotationFileLoader
|
|
|
24 |
{
|
|
|
25 |
/**
|
|
|
26 |
* @throws \InvalidArgumentException When the directory does not exist or its routes cannot be parsed
|
|
|
27 |
*/
|
|
|
28 |
public function load(mixed $path, string $type = null): ?RouteCollection
|
|
|
29 |
{
|
|
|
30 |
if (!is_dir($dir = $this->locator->locate($path))) {
|
|
|
31 |
return parent::supports($path, $type) ? parent::load($path, $type) : new RouteCollection();
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
$collection = new RouteCollection();
|
|
|
35 |
$collection->addResource(new DirectoryResource($dir, '/\.php$/'));
|
|
|
36 |
$files = iterator_to_array(new \RecursiveIteratorIterator(
|
|
|
37 |
new \RecursiveCallbackFilterIterator(
|
|
|
38 |
new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
|
|
|
39 |
function (\SplFileInfo $current) {
|
|
|
40 |
return !str_starts_with($current->getBasename(), '.');
|
|
|
41 |
}
|
|
|
42 |
),
|
|
|
43 |
\RecursiveIteratorIterator::LEAVES_ONLY
|
|
|
44 |
));
|
|
|
45 |
usort($files, function (\SplFileInfo $a, \SplFileInfo $b) {
|
|
|
46 |
return (string) $a > (string) $b ? 1 : -1;
|
|
|
47 |
});
|
|
|
48 |
|
|
|
49 |
foreach ($files as $file) {
|
|
|
50 |
if (!$file->isFile() || !str_ends_with($file->getFilename(), '.php')) {
|
|
|
51 |
continue;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
if ($class = $this->findClass($file)) {
|
|
|
55 |
$refl = new \ReflectionClass($class);
|
|
|
56 |
if ($refl->isAbstract()) {
|
|
|
57 |
continue;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
$collection->addCollection($this->loader->load($class, $type));
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
return $collection;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
public function supports(mixed $resource, string $type = null): bool
|
|
|
68 |
{
|
|
|
69 |
if (!\is_string($resource)) {
|
|
|
70 |
return false;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
if (\in_array($type, ['annotation', 'attribute'], true)) {
|
|
|
74 |
return true;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
if ($type) {
|
|
|
78 |
return false;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
try {
|
|
|
82 |
return is_dir($this->locator->locate($resource));
|
|
|
83 |
} catch (\Exception) {
|
|
|
84 |
return false;
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
}
|