Skip to content

Commit ebadf51

Browse files
committed
bug #34920 [Routing] fix memoryleak when loading compiled routes (nicolas-grekas)
This PR was merged into the 4.3 branch. Discussion ---------- [Routing] fix memoryleak when loading compiled routes | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Same as #34918 for Routing. That's the last significant memory hog I've identified. Commits ------- 85371a1 [Routing] fix memoryleak when loading compiled routes
2 parents 3df1d1e + 85371a1 commit ebadf51

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/Symfony/Component/Routing/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 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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy