forked from symfony-cmf/Routing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChainRouter.php
269 lines (236 loc) · 8.34 KB
/
ChainRouter.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
namespace Symfony\Cmf\Component\Routing;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RequestContextAwareInterface;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
/**
* ChainRouter
*
* Allows access to a lot of different routers.
*
* @author Henrik Bjornskov <[email protected]>
* @author Magnus Nordlander <[email protected]>
*/
class ChainRouter implements RouterInterface, RequestMatcherInterface, WarmableInterface
{
/**
* @var \Symfony\Component\Routing\RequestContext
*/
private $context;
/**
* @var Symfony\Component\Routing\RouterInterface[]
*/
private $routers = array();
/**
* @var \Symfony\Component\Routing\RouterInterface[] Array of routers, sorted by priority
*/
private $sortedRouters;
/**
* @var \Symfony\Component\Routing\RouteCollection
*/
private $routeCollection;
/**
* @var null|\Symfony\Component\HttpKernel\Log\LoggerInterface
*/
protected $logger;
/**
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger;
}
/**
* @return RequestContext
*/
public function getContext()
{
return $this->context;
}
/**
* Add a Router to the index
*
* @param RouterInterface $router The router instance
* @param integer $priority The priority
*/
public function add(RouterInterface $router, $priority = 0)
{
if (empty($this->routers[$priority])) {
$this->routers[$priority] = array();
}
$this->routers[$priority][] = $router;
$this->sortedRouters = array();
}
/**
* Sorts the routers and flattens them.
*
* @return array
*/
public function all()
{
if (empty($this->sortedRouters)) {
$this->sortedRouters = $this->sortRouters();
// setContext() is done here instead of in add() to avoid fatal errors when clearing and warming up caches
// See https://github.com/symfony-cmf/Routing/pull/18
$context = $this->getContext();
if (null !== $context) {
foreach ($this->sortedRouters as $router) {
if ($router instanceof RequestContextAwareInterface) {
$router->setContext($context);
}
}
}
}
return $this->sortedRouters;
}
/**
* Sort routers by priority.
* The highest priority number is the highest priority (reverse sorting)
*
* @return RouterInterface[]
*/
protected function sortRouters()
{
$sortedRouters = array();
krsort($this->routers);
foreach ($this->routers as $routers) {
$sortedRouters = array_merge($sortedRouters, $routers);
}
return $sortedRouters;
}
/**
* {@inheritdoc}
*
* Loops through all routes and tries to match the passed url.
*
* Note: You should use matchRequest if you can.
*/
public function match($url)
{
$methodNotAllowed = null;
/** @var $router ChainedRouterInterface */
foreach ($this->all() as $router) {
try {
return $router->match($url);
} catch (ResourceNotFoundException $e) {
if ($this->logger) {
$this->logger->info('Router '.get_class($router).' was not able to match, message "'.$e->getMessage().'"');
}
// Needs special care
} catch (MethodNotAllowedException $e) {
if ($this->logger) {
$this->logger->info('Router '.get_class($router).' throws MethodNotAllowedException with message "'.$e->getMessage().'"');
}
$methodNotAllowed = $e;
}
}
throw $methodNotAllowed ?: new ResourceNotFoundException("None of the routers in the chain matched '$url'");
}
/**
* {@inheritdoc}
*
* Loops through all routes and tries to match the passed request.
*/
public function matchRequest(Request $request)
{
$methodNotAllowed = null;
foreach ($this->all() as $router) {
try {
// the request/url match logic is the same as in Symfony/Component/HttpKernel/EventListener/RouterListener.php
// matching requests is more powerful than matching URLs only, so try that first
if ($router instanceof RequestMatcherInterface) {
return $router->matchRequest($request);
}
return $router->match($request->getPathInfo());
} catch (ResourceNotFoundException $e) {
if ($this->logger) {
$this->logger->info('Router '.get_class($router).' was not able to match, message "'.$e->getMessage().'"');
}
// Needs special care
} catch (MethodNotAllowedException $e) {
if ($this->logger) {
$this->logger->info('Router '.get_class($router).' throws MethodNotAllowedException with message "'.$e->getMessage().'"');
}
$methodNotAllowed = $e;
}
}
throw $methodNotAllowed ?: new ResourceNotFoundException("None of the routers in the chain matched this request");
}
/**
* {@inheritdoc}
*
* Loops through all registered routers and returns a router if one is found.
* It will always return the first route generated.
*/
public function generate($name, $parameters = array(), $absolute = false)
{
/** @var $router ChainedRouterInterface */
foreach ($this->all() as $router) {
// if $name and $router does not implement ChainedRouterInterface and $name is not a string, continue
// if $name and $router does not implement ChainedRouterInterface and $name is string but does not match a default Symfony2 route name, continue
if ($name && !$router instanceof ChainedRouterInterface) {
if (!is_string($name) || !preg_match('/^[a-z0-9A-Z_.]+$/', $name)) {
continue;
}
}
// If $router implements ChainedRouterInterface but doesn't support this route name, continue
if ($router instanceof ChainedRouterInterface && !$router->supports($name)) {
continue;
}
try {
return $router->generate($name, $parameters, $absolute);
} catch (RouteNotFoundException $e) {
if ($this->logger) {
$this->logger->info($e->getMessage());
}
}
}
throw new RouteNotFoundException(sprintf('None of the chained routers were able to generate route "%s".', $name));
}
/**
* {@inheritdoc}
*/
public function setContext(RequestContext $context)
{
foreach ($this->all() as $router) {
if ($router instanceof RequestContextAwareInterface) {
$router->setContext($context);
}
}
$this->context = $context;
}
/**
* {@inheritdoc}
*
* check for each contained router if it can warmup
*/
public function warmUp($cacheDir)
{
foreach ($this->all() as $router) {
if ($router instanceof WarmableInterface) {
$router->warmUp($cacheDir);
}
}
}
/**
* {@inheritdoc}
*/
public function getRouteCollection()
{
if (!$this->routeCollection instanceof RouteCollection) {
$this->routeCollection = new RouteCollection();
foreach ($this->all() as $router) {
$this->routeCollection->addCollection($router->getRouteCollection());
}
}
return $this->routeCollection;
}
}