2e9e10d3447380937df5040eec3085e9df05cbd5
[yaffs-website] / vendor / symfony / routing / Loader / AnnotationDirectoryLoader.php
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\Routing\RouteCollection;
15 use Symfony\Component\Config\Resource\DirectoryResource;
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      * Loads from annotations from a directory.
27      *
28      * @param string      $path A directory path
29      * @param string|null $type The resource type
30      *
31      * @return RouteCollection A RouteCollection instance
32      *
33      * @throws \InvalidArgumentException When the directory does not exist or its routes cannot be parsed
34      */
35     public function load($path, $type = null)
36     {
37         $dir = $this->locator->locate($path);
38
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);
46                 }
47             ),
48             \RecursiveIteratorIterator::LEAVES_ONLY
49         ));
50         usort($files, function (\SplFileInfo $a, \SplFileInfo $b) {
51             return (string) $a > (string) $b ? 1 : -1;
52         });
53
54         foreach ($files as $file) {
55             if (!$file->isFile() || '.php' !== substr($file->getFilename(), -4)) {
56                 continue;
57             }
58
59             if ($class = $this->findClass($file)) {
60                 $refl = new \ReflectionClass($class);
61                 if ($refl->isAbstract()) {
62                     continue;
63                 }
64
65                 $collection->addCollection($this->loader->load($class, $type));
66             }
67         }
68
69         return $collection;
70     }
71
72     /**
73      * {@inheritdoc}
74      */
75     public function supports($resource, $type = null)
76     {
77         if (!is_string($resource)) {
78             return false;
79         }
80
81         try {
82             $path = $this->locator->locate($resource);
83         } catch (\Exception $e) {
84             return false;
85         }
86
87         return is_dir($path) && (!$type || 'annotation' === $type);
88     }
89 }