@@ -97,6 +97,8 @@ class Router implements RouterInterface, RequestMatcherInterface
9797 */
9898 private $ expressionLanguageProviders = [];
9999
100+ private static $ cache = [];
101+
100102 /**
101103 * @param LoaderInterface $loader A LoaderInterface instance
102104 * @param mixed $resource The main resource to load
@@ -325,7 +327,7 @@ function (ConfigCacheInterface $cache) {
325327 );
326328
327329 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 );
329331 }
330332
331333 if (!class_exists ($ this ->options ['matcher_cache_class ' ], false )) {
@@ -369,7 +371,7 @@ function (ConfigCacheInterface $cache) {
369371 );
370372
371373 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 );
373375 } else {
374376 if (!class_exists ($ this ->options ['generator_cache_class ' ], false )) {
375377 require_once $ cache ->getPath ();
@@ -442,4 +444,21 @@ private function checkDeprecatedOption($key)
442444 @trigger_error (sprintf ('Option "%s" given to router %s is deprecated since Symfony 4.3. ' , $ key , static ::class), E_USER_DEPRECATED );
443445 }
444446 }
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+ }
445464}
0 commit comments