Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Routing\Loader;
|
Chris@0
|
13
|
Chris@17
|
14 use Symfony\Component\Config\Resource\DirectoryResource;
|
Chris@0
|
15 use Symfony\Component\Routing\RouteCollection;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * AnnotationDirectoryLoader loads routing information from annotations set
|
Chris@0
|
19 * on PHP classes and methods.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
22 */
|
Chris@0
|
23 class AnnotationDirectoryLoader extends AnnotationFileLoader
|
Chris@0
|
24 {
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Loads from annotations from a directory.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @param string $path A directory path
|
Chris@0
|
29 * @param string|null $type The resource type
|
Chris@0
|
30 *
|
Chris@0
|
31 * @return RouteCollection A RouteCollection instance
|
Chris@0
|
32 *
|
Chris@0
|
33 * @throws \InvalidArgumentException When the directory does not exist or its routes cannot be parsed
|
Chris@0
|
34 */
|
Chris@0
|
35 public function load($path, $type = null)
|
Chris@0
|
36 {
|
Chris@14
|
37 if (!is_dir($dir = $this->locator->locate($path))) {
|
Chris@14
|
38 return parent::supports($path, $type) ? parent::load($path, $type) : new RouteCollection();
|
Chris@14
|
39 }
|
Chris@0
|
40
|
Chris@0
|
41 $collection = new RouteCollection();
|
Chris@0
|
42 $collection->addResource(new DirectoryResource($dir, '/\.php$/'));
|
Chris@0
|
43 $files = iterator_to_array(new \RecursiveIteratorIterator(
|
Chris@0
|
44 new \RecursiveCallbackFilterIterator(
|
Chris@14
|
45 new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
|
Chris@0
|
46 function (\SplFileInfo $current) {
|
Chris@0
|
47 return '.' !== substr($current->getBasename(), 0, 1);
|
Chris@0
|
48 }
|
Chris@0
|
49 ),
|
Chris@0
|
50 \RecursiveIteratorIterator::LEAVES_ONLY
|
Chris@0
|
51 ));
|
Chris@0
|
52 usort($files, function (\SplFileInfo $a, \SplFileInfo $b) {
|
Chris@0
|
53 return (string) $a > (string) $b ? 1 : -1;
|
Chris@0
|
54 });
|
Chris@0
|
55
|
Chris@0
|
56 foreach ($files as $file) {
|
Chris@0
|
57 if (!$file->isFile() || '.php' !== substr($file->getFilename(), -4)) {
|
Chris@0
|
58 continue;
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 if ($class = $this->findClass($file)) {
|
Chris@0
|
62 $refl = new \ReflectionClass($class);
|
Chris@0
|
63 if ($refl->isAbstract()) {
|
Chris@0
|
64 continue;
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 $collection->addCollection($this->loader->load($class, $type));
|
Chris@0
|
68 }
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 return $collection;
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * {@inheritdoc}
|
Chris@0
|
76 */
|
Chris@0
|
77 public function supports($resource, $type = null)
|
Chris@0
|
78 {
|
Chris@14
|
79 if ('annotation' === $type) {
|
Chris@14
|
80 return true;
|
Chris@14
|
81 }
|
Chris@14
|
82
|
Chris@17
|
83 if ($type || !\is_string($resource)) {
|
Chris@0
|
84 return false;
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 try {
|
Chris@14
|
88 return is_dir($this->locator->locate($resource));
|
Chris@0
|
89 } catch (\Exception $e) {
|
Chris@0
|
90 return false;
|
Chris@0
|
91 }
|
Chris@0
|
92 }
|
Chris@0
|
93 }
|