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\Routing\RouteCollection;
15 use Symfony\Component\Config\Resource\DirectoryResource;
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 $dir = $this->locator->locate($path);
39 $collection = new RouteCollection();
40 $collection->addResource(new DirectoryResource($dir, '/\.php$/'));
41 $files = iterator_to_array(new \RecursiveIteratorIterator(
42 new \RecursiveCallbackFilterIterator(
43 new \RecursiveDirectoryIterator($dir),
44 function (\SplFileInfo $current) {
45 return '.' !== substr($current->getBasename(), 0, 1);
48 \RecursiveIteratorIterator::LEAVES_ONLY
50 usort($files, function (\SplFileInfo $a, \SplFileInfo $b) {
51 return (string) $a > (string) $b ? 1 : -1;
54 foreach ($files as $file) {
55 if (!$file->isFile() || '.php' !== substr($file->getFilename(), -4)) {
59 if ($class = $this->findClass($file)) {
60 $refl = new \ReflectionClass($class);
61 if ($refl->isAbstract()) {
65 $collection->addCollection($this->loader->load($class, $type));
75 public function supports($resource, $type = null)
77 if (!is_string($resource)) {
82 $path = $this->locator->locate($resource);
83 } catch (\Exception $e) {
87 return is_dir($path) && (!$type || 'annotation' === $type);