forked from symfony-cmf/Routing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentAwareGenerator.php
More file actions
236 lines (210 loc) · 7.85 KB
/
ContentAwareGenerator.php
File metadata and controls
236 lines (210 loc) · 7.85 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
namespace Symfony\Cmf\Component\Routing;
use Symfony\Component\Routing\Route as SymfonyRoute;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Cmf\Component\Routing\RouteProviderInterface;
/**
* A generator that tries to generate routes from object, route names or
* content objects or names.
*
* @author Philippo de Santis
* @author David Buchmann
* @author Uwe Jäger
*/
class ContentAwareGenerator extends ProviderBasedGenerator
{
/**
* The content repository used to find content by it's id
* This can be used to specify a parameter content_id when generating urls
*
* This is optional and might not be initialized.
*
* @var ContentRepositoryInterface
*/
protected $contentRepository;
/**
* Set an optional content repository to find content by ids
*
* @param ContentRepositoryInterface $contentRepository
*/
public function setContentRepository(ContentRepositoryInterface $contentRepository)
{
$this->contentRepository = $contentRepository;
}
/**
* {@inheritDoc}
*
* @param string $name ignored
* @param array $parameters must either contain the field 'route' with a
* RouteObjectInterface or the field 'content' with the document
* instance to get the route for (implementing RouteAwareInterface)
*
* @throws RouteNotFoundException If there is no such route in the database
*/
public function generate($name, $parameters = array(), $absolute = false)
{
if ($name instanceof SymfonyRoute) {
$route = $this->getBestLocaleRoute($name, $parameters);
} elseif (is_string($name) && $name) {
$route = $this->getRouteByName($name, $parameters);
} else {
$route = $this->getRouteByContent($name, $parameters);
}
if (! $route instanceof SymfonyRoute) {
$hint = is_object($route) ? get_class($route) : gettype($route);
throw new RouteNotFoundException('Route of this document is not an instance of Symfony\Component\Routing\Route but: '.$hint);
}
return parent::generate($route, $parameters, $absolute);
}
/**
* Get the route by a string name
*
* @param string $route
* @param array $parameters
*
* @return SymfonyRoute
*
* @throws RouteNotFoundException if there is no route found for the provided name
*/
protected function getRouteByName($name, array $parameters)
{
$route = $this->provider->getRouteByName($name, $parameters);
if (empty($route)) {
throw new RouteNotFoundException('No route found for name: ' . $name);
}
return $this->getBestLocaleRoute($route, $parameters);
}
/**
* Determine if there is a route with matching locale associated with the
* given route via associated content.
*
* @param SymfonyRoute $route
* @param array $parameters
*
* @return SymfonyRoute either the passed route or an alternative with better locale
*/
protected function getBestLocaleRoute(SymfonyRoute $route, $parameters)
{
if (! $route instanceof RouteObjectInterface) {
// this route has no content, we can't get the alternatives
return $route;
}
$locale = $this->getLocale($parameters);
if (! $this->checkLocaleRequirement($route, $locale)) {
$content = $route->getRouteContent();
if ($content instanceof RouteAwareInterface) {
$routes = $content->getRoutes();
$contentRoute = $this->getRouteByLocale($routes, $locale);
if ($contentRoute) {
return $contentRoute;
}
}
}
return $route;
}
/**
* Get the route based on the content field in parameters
*
* Called in generate when there is no route given in the parameters.
*
* If there is more than one route for the content, tries to find the
* first one that matches the _locale (provided in $parameters or otherwise
* defaulting to the request locale).
*
* If none is found, falls back to just return the first route.
*
* @param mixed $name
* @param array $parameters which should contain a content field containing a RouteAwareInterface object
*
* @return SymfonyRoute the route instance
*
* @throws RouteNotFoundException if there is no content field in the
* parameters or its not possible to build a route from that object
*/
protected function getRouteByContent($name, &$parameters)
{
if ($name instanceof RouteAwareInterface) {
$content = $name;
} elseif (isset($parameters['content_id']) && null !== $this->contentRepository) {
$content = $this->contentRepository->findById($parameters['content_id']);
} elseif (isset($parameters['content'])) {
$content = $parameters['content'];
}
unset($parameters['content'], $parameters['content_id']);
if (empty($content)) {
throw new RouteNotFoundException('Neither the route name, nor a parameter "content" or "content_id" could be resolved to an content instance');
}
if (!$content instanceof RouteAwareInterface) {
$hint = is_object($content) ? get_class($content) : gettype($content);
throw new RouteNotFoundException('The content does not implement RouteAwareInterface: ' . $hint);
}
$routes = $content->getRoutes();
if (empty($routes)) {
$hint = method_exists($content, 'getPath') ? $content->getPath() : get_class($content);
throw new RouteNotFoundException('Document has no route: ' . $hint);
}
$route = $this->getRouteByLocale($routes, $this->getLocale($parameters));
if ($route) {
return $route;
}
// if none matched, continue and randomly return the first one
return reset($routes);
}
/**
* @param RouteCollection $routes
* @param string $locale
*
* @return bool|SymfonyRoute false if no route requirement matches the provided locale
*/
protected function getRouteByLocale($routes, $locale)
{
foreach ($routes as $route) {
if (! $route instanceof SymfonyRoute) {
continue;
}
if ($this->checkLocaleRequirement($route, $locale)) {
return $route;
}
}
return false;
}
/**
* @param SymfonyRoute $route
* @param string $locale
*
* @return bool TRUE if there is either no _locale, no _locale requirement or if the two match
*/
private function checkLocaleRequirement(SymfonyRoute $route, $locale)
{
return empty($locale)
|| !$route->getRequirement('_locale')
|| preg_match('/'.$route->getRequirement('_locale').'/', $locale)
;
}
/**
* Determine the locale to be used with this request
*
* @param array $parameters the parameters determined by the route
*
* @return string|null the locale following of the parameters or any other
* information the router has available.
*/
protected function getLocale($parameters)
{
if (isset($parameters['_locale'])) {
return $parameters['_locale'];
}
return null;
}
/**
* We additionally support empty name and data in parameters and RouteAware content
*/
public function supports($name)
{
return ! $name || parent::supports($name) || $name instanceof RouteAwareInterface;
}
}