-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathRouter.php
More file actions
214 lines (186 loc) · 6.95 KB
/
Router.php
File metadata and controls
214 lines (186 loc) · 6.95 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace BeSimple\I18nRoutingBundle\Routing;
use BeSimple\I18nRoutingBundle\Routing\RouteGenerator\NameInflector\PostfixInflector;
use BeSimple\I18nRoutingBundle\Routing\RouteGenerator\NameInflector\RouteNameInflectorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use BeSimple\I18nRoutingBundle\Routing\Translator\AttributeTranslatorInterface;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\RequestContext;
class Router implements RouterInterface
{
/**
* @var AttributeTranslatorInterface
*/
protected $translator;
/**
* @var RouterInterface
*/
protected $router;
/**
* The locale to use when neither the parameters nor the request context
* indicate the locale to use.
*
* @var string
*/
protected $defaultLocale;
/**
* @var RouteNameInflectorInterface
*/
private $routeNameInflector;
/**
* Constructor
*
* @param \Symfony\Component\Routing\RouterInterface $router
* @param Translator\AttributeTranslatorInterface|null $translator
* @param string $defaultLocale
*/
public function __construct(RouterInterface $router, AttributeTranslatorInterface $translator = null, $defaultLocale = null, RouteNameInflectorInterface $routeNameInflector = null)
{
$this->router = $router;
$this->translator = $translator;
$this->defaultLocale = $defaultLocale;
$this->routeNameInflector = $routeNameInflector ?: new PostfixInflector();
}
/**
* Generates a URL from the given parameters.
*
* @param string $name The name of the route
* @param array $parameters An array of parameters
* @param bool|int $referenceType The type of reference to be generated (one of the constants)
*
* @return string The generated URL
*
* @throws \InvalidArgumentException When the route doesn't exists
*/
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
{
if (isset($parameters['locale']) || isset($parameters['translate'])) {
$locale = $this->getLocale($parameters);
if (isset($parameters['locale'])) {
unset($parameters['locale']);
}
if (null === $locale) {
throw new MissingMandatoryParametersException('The locale must be available when using the "translate" option.');
}
if (isset($parameters['translate'])) {
if (null !== $this->translator) {
foreach ((array) $parameters['translate'] as $translateAttribute) {
$parameters[$translateAttribute] = $this->translator->reverseTranslate(
$name,
$locale,
$translateAttribute,
$parameters[$translateAttribute]
);
}
}
unset($parameters['translate']);
}
return $this->generateI18n($name, $locale, $parameters, $referenceType);
}
try {
return $this->router->generate($name, $parameters, $referenceType);
} catch (RouteNotFoundException $e) {
$locale = $this->getLocale($parameters);
if (null !== $locale) {
// at this point here we would never have $parameters['translate'] due to condition before
return $this->generateI18n($name, $locale, $parameters, $referenceType);
}
throw $e;
}
}
/**
* {@inheritDoc}
*/
public function match($pathinfo)
{
$match = $this->router->match($pathinfo);
$route = $match['_route'];
$locale = $match['_locale'] ?? '';
if (empty($locale)) {
// no point in trying to match when we have no locale
return $match;
}
if ($this->routeNameInflector->isInflected($route, $locale)) {
if ( ! $this->routeNameInflector->isValidMatch($route, $locale, $this->getRouteCollection())) {
throw new RouteNotFoundException('A BeSimple route was matched, but it is not valid.');
}
$match['_route'] = $this->routeNameInflector->unInflect($route, $locale);
// now also check if we want to translate parameters:
if (null !== $this->translator && isset($match['_translate'])) {
foreach ((array) $match['_translate'] as $attribute) {
$match[$attribute] = $this->translator->translate(
$match['_route'],
$match['_locale'],
$attribute,
$match[$attribute]
);
}
}
}
return $match;
}
public function getRouteCollection()
{
return $this->router->getRouteCollection();
}
public function setContext(RequestContext $context)
{
$this->router->setContext($context);
}
public function getContext()
{
return $this->router->getContext();
}
/**
* Overwrite the locale to be used by default if the current locale could
* not be found when building the route
*
* @param string $locale
*/
public function setDefaultLocale($locale)
{
$this->defaultLocale = $locale;
}
/**
* Generates a I18N URL from the given parameter
*
* @param string $name The name of the I18N route
* @param string $locale The locale of the I18N route
* @param array $parameters An array of parameters
* @param bool|int $referenceType The type of reference to be generated (one of the constants)
*
* @return string The generated URL
*
* @throws RouteNotFoundException When the route doesn't exists
*/
protected function generateI18n($name, $locale, $parameters, $referenceType = self::ABSOLUTE_PATH)
{
try {
return $this->router->generate(
$this->routeNameInflector->inflect($name, $locale),
$parameters,
$referenceType
);
} catch (RouteNotFoundException $e) {
throw new RouteNotFoundException(sprintf('I18nRoute "%s" (%s) does not exist.', $name, $locale));
}
}
/**
* Determine the locale to be used with this request
*
* @param array $parameters the parameters determined by the route
*
* @return string
*/
protected function getLocale($parameters)
{
if (isset($parameters['locale'])) {
return $parameters['locale'];
}
if ($this->getContext()->hasParameter('_locale')) {
return $this->getContext()->getParameter('_locale');
}
return $this->defaultLocale;
}
}