Skip to content

Commit 120c5fa

Browse files
Merge branch '4.4' into 5.0
* 4.4: Fix merge [DoctrineBridge] try to fix deprecations from doctrine/persistence [DI] Add support for immutable setters in CallTrait [Cache] Propagate expiry when syncing items in ChainAdapter Removed request header "Content-Type" from the preferred format guessing mechanism [Routing] fix memoryleak when loading compiled routes [Translation] fix memoryleak in PhpFileLoader fix triggering deprecation in file locator bug #34877 [TwigBundle] fix findTemplate() to return `null`
2 parents 3b9689c + 628bcaf commit 120c5fa

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Router.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class Router implements RouterInterface, RequestMatcherInterface
9292
*/
9393
private $expressionLanguageProviders = [];
9494

95+
private static $cache = [];
96+
9597
/**
9698
* @param mixed $resource The main resource to load
9799
*/
@@ -295,7 +297,7 @@ function (ConfigCacheInterface $cache) {
295297
}
296298
);
297299

298-
return $this->matcher = new $this->options['matcher_class'](require $cache->getPath(), $this->context);
300+
return $this->matcher = new $this->options['matcher_class'](self::getCompiledRoutes($cache->getPath()), $this->context);
299301
}
300302

301303
/**
@@ -325,7 +327,7 @@ function (ConfigCacheInterface $cache) {
325327
}
326328
);
327329

328-
$this->generator = new $this->options['generator_class'](require $cache->getPath(), $this->context, $this->logger, $this->defaultLocale);
330+
$this->generator = new $this->options['generator_class'](self::getCompiledRoutes($cache->getPath()), $this->context, $this->logger, $this->defaultLocale);
329331
}
330332

331333
if ($this->generator instanceof ConfigurableRequirementsInterface) {
@@ -368,4 +370,21 @@ private function getConfigCacheFactory(): ConfigCacheFactoryInterface
368370

369371
return $this->configCacheFactory;
370372
}
373+
374+
private static function getCompiledRoutes(string $path): array
375+
{
376+
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))) {
377+
self::$cache = null;
378+
}
379+
380+
if (null === self::$cache) {
381+
return require $path;
382+
}
383+
384+
if (isset(self::$cache[$path])) {
385+
return self::$cache[$path];
386+
}
387+
388+
return self::$cache[$path] = require $path;
389+
}
371390
}

0 commit comments

Comments
 (0)