1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FrankDeJonge\SymfonyI18nRouting\Routing\Loader; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\Reader; |
6
|
|
|
use FrankDeJonge\SymfonyI18nRouting\Routing\Annotation\I18nRoute; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
use ReflectionMethod; |
9
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
10
|
|
|
use Symfony\Component\Routing\Annotation\Route as SymfonyRoute; |
11
|
|
|
use Symfony\Component\Routing\Loader\AnnotationClassLoader; |
12
|
|
|
use Symfony\Component\Routing\Route; |
13
|
|
|
use Symfony\Component\Routing\RouteCollection; |
14
|
|
|
use function array_diff; |
15
|
|
|
use function array_keys; |
16
|
|
|
use function join; |
17
|
|
|
use function var_dump; |
18
|
|
|
|
19
|
|
|
class AnnotatedI18nRouteLoader extends AnnotationClassLoader |
20
|
|
|
{ |
21
|
|
|
protected $routeAnnotationClass = I18nRoute::class; |
22
|
|
|
|
23
|
30 |
|
public function __construct(Reader $reader) |
24
|
|
|
{ |
25
|
30 |
|
parent::__construct($reader); |
26
|
30 |
|
$this->setRouteAnnotationClass(I18nRoute::class); |
27
|
30 |
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Loads from annotations from a class. |
32
|
|
|
* |
33
|
|
|
* @param string $class A class name |
34
|
|
|
* @param string|null $type The resource type |
35
|
|
|
* |
36
|
|
|
* @return RouteCollection A RouteCollection instance |
37
|
|
|
* |
38
|
|
|
* @throws \InvalidArgumentException When route can't be parsed |
39
|
|
|
*/ |
40
|
30 |
|
public function load($class, $type = null) |
41
|
|
|
{ |
42
|
30 |
|
if ( ! class_exists($class)) { |
43
|
4 |
|
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class)); |
44
|
|
|
} |
45
|
|
|
|
46
|
26 |
|
$class = new \ReflectionClass($class); |
47
|
26 |
|
if ($class->isAbstract()) { |
48
|
|
|
throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class->getName())); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
26 |
|
$globals = $this->getGlobals($class); |
52
|
|
|
|
53
|
26 |
|
$collection = new RouteCollection(); |
54
|
26 |
|
$collection->addResource(new FileResource($class->getFileName())); |
55
|
|
|
|
56
|
26 |
|
foreach ($class->getMethods() as $method) { |
57
|
26 |
|
$this->defaultRouteIndex = 0; |
58
|
26 |
|
foreach ($this->reader->getMethodAnnotations($method) as $annot) { |
59
|
26 |
|
if ($annot instanceof SymfonyRoute || $annot instanceof I18nRoute) { |
60
|
26 |
|
$this->addRoute($collection, $annot, $globals, $class, $method); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
18 |
|
if (0 === $collection->count() && $class->hasMethod('__invoke')) { |
66
|
|
|
$annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass) |
67
|
|
|
?: $this->reader->getClassAnnotation($class, SymfonyRoute::class); |
68
|
|
|
|
69
|
|
|
if ($annot != null) { |
70
|
|
|
$globals['path'] = ''; |
71
|
|
|
$globals['name'] = ''; |
72
|
|
|
$globals['locales'] = []; |
73
|
|
|
$this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke')); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
18 |
|
return $collection; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param RouteCollection $collection |
82
|
|
|
* @param I18nRoute $annotation |
83
|
|
|
* @param array $globals |
84
|
|
|
* @param ReflectionClass $class |
85
|
|
|
* @param ReflectionMethod $method |
86
|
|
|
*/ |
87
|
26 |
|
protected function addRoute(RouteCollection $collection, $annotation, $globals, ReflectionClass $class, ReflectionMethod $method) |
88
|
|
|
{ |
89
|
26 |
|
$name = $annotation->getName(); |
90
|
|
|
|
91
|
26 |
|
if (null === $name) { |
92
|
2 |
|
throw MissingRouteName::forAnnotation($class->name . '::' . $method->name); |
93
|
|
|
} |
94
|
|
|
|
95
|
24 |
|
$defaults = array_replace($globals['defaults'], $annotation->getDefaults()); |
96
|
|
|
|
97
|
24 |
|
foreach ($method->getParameters() as $param) { |
98
|
2 |
|
if ( ! isset($defaults[$param->name]) && $param->isDefaultValueAvailable()) { |
99
|
2 |
|
$defaults[$param->name] = $param->getDefaultValue(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
24 |
|
$requirements = array_replace($globals['requirements'], $annotation->getRequirements()); |
104
|
24 |
|
$options = array_replace($globals['options'], $annotation->getOptions()); |
105
|
24 |
|
$schemes = array_merge($globals['schemes'], $annotation->getSchemes()); |
106
|
24 |
|
$methods = array_merge($globals['methods'], $annotation->getMethods()); |
107
|
24 |
|
$host = $annotation->getHost() ?: $globals['host']; |
108
|
24 |
|
$condition = $annotation->getCondition() ?: $globals['condition']; |
109
|
24 |
|
$path = $annotation->getPath(); |
110
|
24 |
|
$locales = $annotation instanceof I18nRoute ? $annotation->getLocales() : []; |
111
|
|
|
|
112
|
24 |
|
$hasLocalizedPrefix = empty($globals['locales']) === false; |
113
|
24 |
|
$hasPrefix = $hasLocalizedPrefix || empty($globals['path']) === false; |
114
|
24 |
|
$isLocalized = ! empty($locales); |
115
|
24 |
|
$hasPathOrLocales = empty($path) === false || $isLocalized; |
116
|
|
|
|
117
|
24 |
|
if ($hasPrefix === false && $hasPathOrLocales === false) { |
118
|
2 |
|
throw MissingRoutePath::forAnnotation("{$class->name}::{$method->name}"); |
119
|
|
|
} |
120
|
|
|
|
121
|
22 |
|
if ( ! $hasPathOrLocales) { |
122
|
4 |
View Code Duplication |
if ($hasLocalizedPrefix) { |
|
|
|
|
123
|
2 |
|
foreach ($globals['locales'] as $locale => $localePath) { |
124
|
2 |
|
$routeName = "{$name}.{$locale}"; |
125
|
2 |
|
$route = $this->createRoute($localePath, $defaults, $requirements, $options, $host, $schemes, $methods, $condition); |
126
|
2 |
|
$this->configureRoute($route, $class, $method, $annotation); |
127
|
2 |
|
$route->setDefault('_locale', $locale); |
128
|
2 |
|
$collection->add($routeName, $route); |
129
|
|
|
} |
130
|
|
|
} else { |
131
|
2 |
|
$route = $this->createRoute($globals['path'], $defaults, $requirements, $options, $host, $schemes, $methods, $condition); |
132
|
2 |
|
$this->configureRoute($route, $class, $method, $annotation); |
133
|
4 |
|
$collection->add($name, $route); |
134
|
|
|
} |
135
|
18 |
|
} elseif ( ! $hasPrefix) { |
136
|
6 |
View Code Duplication |
if ($isLocalized) { |
|
|
|
|
137
|
2 |
|
foreach ($locales as $locale => $localePath) { |
138
|
2 |
|
$routeName = "{$name}.{$locale}"; |
139
|
2 |
|
$route = $this->createRoute($localePath, $defaults, $requirements, $options, $host, $schemes, $methods, $condition); |
140
|
2 |
|
$this->configureRoute($route, $class, $method, $annotation); |
141
|
2 |
|
$route->setDefault('_locale', $locale); |
142
|
2 |
|
$collection->add($routeName, $route); |
143
|
|
|
} |
144
|
|
|
} else { |
145
|
4 |
|
$route = $this->createRoute($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition); |
146
|
4 |
|
$this->configureRoute($route, $class, $method, $annotation); |
147
|
6 |
|
$collection->add($name, $route); |
148
|
|
|
} |
149
|
|
|
} else { |
150
|
12 |
|
if ($hasLocalizedPrefix) { |
151
|
8 |
|
if ($isLocalized) { |
152
|
6 |
|
$missing = array_diff(array_keys($globals['locales']), array_keys($locales)); |
153
|
|
|
|
154
|
6 |
|
if ( ! empty($missing)) { |
155
|
2 |
|
throw MissingRouteLocale::forClass($class, $method, join(' and ', $missing)); |
156
|
|
|
} |
157
|
|
|
|
158
|
4 |
|
foreach ($locales as $locale => $localePath) { |
159
|
4 |
|
if ( ! isset($globals['locales'][$locale])) { |
160
|
2 |
|
throw MissingRouteLocale::forClass($class, $method, $locale); |
161
|
|
|
} |
162
|
|
|
|
163
|
4 |
|
$routePath = $globals['locales'][$locale] . $localePath; |
164
|
4 |
|
$routeName = "{$name}.{$locale}"; |
165
|
4 |
|
$route = $this->createRoute($routePath, $defaults, $requirements, $options, $host, $schemes, $methods, $condition); |
166
|
4 |
|
$this->configureRoute($route, $class, $method, $annotation); |
167
|
4 |
|
$route->setDefault('_locale', $locale); |
168
|
4 |
|
$collection->add($routeName, $route); |
169
|
|
|
} |
170
|
|
|
} else { |
171
|
2 |
View Code Duplication |
foreach ($globals['locales'] as $locale => $localePrefix) { |
|
|
|
|
172
|
2 |
|
$routeName = "{$name}.{$locale}"; |
173
|
2 |
|
$routePath = $localePrefix . $path; |
174
|
2 |
|
$route = $this->createRoute($routePath, $defaults, $requirements, $options, $host, $schemes, $methods, $condition); |
175
|
2 |
|
$this->configureRoute($route, $class, $method, $annotation); |
176
|
2 |
|
$route->setDefault('_locale', $locale); |
177
|
4 |
|
$collection->add($routeName, $route); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} else { |
181
|
4 |
|
if ($isLocalized) { |
182
|
2 |
View Code Duplication |
foreach ($locales as $locale => $localePath) { |
|
|
|
|
183
|
2 |
|
$routePath = $globals['path'] . $localePath; |
184
|
2 |
|
$routeName = "{$name}.{$locale}"; |
185
|
2 |
|
$route = $this->createRoute($routePath, $defaults, $requirements, $options, $host, $schemes, $methods, $condition); |
186
|
2 |
|
$this->configureRoute($route, $class, $method, $annotation); |
187
|
2 |
|
$route->setDefault('_locale', $locale); |
188
|
2 |
|
$collection->add($routeName, $route); |
189
|
|
|
} |
190
|
|
|
} else { |
191
|
2 |
|
$routePath = $globals['path'] . $path; |
192
|
2 |
|
$route = $this->createRoute($routePath, $defaults, $requirements, $options, $host, $schemes, $methods, $condition); |
193
|
2 |
|
$this->configureRoute($route, $class, $method, $annotation); |
194
|
2 |
|
$collection->add($name, $route); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
18 |
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @inheritdoc |
202
|
|
|
*/ |
203
|
26 |
|
protected function getGlobals(ReflectionClass $class) |
204
|
|
|
{ |
205
|
|
|
$globals = [ |
206
|
26 |
|
'path' => '', |
207
|
|
|
'locales' => [], |
208
|
|
|
'requirements' => [], |
209
|
|
|
'options' => [], |
210
|
|
|
'defaults' => [], |
211
|
|
|
'schemes' => [], |
212
|
|
|
'methods' => [], |
213
|
|
|
'host' => '', |
214
|
|
|
'condition' => '', |
215
|
|
|
]; |
216
|
|
|
|
217
|
26 |
|
$annotation = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass); |
218
|
|
|
|
219
|
26 |
|
if ( ! $annotation instanceof I18nRoute && ! $annotation instanceof SymfonyRoute) { |
220
|
10 |
|
return $globals; |
221
|
|
|
} |
222
|
16 |
|
if ($annotation instanceof I18nRoute) { |
223
|
16 |
|
$globals['locales'] = $annotation->getLocales(); |
224
|
|
|
} |
225
|
16 |
|
if (null !== $annotation->getPath()) { |
226
|
6 |
|
$globals['path'] = $annotation->getPath(); |
227
|
|
|
} |
228
|
16 |
|
if (null !== $annotation->getRequirements()) { |
229
|
16 |
|
$globals['requirements'] = $annotation->getRequirements(); |
230
|
|
|
} |
231
|
16 |
|
if (null !== $annotation->getOptions()) { |
232
|
16 |
|
$globals['options'] = $annotation->getOptions(); |
233
|
|
|
} |
234
|
16 |
|
if (null !== $annotation->getDefaults()) { |
235
|
16 |
|
$globals['defaults'] = $annotation->getDefaults(); |
236
|
|
|
} |
237
|
16 |
|
if (null !== $annotation->getSchemes()) { |
238
|
16 |
|
$globals['schemes'] = $annotation->getSchemes(); |
239
|
|
|
} |
240
|
16 |
|
if (null !== $annotation->getMethods()) { |
241
|
16 |
|
$globals['methods'] = $annotation->getMethods(); |
242
|
|
|
} |
243
|
16 |
|
if (null !== $annotation->getHost()) { |
244
|
2 |
|
$globals['host'] = $annotation->getHost(); |
245
|
|
|
} |
246
|
16 |
|
if (null !== $annotation->getCondition()) { |
247
|
2 |
|
$globals['condition'] = $annotation->getCondition(); |
248
|
|
|
} |
249
|
|
|
|
250
|
16 |
|
return $globals; |
251
|
|
|
} |
252
|
|
|
|
253
|
20 |
|
protected function configureRoute(Route $route, ReflectionClass $class, ReflectionMethod $method, $annot) |
254
|
|
|
{ |
255
|
20 |
|
$route->setDefault('_controller', $class->name . '::' . $method->getName()); |
|
|
|
|
256
|
20 |
|
} |
257
|
|
|
} |
258
|
|
|
|