diff options
Diffstat (limited to 'vendor/symfony/routing/Symfony/Component/Routing/Loader/AnnotationFileLoader.php')
-rw-r--r-- | vendor/symfony/routing/Symfony/Component/Routing/Loader/AnnotationFileLoader.php | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/vendor/symfony/routing/Symfony/Component/Routing/Loader/AnnotationFileLoader.php b/vendor/symfony/routing/Symfony/Component/Routing/Loader/AnnotationFileLoader.php new file mode 100644 index 00000000..33776fdc --- /dev/null +++ b/vendor/symfony/routing/Symfony/Component/Routing/Loader/AnnotationFileLoader.php | |||
@@ -0,0 +1,122 @@ | |||
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\FileResource; | ||
16 | use Symfony\Component\Config\Loader\FileLoader; | ||
17 | use Symfony\Component\Config\FileLocatorInterface; | ||
18 | |||
19 | /** | ||
20 | * AnnotationFileLoader loads routing information from annotations set | ||
21 | * on a PHP class and its methods. | ||
22 | * | ||
23 | * @author Fabien Potencier <fabien@symfony.com> | ||
24 | */ | ||
25 | class AnnotationFileLoader extends FileLoader | ||
26 | { | ||
27 | protected $loader; | ||
28 | |||
29 | /** | ||
30 | * Constructor. | ||
31 | * | ||
32 | * @param FileLocatorInterface $locator A FileLocator instance | ||
33 | * @param AnnotationClassLoader $loader An AnnotationClassLoader instance | ||
34 | * @param string|array $paths A path or an array of paths where to look for resources | ||
35 | * | ||
36 | * @throws \RuntimeException | ||
37 | */ | ||
38 | public function __construct(FileLocatorInterface $locator, AnnotationClassLoader $loader, $paths = array()) | ||
39 | { | ||
40 | if (!function_exists('token_get_all')) { | ||
41 | throw new \RuntimeException('The Tokenizer extension is required for the routing annotation loaders.'); | ||
42 | } | ||
43 | |||
44 | parent::__construct($locator, $paths); | ||
45 | |||
46 | $this->loader = $loader; | ||
47 | } | ||
48 | |||
49 | /** | ||
50 | * Loads from annotations from a file. | ||
51 | * | ||
52 | * @param string $file A PHP file path | ||
53 | * @param string|null $type The resource type | ||
54 | * | ||
55 | * @return RouteCollection A RouteCollection instance | ||
56 | * | ||
57 | * @throws \InvalidArgumentException When the file does not exist or its routes cannot be parsed | ||
58 | */ | ||
59 | public function load($file, $type = null) | ||
60 | { | ||
61 | $path = $this->locator->locate($file); | ||
62 | |||
63 | $collection = new RouteCollection(); | ||
64 | if ($class = $this->findClass($path)) { | ||
65 | $collection->addResource(new FileResource($path)); | ||
66 | $collection->addCollection($this->loader->load($class, $type)); | ||
67 | } | ||
68 | |||
69 | return $collection; | ||
70 | } | ||
71 | |||
72 | /** | ||
73 | * {@inheritdoc} | ||
74 | */ | ||
75 | public function supports($resource, $type = null) | ||
76 | { | ||
77 | return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'annotation' === $type); | ||
78 | } | ||
79 | |||
80 | /** | ||
81 | * Returns the full class name for the first class in the file. | ||
82 | * | ||
83 | * @param string $file A PHP file path | ||
84 | * | ||
85 | * @return string|false Full class name if found, false otherwise | ||
86 | */ | ||
87 | protected function findClass($file) | ||
88 | { | ||
89 | $class = false; | ||
90 | $namespace = false; | ||
91 | $tokens = token_get_all(file_get_contents($file)); | ||
92 | for ($i = 0, $count = count($tokens); $i < $count; $i++) { | ||
93 | $token = $tokens[$i]; | ||
94 | |||
95 | if (!is_array($token)) { | ||
96 | continue; | ||
97 | } | ||
98 | |||
99 | if (true === $class && T_STRING === $token[0]) { | ||
100 | return $namespace.'\\'.$token[1]; | ||
101 | } | ||
102 | |||
103 | if (true === $namespace && T_STRING === $token[0]) { | ||
104 | $namespace = ''; | ||
105 | do { | ||
106 | $namespace .= $token[1]; | ||
107 | $token = $tokens[++$i]; | ||
108 | } while ($i < $count && is_array($token) && in_array($token[0], array(T_NS_SEPARATOR, T_STRING))); | ||
109 | } | ||
110 | |||
111 | if (T_CLASS === $token[0]) { | ||
112 | $class = true; | ||
113 | } | ||
114 | |||
115 | if (T_NAMESPACE === $token[0]) { | ||
116 | $namespace = true; | ||
117 | } | ||
118 | } | ||
119 | |||
120 | return false; | ||
121 | } | ||
122 | } | ||