12
12
namespace Symfony \Cmf \Component \Routing ;
13
13
14
14
use Symfony \Component \Routing \RouterInterface ;
15
+ use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
15
16
use Symfony \Component \Routing \Matcher \RequestMatcherInterface ;
16
17
use Symfony \Component \Routing \RequestContext ;
17
18
use Symfony \Component \Routing \RequestContextAwareInterface ;
24
25
use Psr \Log \LoggerInterface ;
25
26
26
27
/**
27
- * ChainRouter
28
- *
29
- * Allows access to a lot of different routers.
28
+ * The ChainRouter allows to combine several routers to try in a defined order.
30
29
*
31
30
* @author Henrik Bjornskov <[email protected] >
32
31
* @author Magnus Nordlander <[email protected] >
33
32
*/
34
33
class ChainRouter implements ChainRouterInterface, WarmableInterface
35
34
{
36
35
/**
37
- * @var \Symfony\Component\Routing\ RequestContext
36
+ * @var RequestContext
38
37
*/
39
38
private $ context ;
40
39
@@ -45,17 +44,17 @@ class ChainRouter implements ChainRouterInterface, WarmableInterface
45
44
private $ routers = array ();
46
45
47
46
/**
48
- * @var \Symfony\Component\Routing\ RouterInterface[] Array of routers, sorted by priority
47
+ * @var RouterInterface[] Array of routers, sorted by priority
49
48
*/
50
49
private $ sortedRouters ;
51
50
52
51
/**
53
- * @var \Symfony\Component\Routing\ RouteCollection
52
+ * @var RouteCollection
54
53
*/
55
54
private $ routeCollection ;
56
55
57
56
/**
58
- * @var null|\Psr\Log\ LoggerInterface
57
+ * @var null|LoggerInterface
59
58
*/
60
59
protected $ logger ;
61
60
@@ -78,8 +77,13 @@ public function getContext()
78
77
/**
79
78
* {@inheritdoc}
80
79
*/
81
- public function add (RouterInterface $ router , $ priority = 0 )
80
+ public function add ($ router , $ priority = 0 )
82
81
{
82
+ if (!$ router instanceof RouterInterface
83
+ && !($ router instanceof RequestMatcherInterface && $ router instanceof UrlGeneratorInterface)
84
+ ) {
85
+ throw new \InvalidArgumentException (sprintf ('%s is not a valid router. ' , get_class ($ router )));
86
+ }
83
87
if (empty ($ this ->routers [$ priority ])) {
84
88
$ this ->routers [$ priority ] = array ();
85
89
}
@@ -159,6 +163,10 @@ public function matchRequest(Request $request)
159
163
*
160
164
* @param string $url
161
165
* @param Request $request
166
+ *
167
+ * @return array An array of parameters
168
+ *
169
+ * @throws ResourceNotFoundException If no router matched.
162
170
*/
163
171
private function doMatch ($ url , Request $ request = null )
164
172
{
@@ -208,15 +216,14 @@ public function generate($name, $parameters = array(), $absolute = false)
208
216
$ debug = array ();
209
217
210
218
foreach ($ this ->all () as $ router ) {
211
- // if $router does not implement ChainedRouterInterface and $name is not a string, continue
212
- if ($ name && !$ router instanceof ChainedRouterInterface) {
213
- if (! is_string ($ name )) {
214
- continue ;
215
- }
219
+ // if $router does not announce it is capable of handling
220
+ // non-string routes and $name is not a string, continue
221
+ if ($ name && !is_string ($ name ) && !$ router instanceof VersatileGeneratorInterface) {
222
+ continue ;
216
223
}
217
224
218
- // If $router implements ChainedRouterInterface but doesn't support this route name, continue
219
- if ($ router instanceof ChainedRouterInterface && !$ router ->supports ($ name )) {
225
+ // If $router is versatile and doesn't support this route name, continue
226
+ if ($ router instanceof VersatileGeneratorInterface && !$ router ->supports ($ name )) {
220
227
continue ;
221
228
}
222
229
0 commit comments