Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 85371a1

Browse filesBrowse files
[Routing] fix memoryleak when loading compiled routes
1 parent 7f2d9c2 commit 85371a1
Copy full SHA for 85371a1

File tree

Expand file treeCollapse file tree

1 file changed

+21
-2
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+21
-2
lines changed

‎src/Symfony/Component/Routing/Router.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Router.php
+21-2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.