From 7ed830c3ab66754e16c609e82a32e7d566561e40 Mon Sep 17 00:00:00 2001 From: effulgentsia Date: Sat, 3 Oct 2015 23:29:51 -0700 Subject: [PATCH] Issue #2558885 by Cottser, jhedstrom, david_garcia, alexpott: TwigEnvironment is unable to cache inline templates because it sends invalid filenames to MTimeProtectedFastFileStorage --- .../Core/Template/TwigPhpStorageCache.php | 12 +++++++- .../src/Tests/Theme/TwigEnvironmentTest.php | 29 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php b/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php index 4f5e6b1473e..d2cf9182151 100644 --- a/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php +++ b/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php @@ -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; } /** diff --git a/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php b/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php index 1bab9bf44f7..0bc52d0fedc 100644 --- a/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php +++ b/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php @@ -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)); } /**