7b8467a81c0dafb0369731475962bb1399263f51
[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 }
90
91 /**
92  * @internal To be removed as RecursiveCallbackFilterIterator is available since PHP 5.4
93  */
94 class RecursiveCallbackFilterIterator extends \FilterIterator implements \RecursiveIterator
95 {
96     private $iterator;
97     private $callback;
98
99     public function __construct(\RecursiveIterator $iterator, $callback)
100     {
101         $this->iterator = $iterator;
102         $this->callback = $callback;
103         parent::__construct($iterator);
104     }
105
106     public function accept()
107     {
108         return call_user_func($this->callback, $this->current(), $this->key(), $this->iterator);
109     }
110
111     public function hasChildren()
112     {
113         return $this->iterator->hasChildren();
114     }
115
116     public function getChildren()
117     {
118         return new static($this->iterator->getChildren(), $this->callback);
119     }
120 }