|
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\Routing\Annotation\Route as RouteAnnotation; |
|
16 use Symfony\Component\Config\Resource\FileResource; |
|
17 use Symfony\Component\Routing\Route; |
|
18 use Symfony\Component\Routing\RouteCollection; |
|
19 use Symfony\Component\Config\Loader\LoaderInterface; |
|
20 use Symfony\Component\Config\Loader\LoaderResolver; |
|
21 |
|
22 /** |
|
23 * AnnotationClassLoader loads routing information from a PHP class and its methods. |
|
24 * |
|
25 * You need to define an implementation for the getRouteDefaults() method. Most of the |
|
26 * time, this method should define some PHP callable to be called for the route |
|
27 * (a controller in MVC speak). |
|
28 * |
|
29 * The @Route annotation can be set on the class (for global parameters), |
|
30 * and on each method. |
|
31 * |
|
32 * The @Route annotation main value is the route pattern. The annotation also |
|
33 * recognizes three parameters: requirements, options, and name. The name parameter |
|
34 * is mandatory. 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 protected $reader; |
|
61 protected $routeAnnotationClass = 'Symfony\\Component\\Routing\\Annotation\\Route'; |
|
62 protected $defaultRouteIndex; |
|
63 |
|
64 /** |
|
65 * Constructor. |
|
66 * |
|
67 * @param Reader $reader |
|
68 */ |
|
69 public function __construct(Reader $reader) |
|
70 { |
|
71 $this->reader = $reader; |
|
72 } |
|
73 |
|
74 /** |
|
75 * Sets the annotation class to read route properties from. |
|
76 * |
|
77 * @param string $class A fully-qualified class name |
|
78 */ |
|
79 public function setRouteAnnotationClass($class) |
|
80 { |
|
81 $this->routeAnnotationClass = $class; |
|
82 } |
|
83 |
|
84 /** |
|
85 * Loads from annotations from a class. |
|
86 * |
|
87 * @param string $class A class name |
|
88 * @param string $type The resource type |
|
89 * |
|
90 * @return RouteCollection A RouteCollection instance |
|
91 * |
|
92 * @throws \InvalidArgumentException When route can't be parsed |
|
93 */ |
|
94 public function load($class, $type = null) |
|
95 { |
|
96 if (!class_exists($class)) { |
|
97 throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class)); |
|
98 } |
|
99 |
|
100 $globals = array( |
|
101 'pattern' => '', |
|
102 'requirements' => array(), |
|
103 'options' => array(), |
|
104 'defaults' => array(), |
|
105 ); |
|
106 |
|
107 $class = new \ReflectionClass($class); |
|
108 if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) { |
|
109 if (null !== $annot->getPattern()) { |
|
110 $globals['pattern'] = $annot->getPattern(); |
|
111 } |
|
112 |
|
113 if (null !== $annot->getRequirements()) { |
|
114 $globals['requirements'] = $annot->getRequirements(); |
|
115 } |
|
116 |
|
117 if (null !== $annot->getOptions()) { |
|
118 $globals['options'] = $annot->getOptions(); |
|
119 } |
|
120 |
|
121 if (null !== $annot->getDefaults()) { |
|
122 $globals['defaults'] = $annot->getDefaults(); |
|
123 } |
|
124 } |
|
125 |
|
126 $collection = new RouteCollection(); |
|
127 $collection->addResource(new FileResource($class->getFileName())); |
|
128 |
|
129 foreach ($class->getMethods() as $method) { |
|
130 $this->defaultRouteIndex = 0; |
|
131 foreach ($this->reader->getMethodAnnotations($method) as $annot) { |
|
132 if ($annot instanceof $this->routeAnnotationClass) { |
|
133 $this->addRoute($collection, $annot, $globals, $class, $method); |
|
134 } |
|
135 } |
|
136 } |
|
137 |
|
138 return $collection; |
|
139 } |
|
140 |
|
141 protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method) |
|
142 { |
|
143 if (null === $annot->getName()) { |
|
144 $annot->setName($this->getDefaultRouteName($class, $method)); |
|
145 } |
|
146 |
|
147 $defaults = array_merge($globals['defaults'], $annot->getDefaults()); |
|
148 $requirements = array_merge($globals['requirements'], $annot->getRequirements()); |
|
149 $options = array_merge($globals['options'], $annot->getOptions()); |
|
150 |
|
151 $route = new Route($globals['pattern'].$annot->getPattern(), $defaults, $requirements, $options); |
|
152 |
|
153 $this->configureRoute($route, $class, $method, $annot); |
|
154 |
|
155 $collection->add($annot->getName(), $route); |
|
156 } |
|
157 |
|
158 /** |
|
159 * Returns true if this class supports the given resource. |
|
160 * |
|
161 * @param mixed $resource A resource |
|
162 * @param string $type The resource type |
|
163 * |
|
164 * @return Boolean True if this class supports the given resource, false otherwise |
|
165 */ |
|
166 public function supports($resource, $type = null) |
|
167 { |
|
168 return is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type); |
|
169 } |
|
170 |
|
171 /** |
|
172 * Sets the loader resolver. |
|
173 * |
|
174 * @param LoaderResolver $resolver A LoaderResolver instance |
|
175 */ |
|
176 public function setResolver(LoaderResolver $resolver) |
|
177 { |
|
178 } |
|
179 |
|
180 /** |
|
181 * Gets the loader resolver. |
|
182 * |
|
183 * @return LoaderResolver A LoaderResolver instance |
|
184 */ |
|
185 public function getResolver() |
|
186 { |
|
187 } |
|
188 |
|
189 /** |
|
190 * Gets the default route name for a class method. |
|
191 * |
|
192 * @param \ReflectionClass $class |
|
193 * @param \ReflectionMethod $method |
|
194 * |
|
195 * @return string |
|
196 */ |
|
197 protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method) |
|
198 { |
|
199 $name = strtolower(str_replace('\\', '_', $class->getName()).'_'.$method->getName()); |
|
200 if ($this->defaultRouteIndex > 0) { |
|
201 $name .= '_'.$this->defaultRouteIndex; |
|
202 } |
|
203 $this->defaultRouteIndex++; |
|
204 |
|
205 return $name; |
|
206 } |
|
207 |
|
208 abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot); |
|
209 } |