Skip to content

Commit 628bcaf

Browse files
Merge branch '4.3' into 4.4
* 4.3: 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 [Routing] fix memoryleak when loading compiled routes [Translation] fix memoryleak in PhpFileLoader
2 parents 51f3f20 + a756d19 commit 628bcaf

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
@@ -97,6 +97,8 @@ class Router implements RouterInterface, RequestMatcherInterface
9797
*/
9898
private $expressionLanguageProviders = [];
9999

100+
private static $cache = [];
101+
100102
/**
101103
* @param mixed $resource The main resource to load
102104
*/
@@ -319,7 +321,7 @@ function (ConfigCacheInterface $cache) {
319321
);
320322

321323
if ($compiled) {
322-
return $this->matcher = new $this->options['matcher_class'](require $cache->getPath(), $this->context);
324+
return $this->matcher = new $this->options['matcher_class'](self::getCompiledRoutes($cache->getPath()), $this->context);
323325
}
324326

325327
if (!class_exists($this->options['matcher_cache_class'], false)) {
@@ -363,7 +365,7 @@ function (ConfigCacheInterface $cache) {
363365
);
364366

365367
if ($compiled) {
366-
$this->generator = new $this->options['generator_class'](require $cache->getPath(), $this->context, $this->logger, $this->defaultLocale);
368+
$this->generator = new $this->options['generator_class'](self::getCompiledRoutes($cache->getPath()), $this->context, $this->logger, $this->defaultLocale);
367369
} else {
368370
if (!class_exists($this->options['generator_cache_class'], false)) {
369371
require_once $cache->getPath();
@@ -434,4 +436,21 @@ private function checkDeprecatedOption(string $key)
434436
@trigger_error(sprintf('Option "%s" given to router %s is deprecated since Symfony 4.3.', $key, static::class), E_USER_DEPRECATED);
435437
}
436438
}
439+
440+
private static function getCompiledRoutes(string $path): array
441+
{
442+
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))) {
443+
self::$cache = null;
444+
}
445+
446+
if (null === self::$cache) {
447+
return require $path;
448+
}
449+
450+
if (isset(self::$cache[$path])) {
451+
return self::$cache[$path];
452+
}
453+
454+
return self::$cache[$path] = require $path;
455+
}
437456
}

0 commit comments

Comments
 (0)