Skip to content

Commit

Permalink
Merge branch '4.4' into 5.0
Browse files Browse the repository at this point in the history
* 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`
  • Loading branch information
nicolas-grekas committed Dec 12, 2019
2 parents 3b9689c + 628bcaf commit 120c5fa
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class Router implements RouterInterface, RequestMatcherInterface
*/
private $expressionLanguageProviders = [];

private static $cache = [];

/**
* @param mixed $resource The main resource to load
*/
Expand Down Expand Up @@ -295,7 +297,7 @@ function (ConfigCacheInterface $cache) {
}
);

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

/**
Expand Down Expand Up @@ -325,7 +327,7 @@ function (ConfigCacheInterface $cache) {
}
);

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

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

return $this->configCacheFactory;
}

private static function getCompiledRoutes(string $path): array
{
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))) {
self::$cache = null;
}

if (null === self::$cache) {
return require $path;
}

if (isset(self::$cache[$path])) {
return self::$cache[$path];
}

return self::$cache[$path] = require $path;
}
}

0 comments on commit 120c5fa

Please sign in to comment.