Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 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\Config\Resource\DirectoryResource;
15
use Symfony\Component\Routing\RouteCollection;
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, string $type = null)
36
    {
37
        if (!is_dir($dir = $this->locator->locate($path))) {
38
            return parent::supports($path, $type) ? parent::load($path, $type) : new RouteCollection();
39
        }
40
 
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);
48
                }
49
            ),
50
            \RecursiveIteratorIterator::LEAVES_ONLY
51
        ));
52
        usort($files, function (\SplFileInfo $a, \SplFileInfo $b) {
53
            return (string) $a > (string) $b ? 1 : -1;
54
        });
55
 
56
        foreach ($files as $file) {
57
            if (!$file->isFile() || '.php' !== substr($file->getFilename(), -4)) {
58
                continue;
59
            }
60
 
61
            if ($class = $this->findClass($file)) {
62
                $refl = new \ReflectionClass($class);
63
                if ($refl->isAbstract()) {
64
                    continue;
65
                }
66
 
67
                $collection->addCollection($this->loader->load($class, $type));
68
            }
69
        }
70
 
71
        return $collection;
72
    }
73
 
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function supports($resource, string $type = null)
78
    {
79
        if ('annotation' === $type) {
80
            return true;
81
        }
82
 
83
        if ($type || !\is_string($resource)) {
84
            return false;
85
        }
86
 
87
        try {
88
            return is_dir($this->locator->locate($resource));
89
        } catch (\Exception $e) {
90
            return false;
91
        }
92
    }
93
}