@@ -77,13 +77,6 @@ public function add(RouterInterface $router, $priority = 0)
77
77
$ this ->routers [$ priority ] = array ();
78
78
}
79
79
80
- if ($ router instanceof RequestContextAwareInterface) {
81
- $ context = $ this ->getContext ();
82
- if (null !== $ context ) {
83
- $ router ->setContext ($ context );
84
- }
85
- }
86
-
87
80
$ this ->routers [$ priority ][] = $ router ;
88
81
$ this ->sortedRouters = array ();
89
82
}
@@ -97,6 +90,17 @@ public function all()
97
90
{
98
91
if (empty ($ this ->sortedRouters )) {
99
92
$ this ->sortedRouters = $ this ->sortRouters ();
93
+
94
+ // setContext() is done here instead of in add() to avoid fatal errors when clearing and warming up caches
95
+ // See https://github.com/symfony-cmf/Routing/pull/18
96
+ $ context = $ this ->getContext ();
97
+ if (null !== $ context ) {
98
+ foreach ($ this ->sortedRouters as $ router ) {
99
+ if ($ router instanceof RequestContextAwareInterface) {
100
+ $ router ->setContext ($ context );
101
+ }
102
+ }
103
+ }
100
104
}
101
105
102
106
return $ this ->sortedRouters ;
@@ -220,13 +224,13 @@ public function generate($name, $parameters = array(), $absolute = false)
220
224
*/
221
225
public function setContext (RequestContext $ context )
222
226
{
223
- $ this ->context = $ context ;
224
-
225
227
foreach ($ this ->all () as $ router ) {
226
228
if ($ router instanceof RequestContextAwareInterface) {
227
229
$ router ->setContext ($ context );
228
230
}
229
231
}
232
+
233
+ $ this ->context = $ context ;
230
234
}
231
235
232
236
/**
0 commit comments