AttributeDirectoryLoader.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing\Loader;
  11. use Symfony\Component\Config\Resource\DirectoryResource;
  12. use Symfony\Component\Routing\RouteCollection;
  13. /**
  14. * AttributeDirectoryLoader loads routing information from attributes set
  15. * on PHP classes and methods.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. * @author Alexandre Daubois <alex.daubois@gmail.com>
  19. */
  20. class AttributeDirectoryLoader extends AttributeFileLoader
  21. {
  22. /**
  23. * @throws \InvalidArgumentException When the directory does not exist or its routes cannot be parsed
  24. */
  25. public function load(mixed $path, ?string $type = null): ?RouteCollection
  26. {
  27. if (!is_dir($dir = $this->locator->locate($path))) {
  28. return parent::supports($path, $type) ? parent::load($path, $type) : new RouteCollection();
  29. }
  30. $collection = new RouteCollection();
  31. $collection->addResource(new DirectoryResource($dir, '/\.php$/'));
  32. $files = iterator_to_array(new \RecursiveIteratorIterator(
  33. new \RecursiveCallbackFilterIterator(
  34. new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
  35. fn (\SplFileInfo $current) => !str_starts_with($current->getBasename(), '.')
  36. ),
  37. \RecursiveIteratorIterator::LEAVES_ONLY
  38. ));
  39. usort($files, fn (\SplFileInfo $a, \SplFileInfo $b) => (string) $a > (string) $b ? 1 : -1);
  40. foreach ($files as $file) {
  41. if (!$file->isFile() || !str_ends_with($file->getFilename(), '.php')) {
  42. continue;
  43. }
  44. if ($class = $this->findClass($file)) {
  45. $refl = new \ReflectionClass($class);
  46. if ($refl->isAbstract()) {
  47. continue;
  48. }
  49. $collection->addCollection($this->loader->load($class, $type));
  50. }
  51. }
  52. return $collection;
  53. }
  54. public function supports(mixed $resource, ?string $type = null): bool
  55. {
  56. if (!\is_string($resource)) {
  57. return false;
  58. }
  59. if (\in_array($type, ['annotation', 'attribute'], true)) {
  60. if ('annotation' === $type) {
  61. trigger_deprecation('symfony/routing', '6.4', 'The "annotation" route type is deprecated, use the "attribute" route type instead.');
  62. }
  63. return true;
  64. }
  65. if ($type) {
  66. return false;
  67. }
  68. try {
  69. return is_dir($this->locator->locate($resource));
  70. } catch (\Exception) {
  71. return false;
  72. }
  73. }
  74. }
  75. if (!class_exists(AnnotationDirectoryLoader::class, false)) {
  76. class_alias(AttributeDirectoryLoader::class, AnnotationDirectoryLoader::class);
  77. }