Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* Add a way to mark a `Twig\Markup` instance as safe for some escaping strategies only
* Make `Environment::getExtension()` return the runtime of an extension class registered with `AttributeExtension`
* Make `Environment::getRuntime()` fall back to the registered extension of the same class
* Speed up `getCacheKey()` in `ChainLoader` and `FilesystemLoader` for template names already resolved

# 3.30.0 (2026-09-25)

Expand Down
12 changes: 11 additions & 1 deletion src/Loader/ChainLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ final class ChainLoader implements LoaderInterface
*/
private $hasSourceCache = [];

/**
* @var array<string, string>
*/
private $cacheKeyCache = [];

/**
* @param iterable<LoaderInterface> $loaders
*/
Expand All @@ -44,6 +49,7 @@ public function addLoader(LoaderInterface $loader): void
})();

$this->hasSourceCache = [];
$this->cacheKeyCache = [];
}

/**
Expand Down Expand Up @@ -94,6 +100,10 @@ public function exists(string $name): bool

public function getCacheKey(string $name): string
{
if (isset($this->cacheKeyCache[$name])) {
return $this->cacheKeyCache[$name];
}

$exceptions = [];

foreach ($this->getLoaders() as $loader) {
Expand All @@ -102,7 +112,7 @@ public function getCacheKey(string $name): string
}

try {
return $loader->getCacheKey($name);
return $this->cacheKeyCache[$name] = $loader->getCacheKey($name);
} catch (LoaderError $e) {
$exceptions[] = $loader::class.': '.$e->getMessage();
}
Expand Down
17 changes: 13 additions & 4 deletions src/Loader/FilesystemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class FilesystemLoader implements LoaderInterface
protected $cache = [];
protected $errorCache = [];

/**
* @var array<string, string>
*/
private $cacheKeyCache = [];

private $rootPath;

/**
Expand Down Expand Up @@ -94,7 +99,7 @@ public function setPaths($paths, string $namespace = self::MAIN_NAMESPACE): void
public function addPath(string $path, string $namespace = self::MAIN_NAMESPACE/* , bool $check = true */): void
{
// invalidate the cache
$this->cache = $this->errorCache = [];
$this->cache = $this->errorCache = $this->cacheKeyCache = [];

if (\func_num_args() < 3 || func_get_arg(2)) {
$checkPath = $this->isAbsolutePath($path) ? $path : $this->rootPath.$path;
Expand All @@ -114,7 +119,7 @@ public function addPath(string $path, string $namespace = self::MAIN_NAMESPACE/*
public function prependPath(string $path, string $namespace = self::MAIN_NAMESPACE/* , bool $check = true */): void
{
// invalidate the cache
$this->cache = $this->errorCache = [];
$this->cache = $this->errorCache = $this->cacheKeyCache = [];

if (\func_num_args() < 3 || func_get_arg(2)) {
$checkPath = $this->isAbsolutePath($path) ? $path : $this->rootPath.$path;
Expand Down Expand Up @@ -143,15 +148,19 @@ public function getSourceContext(string $name): Source

public function getCacheKey(string $name): string
{
if (isset($this->cacheKeyCache[$name])) {
return $this->cacheKeyCache[$name];
}

if (null === $path = $this->findTemplate($name)) {
return '';
}
$len = \strlen($this->rootPath);
if (0 === strncmp($this->rootPath, $path, $len)) {
return substr($path, $len);
return $this->cacheKeyCache[$name] = substr($path, $len);
}

return $path;
return $this->cacheKeyCache[$name] = $path;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/Loader/ChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ public function testGetCacheKey(): void
$this->assertEquals('bar:foo', $loader->getCacheKey('bar'));
}

public function testGetCacheKeyFollowsLoadersAddedAfterItWasComputed(): void
{
$loader = new ChainLoader([new ArrayLoader(['foo' => 'bar'])]);

$this->assertEquals('foo:bar', $loader->getCacheKey('foo'));

$loader->addLoader(new ArrayLoader(['bar' => 'foo']));

$this->assertEquals('foo:bar', $loader->getCacheKey('foo'));
$this->assertEquals('bar:foo', $loader->getCacheKey('bar'));
}

public function testGetCacheKeyWhenTemplateDoesNotExist(): void
{
$loader = new ChainLoader([]);
Expand Down
12 changes: 12 additions & 0 deletions tests/Loader/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ public function testPaths($basePath, $cacheKey, $rootPath): void
$this->assertEquals("named path (final)\n", $loader->getSourceContext('@named/index.html')->getCode());
}

public function testGetCacheKeyFollowsPathsPrependedAfterItWasComputed(): void
{
$basePath = __DIR__.'/Fixtures';
$loader = new FilesystemLoader([$basePath.'/normal'], $basePath);

$this->assertSame('normal/index.html', str_replace('\\', '/', $loader->getCacheKey('index.html')));

$loader->prependPath($basePath.'/normal_final');

$this->assertSame('normal_final/index.html', str_replace('\\', '/', $loader->getCacheKey('index.html')));
}

public static function getBasePaths()
{
return [
Expand Down
Loading