-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
AnnotationClassLoader.php
167 lines (146 loc) · 5.12 KB
/
AnnotationClassLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace Symfony\Component\Routing\Loader;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\Routing\Annotation\Route as RouteAnnotation;
use Symfony\Component\Routing\Loader\LoaderResolver;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* AnnotationClassLoader loads routing information from a PHP class and its methods.
*
* You need to define an implementation for the getRouteDefaults() method. Most of the
* time, this method should define some PHP callable to be called for the route
* (a controller in MVC speak).
*
* The @Route annotation can be set on the class (for global parameters),
* and on each method.
*
* The @Route annotation main value is the route pattern. The annotation also
* recognizes three parameters: requirements, options, and name. The name parameter
* is mandatory. Here is an example of how you should be able to use it:
*
* /**
* * @Route("/Blog")
* * /
* class Blog
* {
* /**
* * @Route("/", name="blog_index")
* * /
* public function index()
* {
* }
*
* /**
* * @Route("/:id", name="blog_post", requirements = {"id" = "\d+"})
* * /
* public function show()
* {
* }
* }
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
abstract class AnnotationClassLoader implements LoaderInterface
{
protected $reader;
/**
* Constructor.
*/
public function __construct(AnnotationReader $reader)
{
$this->reader = $reader;
}
/**
* Loads from annotations from a class.
*
* @param string $class A class name
*
* @return RouteCollection A RouteCollection instance
*
* @throws \InvalidArgumentException When route can't be parsed
*/
public function load($class)
{
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
}
$class = new \ReflectionClass($class);
class_exists($annotClass = 'Symfony\\Component\\Routing\\Annotation\\Route');
$globals = array(
'pattern' => '',
'requirements' => array(),
'options' => array(),
'defaults' => array(),
);
if ($annot = $this->reader->getClassAnnotation($class, $annotClass)) {
if (null !== $annot->getPattern()) {
$globals['pattern'] = $annot->getPattern();
}
if (null !== $annot->getRequirements()) {
$globals['requirements'] = $annot->getRequirements();
}
if (null !== $annot->getOptions()) {
$globals['options'] = $annot->getOptions();
}
if (null !== $annot->getDefaults()) {
$globals['defaults'] = $annot->getDefaults();
}
}
$this->reader->setDefaultAnnotationNamespace('Symfony\\Component\\Routing\\Annotation\\');
$collection = new RouteCollection();
foreach ($class->getMethods() as $method) {
if ($annot = $this->reader->getMethodAnnotation($method, $annotClass)) {
if (null === $annot->getName()) {
$annot->setName($this->getDefaultRouteName($class, $method));
}
$defaults = array_merge($globals['defaults'], $annot->getDefaults(), $this->getRouteDefaults($class, $method, $annot));
$requirements = array_merge($globals['requirements'], $annot->getRequirements());
$options = array_merge($globals['options'], $annot->getOptions());
$route = new Route($globals['pattern'].$annot->getPattern(), $defaults, $requirements, $options);
$collection->addRoute($annot->getName(), $route);
}
}
return $collection;
}
protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
{
return strtolower(str_replace('\\', '_', $class->getName()).'_'.$method->getName());
}
/**
* Returns true if this class supports the given resource.
*
* @param mixed $resource A resource
*
* @return Boolean true if this class supports the given resource, false otherwise
*/
public function supports($resource)
{
return is_string($resource) && class_exists($resource);
}
/**
* Sets the loader resolver.
*
* @param LoaderResolver $resolver A LoaderResolver instance
*/
public function setResolver(LoaderResolver $resolver)
{
}
/**
* Gets the loader resolver.
*
* @return LoaderResolver A LoaderResolver instance
*/
public function getResolver()
{
}
abstract protected function getRouteDefaults(\ReflectionClass $class, \ReflectionMethod $method, RouteAnnotation $annot);
}