|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FrankDeJonge\SymfonyI18nRouting\Routing\Loader; |
|
4
|
|
|
|
|
5
|
|
|
use function array_diff; |
|
6
|
|
|
use function array_keys; |
|
7
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
8
|
|
|
use FrankDeJonge\SymfonyI18nRouting\Routing\Annotation\I18nRoute; |
|
9
|
|
|
use function join; |
|
10
|
|
|
use LogicException; |
|
11
|
|
|
use ReflectionClass; |
|
12
|
|
|
use ReflectionMethod; |
|
13
|
|
|
use Symfony\Component\Routing\Loader\AnnotationClassLoader; |
|
14
|
|
|
use Symfony\Component\Routing\Route; |
|
15
|
|
|
use Symfony\Component\Routing\RouteCollection; |
|
16
|
|
|
|
|
17
|
|
|
class AnnotatedI18nRouteLoader extends AnnotationClassLoader |
|
18
|
|
|
{ |
|
19
|
|
|
protected $routeAnnotationClass = I18nRoute::class; |
|
20
|
|
|
|
|
21
|
26 |
|
public function __construct(Reader $reader) |
|
22
|
|
|
{ |
|
23
|
26 |
|
parent::__construct($reader); |
|
24
|
26 |
|
$this->setRouteAnnotationClass(I18nRoute::class); |
|
25
|
26 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param RouteCollection $collection |
|
29
|
|
|
* @param I18nRoute $annotation |
|
30
|
|
|
* @param array $globals |
|
31
|
|
|
* @param ReflectionClass $class |
|
32
|
|
|
* @param ReflectionMethod $method |
|
33
|
|
|
*/ |
|
34
|
26 |
|
protected function addRoute(RouteCollection $collection, $annotation, $globals, ReflectionClass $class, ReflectionMethod $method) |
|
35
|
|
|
{ |
|
36
|
26 |
|
$name = $annotation->getName(); |
|
37
|
|
|
|
|
38
|
26 |
|
if (null === $name) { |
|
39
|
2 |
|
throw MissingRouteName::forAnnotation($class->name . '::' . $method->name); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
24 |
|
$defaults = array_replace($globals['defaults'], $annotation->getDefaults()); |
|
43
|
|
|
|
|
44
|
24 |
|
foreach ($method->getParameters() as $param) { |
|
45
|
2 |
|
if ( ! isset($defaults[$param->getName()]) && $param->isDefaultValueAvailable()) { |
|
|
|
|
|
|
46
|
2 |
|
$defaults[$param->getName()] = $param->getDefaultValue(); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
24 |
|
$requirements = array_replace($globals['requirements'], $annotation->getRequirements()); |
|
51
|
24 |
|
$options = array_replace($globals['options'], $annotation->getOptions()); |
|
52
|
24 |
|
$schemes = array_merge($globals['schemes'], $annotation->getSchemes()); |
|
53
|
24 |
|
$methods = array_merge($globals['methods'], $annotation->getMethods()); |
|
54
|
24 |
|
$host = $annotation->getHost() ?: $globals['host']; |
|
55
|
24 |
|
$condition = $annotation->getCondition() ?: $globals['condition']; |
|
56
|
24 |
|
$path = $annotation->getPath(); |
|
57
|
24 |
|
$locales = $annotation->getLocales(); |
|
58
|
|
|
|
|
59
|
24 |
|
$hasLocalizedPrefix = empty($globals['locales']) === false; |
|
60
|
24 |
|
$hasPrefix = $hasLocalizedPrefix || empty($globals['path']) === false; |
|
61
|
24 |
|
$isLocalized = ! empty($locales); |
|
62
|
24 |
|
$hasPathOrLocales = empty($path) === false || $isLocalized; |
|
63
|
24 |
|
$defaultLocale = $globals['defaults']['_locale'] ?? null; |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
24 |
|
if ($hasPrefix === false && $hasPathOrLocales === false) { |
|
66
|
2 |
|
throw MissingRoutePath::forAnnotation("{$class->name}::{$method->name}"); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
22 |
|
if ( ! $hasPathOrLocales) { |
|
70
|
4 |
View Code Duplication |
if ($hasLocalizedPrefix) { |
|
|
|
|
|
|
71
|
2 |
|
foreach ($globals['locales'] as $locale => $localePath) { |
|
72
|
2 |
|
$routeName = "{$name}.{$locale}"; |
|
73
|
2 |
|
$route = $this->createRoute($localePath, $defaults, $requirements, $options, $host, $schemes, $methods, $condition); |
|
74
|
2 |
|
$this->configureRoute($route, $class, $method, $annotation); |
|
75
|
2 |
|
$route->setDefault('_locale', $locale); |
|
76
|
2 |
|
$collection->add($routeName, $route); |
|
77
|
|
|
} |
|
78
|
|
|
} else { |
|
79
|
2 |
|
$route = $this->createRoute($globals['path'], $defaults, $requirements, $options, $host, $schemes, $methods, $condition); |
|
80
|
2 |
|
$this->configureRoute($route, $class, $method, $annotation); |
|
81
|
4 |
|
$collection->add($name, $route); |
|
82
|
|
|
} |
|
83
|
18 |
|
} elseif ( ! $hasPrefix) { |
|
84
|
6 |
View Code Duplication |
if ($isLocalized) { |
|
|
|
|
|
|
85
|
2 |
|
foreach ($locales as $locale => $localePath) { |
|
86
|
2 |
|
$routeName = "{$name}.{$locale}"; |
|
87
|
2 |
|
$route = $this->createRoute($localePath, $defaults, $requirements, $options, $host, $schemes, $methods, $condition); |
|
88
|
2 |
|
$this->configureRoute($route, $class, $method, $annotation); |
|
89
|
2 |
|
$route->setDefault('_locale', $locale); |
|
90
|
2 |
|
$collection->add($routeName, $route); |
|
91
|
|
|
} |
|
92
|
|
|
} else { |
|
93
|
4 |
|
$route = $this->createRoute($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition); |
|
94
|
4 |
|
$this->configureRoute($route, $class, $method, $annotation); |
|
95
|
6 |
|
$collection->add($name, $route); |
|
96
|
|
|
} |
|
97
|
|
|
} else { |
|
98
|
12 |
|
if ($hasLocalizedPrefix) { |
|
99
|
8 |
|
if ($isLocalized) { |
|
100
|
6 |
|
$missing = array_diff(array_keys($globals['locales']), array_keys($locales)); |
|
101
|
|
|
|
|
102
|
6 |
|
if ( ! empty($missing)) { |
|
103
|
2 |
|
throw MissingRouteLocale::forClass($class, $method, join(' and ', $missing)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
4 |
|
foreach ($locales as $locale => $localePath) { |
|
107
|
4 |
|
if ( ! isset($globals['locales'][$locale])) { |
|
108
|
2 |
|
throw MissingRouteLocale::forClass($class, $method, $locale); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
4 |
|
$routePath = $globals['locales'][$locale] . $localePath; |
|
112
|
4 |
|
$routeName = "{$name}.{$locale}"; |
|
113
|
4 |
|
$route = $this->createRoute($routePath, $defaults, $requirements, $options, $host, $schemes, $methods, $condition); |
|
114
|
4 |
|
$this->configureRoute($route, $class, $method, $annotation); |
|
115
|
4 |
|
$route->setDefault('_locale', $locale); |
|
116
|
4 |
|
$collection->add($routeName, $route); |
|
117
|
|
|
} |
|
118
|
|
|
} else { |
|
119
|
2 |
View Code Duplication |
foreach ($globals['locales'] as $locale => $localePrefix) { |
|
|
|
|
|
|
120
|
2 |
|
$routeName = "{$name}.{$locale}"; |
|
121
|
2 |
|
$routePath = $localePrefix . $path; |
|
122
|
2 |
|
$route = $this->createRoute($routePath, $defaults, $requirements, $options, $host, $schemes, $methods, $condition); |
|
123
|
2 |
|
$this->configureRoute($route, $class, $method, $annotation); |
|
124
|
2 |
|
$route->setDefault('_locale', $locale); |
|
125
|
4 |
|
$collection->add($routeName, $route); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} else { |
|
129
|
4 |
|
if ($isLocalized) { |
|
130
|
2 |
View Code Duplication |
foreach ($locales as $locale => $localePath) { |
|
|
|
|
|
|
131
|
2 |
|
$routePath = $globals['path'] . $localePath; |
|
132
|
2 |
|
$routeName = "{$name}.{$locale}"; |
|
133
|
2 |
|
$route = $this->createRoute($routePath, $defaults, $requirements, $options, $host, $schemes, $methods, $condition); |
|
134
|
2 |
|
$this->configureRoute($route, $class, $method, $annotation); |
|
135
|
2 |
|
$route->setDefault('_locale', $locale); |
|
136
|
2 |
|
$collection->add($routeName, $route); |
|
137
|
|
|
} |
|
138
|
|
|
} else { |
|
139
|
2 |
|
$routePath = $globals['path'] . $path; |
|
140
|
2 |
|
$route = $this->createRoute($routePath, $defaults, $requirements, $options, $host, $schemes, $methods, $condition); |
|
141
|
2 |
|
$this->configureRoute($route, $class, $method, $annotation); |
|
142
|
2 |
|
$collection->add($name, $route); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
18 |
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @inheritdoc |
|
150
|
|
|
*/ |
|
151
|
26 |
|
protected function getGlobals(ReflectionClass $class) |
|
152
|
|
|
{ |
|
153
|
|
|
$globals = [ |
|
154
|
26 |
|
'path' => '', |
|
155
|
|
|
'locales' => [], |
|
156
|
|
|
'requirements' => [], |
|
157
|
|
|
'options' => [], |
|
158
|
|
|
'defaults' => [], |
|
159
|
|
|
'schemes' => [], |
|
160
|
|
|
'methods' => [], |
|
161
|
|
|
'host' => '', |
|
162
|
|
|
'condition' => '', |
|
163
|
|
|
]; |
|
164
|
|
|
|
|
165
|
26 |
|
$annotation = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass); |
|
166
|
|
|
|
|
167
|
26 |
|
if ( ! $annotation instanceof I18nRoute) { |
|
168
|
10 |
|
return $globals; |
|
169
|
|
|
} |
|
170
|
16 |
|
if (null !== $annotation->getLocales()) { |
|
171
|
16 |
|
$globals['locales'] = $annotation->getLocales(); |
|
172
|
|
|
} |
|
173
|
16 |
|
if (null !== $annotation->getPath()) { |
|
174
|
6 |
|
$globals['path'] = $annotation->getPath(); |
|
175
|
|
|
} |
|
176
|
16 |
|
if (null !== $annotation->getRequirements()) { |
|
177
|
16 |
|
$globals['requirements'] = $annotation->getRequirements(); |
|
178
|
|
|
} |
|
179
|
16 |
|
if (null !== $annotation->getOptions()) { |
|
180
|
16 |
|
$globals['options'] = $annotation->getOptions(); |
|
181
|
|
|
} |
|
182
|
16 |
|
if (null !== $annotation->getDefaults()) { |
|
183
|
16 |
|
$globals['defaults'] = $annotation->getDefaults(); |
|
184
|
|
|
} |
|
185
|
16 |
|
if (null !== $annotation->getSchemes()) { |
|
186
|
16 |
|
$globals['schemes'] = $annotation->getSchemes(); |
|
187
|
|
|
} |
|
188
|
16 |
|
if (null !== $annotation->getMethods()) { |
|
189
|
16 |
|
$globals['methods'] = $annotation->getMethods(); |
|
190
|
|
|
} |
|
191
|
16 |
|
if (null !== $annotation->getHost()) { |
|
192
|
2 |
|
$globals['host'] = $annotation->getHost(); |
|
193
|
|
|
} |
|
194
|
16 |
|
if (null !== $annotation->getCondition()) { |
|
195
|
2 |
|
$globals['condition'] = $annotation->getCondition(); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
16 |
|
return $globals; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
20 |
|
protected function configureRoute(Route $route, ReflectionClass $class, ReflectionMethod $method, $annot) |
|
202
|
|
|
{ |
|
203
|
20 |
|
$route->setDefault('_controller', $class->getName() . '::' . $method->getName()); |
|
|
|
|
|
|
204
|
|
|
} |
|
205
|
|
|
} |