diff options
author | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-03 19:26:54 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-03 19:26:54 +0200 |
commit | 4f5b44bd3bd490309eb2ba7b44df4769816ba729 (patch) | |
tree | 6cefe170dfe0a5a361cb1e2d1fc4d580a3316d02 /vendor/symfony/routing/Symfony/Component/Routing/Loader/AnnotationClassLoader.php | |
parent | 2b840e0cfb63a453bea67a98541f3df9c273c5f5 (diff) | |
download | wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.tar.gz wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.tar.zst wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.zip |
twig implementation
Diffstat (limited to 'vendor/symfony/routing/Symfony/Component/Routing/Loader/AnnotationClassLoader.php')
-rw-r--r-- | vendor/symfony/routing/Symfony/Component/Routing/Loader/AnnotationClassLoader.php | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/vendor/symfony/routing/Symfony/Component/Routing/Loader/AnnotationClassLoader.php b/vendor/symfony/routing/Symfony/Component/Routing/Loader/AnnotationClassLoader.php new file mode 100644 index 00000000..9831d85a --- /dev/null +++ b/vendor/symfony/routing/Symfony/Component/Routing/Loader/AnnotationClassLoader.php | |||
@@ -0,0 +1,246 @@ | |||
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 Doctrine\Common\Annotations\Reader; | ||
15 | use Symfony\Component\Config\Resource\FileResource; | ||
16 | use Symfony\Component\Routing\Route; | ||
17 | use Symfony\Component\Routing\RouteCollection; | ||
18 | use Symfony\Component\Config\Loader\LoaderInterface; | ||
19 | use Symfony\Component\Config\Loader\LoaderResolverInterface; | ||
20 | |||
21 | /** | ||
22 | * AnnotationClassLoader loads routing information from a PHP class and its methods. | ||
23 | * | ||
24 | * You need to define an implementation for the getRouteDefaults() method. Most of the | ||
25 | * time, this method should define some PHP callable to be called for the route | ||
26 | * (a controller in MVC speak). | ||
27 | * | ||
28 | * The @Route annotation can be set on the class (for global parameters), | ||
29 | * and on each method. | ||
30 | * | ||
31 | * The @Route annotation main value is the route path. The annotation also | ||
32 | * recognizes several parameters: requirements, options, defaults, schemes, | ||
33 | * methods, host, and name. The name parameter is mandatory. | ||
34 | * Here is an example of how you should be able to use it: | ||
35 | * | ||
36 | * /** | ||
37 | * * @Route("/Blog") | ||
38 | * * / | ||
39 | * class Blog | ||
40 | * { | ||
41 | * /** | ||
42 | * * @Route("/", name="blog_index") | ||
43 | * * / | ||
44 | * public function index() | ||
45 | * { | ||
46 | * } | ||
47 | * | ||
48 | * /** | ||
49 | * * @Route("/{id}", name="blog_post", requirements = {"id" = "\d+"}) | ||
50 | * * / | ||
51 | * public function show() | ||
52 | * { | ||
53 | * } | ||
54 | * } | ||
55 | * | ||
56 | * @author Fabien Potencier <fabien@symfony.com> | ||
57 | */ | ||
58 | abstract class AnnotationClassLoader implements LoaderInterface | ||
59 | { | ||
60 | /** | ||
61 | * @var Reader | ||
62 | */ | ||
63 | protected $reader; | ||
64 | |||
65 | /** | ||
66 | * @var string | ||
67 | */ | ||
68 | protected $routeAnnotationClass = 'Symfony\\Component\\Routing\\Annotation\\Route'; | ||
69 | |||
70 | /** | ||
71 | * @var integer | ||
72 | */ | ||
73 | protected $defaultRouteIndex = 0; | ||
74 | |||
75 | /** | ||
76 | * Constructor. | ||
77 | * | ||
78 | * @param Reader $reader | ||
79 | */ | ||
80 | public function __construct(Reader $reader) | ||
81 | { | ||
82 | $this->reader = $reader; | ||
83 | } | ||
84 | |||
85 | /** | ||
86 | * Sets the annotation class to read route properties from. | ||
87 | * | ||
88 | * @param string $class A fully-qualified class name | ||
89 | */ | ||
90 | public function setRouteAnnotationClass($class) | ||
91 | { | ||
92 | $this->routeAnnotationClass = $class; | ||
93 | } | ||
94 | |||
95 | /** | ||
96 | * Loads from annotations from a class. | ||
97 | * | ||
98 | * @param string $class A class name | ||
99 | * @param string|null $type The resource type | ||
100 | * | ||
101 | * @return RouteCollection A RouteCollection instance | ||
102 | * | ||
103 | * @throws \InvalidArgumentException When route can't be parsed | ||
104 | */ | ||
105 | public function load($class, $type = null) | ||
106 | { | ||
107 | if (!class_exists($class)) { | ||
108 | throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class)); | ||
109 | } | ||
110 | |||
111 | $globals = array( | ||
112 | 'path' => '', | ||
113 | 'requirements' => array(), | ||
114 | 'options' => array(), | ||
115 | 'defaults' => array(), | ||
116 | 'schemes' => array(), | ||
117 | 'methods' => array(), | ||
118 | 'host' => '', | ||
119 | ); | ||
120 | |||
121 | $class = new \ReflectionClass($class); | ||
122 | if ($class->isAbstract()) { | ||
123 | throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class)); | ||
124 | } | ||
125 | |||
126 | if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) { | ||
127 | // for BC reasons | ||
128 | if (null !== $annot->getPath()) { | ||
129 | $globals['path'] = $annot->getPath(); | ||
130 | } elseif (null !== $annot->getPattern()) { | ||
131 | $globals['path'] = $annot->getPattern(); | ||
132 | } | ||
133 | |||
134 | if (null !== $annot->getRequirements()) { | ||
135 | $globals['requirements'] = $annot->getRequirements(); | ||
136 | } | ||
137 | |||
138 | if (null !== $annot->getOptions()) { | ||
139 | $globals['options'] = $annot->getOptions(); | ||
140 | } | ||
141 | |||
142 | if (null !== $annot->getDefaults()) { | ||
143 | $globals['defaults'] = $annot->getDefaults(); | ||
144 | } | ||
145 | |||
146 | if (null !== $annot->getSchemes()) { | ||
147 | $globals['schemes'] = $annot->getSchemes(); | ||
148 | } | ||
149 | |||
150 | if (null !== $annot->getMethods()) { | ||
151 | $globals['methods'] = $annot->getMethods(); | ||
152 | } | ||
153 | |||
154 | if (null !== $annot->getHost()) { | ||
155 | $globals['host'] = $annot->getHost(); | ||
156 | } | ||
157 | } | ||
158 | |||
159 | $collection = new RouteCollection(); | ||
160 | $collection->addResource(new FileResource($class->getFileName())); | ||
161 | |||
162 | foreach ($class->getMethods() as $method) { | ||
163 | $this->defaultRouteIndex = 0; | ||
164 | foreach ($this->reader->getMethodAnnotations($method) as $annot) { | ||
165 | if ($annot instanceof $this->routeAnnotationClass) { | ||
166 | $this->addRoute($collection, $annot, $globals, $class, $method); | ||
167 | } | ||
168 | } | ||
169 | } | ||
170 | |||
171 | return $collection; | ||
172 | } | ||
173 | |||
174 | protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method) | ||
175 | { | ||
176 | $name = $annot->getName(); | ||
177 | if (null === $name) { | ||
178 | $name = $this->getDefaultRouteName($class, $method); | ||
179 | } | ||
180 | |||
181 | $defaults = array_replace($globals['defaults'], $annot->getDefaults()); | ||
182 | foreach ($method->getParameters() as $param) { | ||
183 | if ($param->isOptional()) { | ||
184 | $defaults[$param->getName()] = $param->getDefaultValue(); | ||
185 | } | ||
186 | } | ||
187 | $requirements = array_replace($globals['requirements'], $annot->getRequirements()); | ||
188 | $options = array_replace($globals['options'], $annot->getOptions()); | ||
189 | $schemes = array_replace($globals['schemes'], $annot->getSchemes()); | ||
190 | $methods = array_replace($globals['methods'], $annot->getMethods()); | ||
191 | |||
192 | $host = $annot->getHost(); | ||
193 | if (null === $host) { | ||
194 | $host = $globals['host']; | ||
195 | } | ||
196 | |||
197 | $route = new Route($globals['path'].$annot->getPath(), $defaults, $requirements, $options, $host, $schemes, $methods); | ||
198 | |||
199 | $this->configureRoute($route, $class, $method, $annot); | ||
200 | |||
201 | $collection->add($name, $route); | ||
202 | } | ||
203 | |||
204 | /** | ||
205 | * {@inheritdoc} | ||
206 | */ | ||
207 | public function supports($resource, $type = null) | ||
208 | { | ||
209 | return is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type); | ||
210 | } | ||
211 | |||
212 | /** | ||
213 | * {@inheritdoc} | ||
214 | */ | ||
215 | public function setResolver(LoaderResolverInterface $resolver) | ||
216 | { | ||
217 | } | ||
218 | |||
219 | /** | ||
220 | * {@inheritdoc} | ||
221 | */ | ||
222 | public function getResolver() | ||
223 | { | ||
224 | } | ||
225 | |||
226 | /** | ||
227 | * Gets the default route name for a class method. | ||
228 | * | ||
229 | * @param \ReflectionClass $class | ||
230 | * @param \ReflectionMethod $method | ||
231 | * | ||
232 | * @return string | ||
233 | */ | ||
234 | protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method) | ||
235 | { | ||
236 | $name = strtolower(str_replace('\\', '_', $class->name).'_'.$method->name); | ||
237 | if ($this->defaultRouteIndex > 0) { | ||
238 | $name .= '_'.$this->defaultRouteIndex; | ||
239 | } | ||
240 | $this->defaultRouteIndex++; | ||
241 | |||
242 | return $name; | ||
243 | } | ||
244 | |||
245 | abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot); | ||
246 | } | ||