12
12
namespace Symfony \Component \Routing ;
13
13
14
14
use Symfony \Component \Config \Loader \LoaderInterface ;
15
- use Symfony \Component \Config \ConfigCache ;
15
+ use Symfony \Component \Config \ConfigCacheInterface ;
16
+ use Symfony \Component \Config \ConfigCacheFactoryInterface ;
17
+ use Symfony \Component \Config \DefaultConfigCacheFactory ;
16
18
use Psr \Log \LoggerInterface ;
17
19
use Symfony \Component \Routing \Generator \ConfigurableRequirementsInterface ;
18
20
use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
@@ -71,6 +73,11 @@ class Router implements RouterInterface, RequestMatcherInterface
71
73
*/
72
74
protected $ logger ;
73
75
76
+ /**
77
+ * @var ConfigCacheFactoryInterface|null
78
+ */
79
+ private $ configCacheFactory ;
80
+
74
81
/**
75
82
* @var ExpressionFunctionProviderInterface[]
76
83
*/
@@ -209,6 +216,16 @@ public function getContext()
209
216
return $ this ->context ;
210
217
}
211
218
219
+ /**
220
+ * Sets the ConfigCache factory to use.
221
+ *
222
+ * @param ConfigCacheFactoryInterface $configCacheFactory The factory to use.
223
+ */
224
+ public function setConfigCacheFactory (ConfigCacheFactoryInterface $ configCacheFactory )
225
+ {
226
+ $ this ->configCacheFactory = $ configCacheFactory ;
227
+ }
228
+
212
229
/**
213
230
* {@inheritdoc}
214
231
*/
@@ -262,24 +279,29 @@ public function getMatcher()
262
279
}
263
280
264
281
$ class = $ this ->options ['matcher_cache_class ' ];
265
- $ cache = new ConfigCache ($ this ->options ['cache_dir ' ].'/ ' .$ class .'.php ' , $ this ->options ['debug ' ]);
266
- if (!$ cache ->isFresh ()) {
267
- $ dumper = $ this ->getMatcherDumperInstance ();
268
- if (method_exists ($ dumper , 'addExpressionLanguageProvider ' )) {
269
- foreach ($ this ->expressionLanguageProviders as $ provider ) {
270
- $ dumper ->addExpressionLanguageProvider ($ provider );
282
+ $ baseClass = $ this ->options ['matcher_base_class ' ];
283
+ $ expressionLanguageProviders = $ this ->expressionLanguageProviders ;
284
+ $ self = $ this ; // required for PHP 5.3 where "$this" cannot be use()d in anonymous functions. Change in Symfony 3.0.
285
+
286
+ $ cache = $ this ->getConfigCacheFactory ()->cache ($ this ->options ['cache_dir ' ].'/ ' .$ class .'.php ' ,
287
+ function (ConfigCacheInterface $ cache ) use ($ self , $ class , $ baseClass , $ expressionLanguageProviders ) {
288
+ $ dumper = $ self ->getMatcherDumperInstance ();
289
+ if (method_exists ($ dumper , 'addExpressionLanguageProvider ' )) {
290
+ foreach ($ expressionLanguageProviders as $ provider ) {
291
+ $ dumper ->addExpressionLanguageProvider ($ provider );
292
+ }
271
293
}
272
- }
273
294
274
- $ options = array (
275
- 'class ' => $ class ,
276
- 'base_class ' => $ this -> options [ ' matcher_base_class ' ] ,
277
- );
295
+ $ options = array (
296
+ 'class ' => $ class ,
297
+ 'base_class ' => $ baseClass ,
298
+ );
278
299
279
- $ cache ->write ($ dumper ->dump ($ options ), $ this ->getRouteCollection ()->getResources ());
280
- }
300
+ $ cache ->write ($ dumper ->dump ($ options ), $ self ->getRouteCollection ()->getResources ());
301
+ }
302
+ );
281
303
282
- require_once $ cache ;
304
+ require_once $ cache-> getPath () ;
283
305
284
306
return $ this ->matcher = new $ class ($ this ->context );
285
307
}
@@ -299,19 +321,22 @@ public function getGenerator()
299
321
$ this ->generator = new $ this ->options ['generator_class ' ]($ this ->getRouteCollection (), $ this ->context , $ this ->logger );
300
322
} else {
301
323
$ class = $ this ->options ['generator_cache_class ' ];
302
- $ cache = new ConfigCache ($ this ->options ['cache_dir ' ].'/ ' .$ class .'.php ' , $ this ->options ['debug ' ]);
303
- if (!$ cache ->isFresh ()) {
304
- $ dumper = $ this ->getGeneratorDumperInstance ();
305
-
306
- $ options = array (
307
- 'class ' => $ class ,
308
- 'base_class ' => $ this ->options ['generator_base_class ' ],
309
- );
310
-
311
- $ cache ->write ($ dumper ->dump ($ options ), $ this ->getRouteCollection ()->getResources ());
312
- }
324
+ $ baseClass = $ this ->options ['generator_base_class ' ];
325
+ $ self = $ this ; // required for PHP 5.3 where "$this" cannot be use()d in anonymous functions. Change in Symfony 3.0.
326
+ $ cache = $ this ->getConfigCacheFactory ()->cache ($ this ->options ['cache_dir ' ].'/ ' .$ class .'.php ' ,
327
+ function (ConfigCacheInterface $ cache ) use ($ self , $ class , $ baseClass ) {
328
+ $ dumper = $ self ->getGeneratorDumperInstance ();
329
+
330
+ $ options = array (
331
+ 'class ' => $ class ,
332
+ 'base_class ' => $ baseClass ,
333
+ );
334
+
335
+ $ cache ->write ($ dumper ->dump ($ options ), $ self ->getRouteCollection ()->getResources ());
336
+ }
337
+ );
313
338
314
- require_once $ cache ;
339
+ require_once $ cache-> getPath () ;
315
340
316
341
$ this ->generator = new $ class ($ this ->context , $ this ->logger );
317
342
}
@@ -329,18 +354,37 @@ public function addExpressionLanguageProvider(ExpressionFunctionProviderInterfac
329
354
}
330
355
331
356
/**
357
+ * This method is public because it needs to be callable from a closure in PHP 5.3. It should be converted back to protected in 3.0.
358
+ * @internal
332
359
* @return GeneratorDumperInterface
333
360
*/
334
- protected function getGeneratorDumperInstance ()
361
+ public function getGeneratorDumperInstance ()
335
362
{
336
363
return new $ this ->options ['generator_dumper_class ' ]($ this ->getRouteCollection ());
337
364
}
338
365
339
366
/**
367
+ * This method is public because it needs to be callable from a closure in PHP 5.3. It should be converted back to protected in 3.0.
368
+ * @internal
340
369
* @return MatcherDumperInterface
341
370
*/
342
- protected function getMatcherDumperInstance ()
371
+ public function getMatcherDumperInstance ()
343
372
{
344
373
return new $ this ->options ['matcher_dumper_class ' ]($ this ->getRouteCollection ());
345
374
}
375
+
376
+ /**
377
+ * Provides the ConfigCache factory implementation, falling back to a
378
+ * default implementation if necessary.
379
+ *
380
+ * @return ConfigCacheFactoryInterface $configCacheFactory
381
+ */
382
+ private function getConfigCacheFactory ()
383
+ {
384
+ if (null === $ this ->configCacheFactory ) {
385
+ $ this ->configCacheFactory = new DefaultConfigCacheFactory ($ this ->options ['debug ' ]);
386
+ }
387
+
388
+ return $ this ->configCacheFactory ;
389
+ }
346
390
}
0 commit comments