4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Loader;
14 use Symfony\Component\Config\Resource\DirectoryResource;
15 use Symfony\Component\Routing\RouteCollection;
18 * AnnotationDirectoryLoader loads routing information from annotations set
19 * on PHP classes and methods.
21 * @author Fabien Potencier <fabien@symfony.com>
23 class AnnotationDirectoryLoader extends AnnotationFileLoader
26 * Loads from annotations from a directory.
28 * @param string $path A directory path
29 * @param string|null $type The resource type
31 * @return RouteCollection A RouteCollection instance
33 * @throws \InvalidArgumentException When the directory does not exist or its routes cannot be parsed
35 public function load($path, $type = null)
37 if (!is_dir($dir = $this->locator->locate($path))) {
38 return parent::supports($path, $type) ? parent::load($path, $type) : new RouteCollection();
41 $collection = new RouteCollection();
42 $collection->addResource(new DirectoryResource($dir, '/\.php$/'));
43 $files = iterator_to_array(new \RecursiveIteratorIterator(
44 new \RecursiveCallbackFilterIterator(
45 new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
46 function (\SplFileInfo $current) {
47 return '.' !== substr($current->getBasename(), 0, 1);
50 \RecursiveIteratorIterator::LEAVES_ONLY
52 usort($files, function (\SplFileInfo $a, \SplFileInfo $b) {
53 return (string) $a > (string) $b ? 1 : -1;
56 foreach ($files as $file) {
57 if (!$file->isFile() || '.php' !== substr($file->getFilename(), -4)) {
61 if ($class = $this->findClass($file)) {
62 $refl = new \ReflectionClass($class);
63 if ($refl->isAbstract()) {
67 $collection->addCollection($this->loader->load($class, $type));
77 public function supports($resource, $type = null)
79 if ('annotation' === $type) {
83 if ($type || !\is_string($resource)) {
88 return is_dir($this->locator->locate($resource));
89 } catch (\Exception $e) {