@@ -97,6 +97,8 @@ class Router implements RouterInterface, RequestMatcherInterface
97
97
*/
98
98
private $ expressionLanguageProviders = [];
99
99
100
+ private static $ cache = [];
101
+
100
102
/**
101
103
* @param LoaderInterface $loader A LoaderInterface instance
102
104
* @param mixed $resource The main resource to load
@@ -325,7 +327,7 @@ function (ConfigCacheInterface $cache) {
325
327
);
326
328
327
329
if ($ compiled ) {
328
- return $ this ->matcher = new $ this ->options ['matcher_class ' ](require $ cache ->getPath (), $ this ->context );
330
+ return $ this ->matcher = new $ this ->options ['matcher_class ' ](self :: getCompiledRoutes ( $ cache ->getPath () ), $ this ->context );
329
331
}
330
332
331
333
if (!class_exists ($ this ->options ['matcher_cache_class ' ], false )) {
@@ -369,7 +371,7 @@ function (ConfigCacheInterface $cache) {
369
371
);
370
372
371
373
if ($ compiled ) {
372
- $ this ->generator = new $ this ->options ['generator_class ' ](require $ cache ->getPath (), $ this ->context , $ this ->logger , $ this ->defaultLocale );
374
+ $ this ->generator = new $ this ->options ['generator_class ' ](self :: getCompiledRoutes ( $ cache ->getPath () ), $ this ->context , $ this ->logger , $ this ->defaultLocale );
373
375
} else {
374
376
if (!class_exists ($ this ->options ['generator_cache_class ' ], false )) {
375
377
require_once $ cache ->getPath ();
@@ -442,4 +444,21 @@ private function checkDeprecatedOption($key)
442
444
@trigger_error (sprintf ('Option "%s" given to router %s is deprecated since Symfony 4.3. ' , $ key , static ::class), E_USER_DEPRECATED );
443
445
}
444
446
}
447
+
448
+ private static function getCompiledRoutes (string $ path ): array
449
+ {
450
+ if ([] === self ::$ cache && \function_exists ('opcache_invalidate ' ) && filter_var (ini_get ('opcache.enable ' ), FILTER_VALIDATE_BOOLEAN ) && (!\in_array (\PHP_SAPI , ['cli ' , 'phpdbg ' ], true ) || filter_var (ini_get ('opcache.enable_cli ' ), FILTER_VALIDATE_BOOLEAN ))) {
451
+ self ::$ cache = null ;
452
+ }
453
+
454
+ if (null === self ::$ cache ) {
455
+ return require $ path ;
456
+ }
457
+
458
+ if (isset (self ::$ cache [$ path ])) {
459
+ return self ::$ cache [$ path ];
460
+ }
461
+
462
+ return self ::$ cache [$ path ] = require $ path ;
463
+ }
445
464
}
0 commit comments