AnnotationDirectoryLoader.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Routing\RouteCollection;
  12. use Symfony\Component\Config\Resource\DirectoryResource;
  13. /**
  14. * AnnotationDirectoryLoader loads routing information from annotations set
  15. * on PHP classes and methods.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class AnnotationDirectoryLoader extends AnnotationFileLoader
  20. {
  21. /**
  22. * Loads from annotations from a directory.
  23. *
  24. * @param string $path A directory path
  25. * @param string|null $type The resource type
  26. *
  27. * @return RouteCollection A RouteCollection instance
  28. *
  29. * @throws \InvalidArgumentException When the directory does not exist or its routes cannot be parsed
  30. */
  31. public function load($path, $type = null)
  32. {
  33. $dir = $this->locator->locate($path);
  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 '.' !== substr($current->getBasename(), 0, 1);
  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. foreach ($files as $file) {
  49. if (!$file->isFile() || '.php' !== substr($file->getFilename(), -4)) {
  50. continue;
  51. }
  52. if ($class = $this->findClass($file)) {
  53. $refl = new \ReflectionClass($class);
  54. if ($refl->isAbstract()) {
  55. continue;
  56. }
  57. $collection->addCollection($this->loader->load($class, $type));
  58. }
  59. }
  60. return $collection;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function supports($resource, $type = null)
  66. {
  67. if (!is_string($resource)) {
  68. return false;
  69. }
  70. try {
  71. $path = $this->locator->locate($resource);
  72. } catch (\Exception $e) {
  73. return false;
  74. }
  75. return is_dir($path) && (!$type || 'annotation' === $type);
  76. }
  77. }