Issue #2558885 by Cottser, jhedstrom, david_garcia, alexpott: TwigEnvironment is unable to cache inline templates because it sends invalid filenames to MTimeProtectedFastFileStorage

8.0.x
effulgentsia 2015-10-03 23:29:51 -07:00
parent 0dd08e66e3
commit 7ed830c3ab
2 changed files with 40 additions and 1 deletions

View File

@ -74,8 +74,18 @@ class TwigPhpStorageCache implements \Twig_CacheInterface {
public function generateKey($name, $className) {
$hash = hash('sha256', $className);
if (strpos($name, '{# inline_template_start #}') === 0) {
// $name is an inline template, and can have characters that are not valid
// for a filename. $hash is unique for each inline template so we just use
// the generic name 'inline-template' here.
$name = 'inline-template';
}
else {
$name = basename($name);
}
// The first part is what is invalidated.
return $this->templateCacheFilenamePrefix . '_' . basename($name) . '_' . $hash;
return $this->templateCacheFilenamePrefix . '_' . $name . '_' . $hash;
}
/**

View File

@ -65,6 +65,35 @@ class TwigEnvironmentTest extends KernelTestBase {
// Render it twice so that twig caching is triggered.
$this->assertEqual($renderer->renderRoot($element), 'test-with-context muuh');
$this->assertEqual($renderer->renderRoot($element_copy), 'test-with-context muuh');
// Tests caching of inline templates with long content to ensure the
// generated cache key can be used as a filename.
$element = [];
$element['test'] = [
'#type' => 'inline_template',
'#template' => 'Llamas sometimes spit and wrestle with their {{ llama }}. Kittens are soft and fuzzy and they sometimes say {{ kitten }}. Flamingos have long legs and they are usually {{ flamingo }}. Pandas eat bamboo and they are {{ panda }}. Giraffes have long necks and long tongues and they eat {{ giraffe }}.',
'#context' => [
'llama' => 'necks',
'kitten' => 'meow',
'flamingo' => 'pink',
'panda' => 'bears',
'giraffe' => 'leaves',
],
];
$expected = 'Llamas sometimes spit and wrestle with their necks. Kittens are soft and fuzzy and they sometimes say meow. Flamingos have long legs and they are usually pink. Pandas eat bamboo and they are bears. Giraffes have long necks and long tongues and they eat leaves.';
$element_copy = $element;
// Render it twice so that twig caching is triggered.
$this->assertEqual($renderer->renderRoot($element), $expected);
$this->assertEqual($renderer->renderRoot($element_copy), $expected);
$name = '{# inline_template_start #}' . $element['test']['#template'];
$hash = $this->container->getParameter('twig_extension_hash');
$cache = $environment->getCache();
$class = $environment->getTemplateClass($name);
$expected = $hash . '_inline-template' . '_' . hash('sha256', $class);
$this->assertEqual($expected, $cache->generateKey($name, $class));
}
/**