From c501b2da4d6c67e591ee332bd3e8a8393a7eed35 Mon Sep 17 00:00:00 2001 From: Tim Anido Date: Mon, 9 Feb 2015 14:21:44 +1100 Subject: [PATCH] Add custom twig loader to workaround cache issue --- .../Compiler/ThemeCompilerPass.php | 2 + Loader/FilesystemLoader.php | 42 +++++++++++++++++++ Resources/config/templating.xml | 8 ++++ 3 files changed, 52 insertions(+) create mode 100644 Loader/FilesystemLoader.php diff --git a/DependencyInjection/Compiler/ThemeCompilerPass.php b/DependencyInjection/Compiler/ThemeCompilerPass.php index b88cb2e..30949cc 100644 --- a/DependencyInjection/Compiler/ThemeCompilerPass.php +++ b/DependencyInjection/Compiler/ThemeCompilerPass.php @@ -23,6 +23,8 @@ public function process(ContainerBuilder $container) $container->setAlias('templating.cache_warmer.template_paths', 'liip_theme.templating.cache_warmer.template_paths'); + $container->setAlias('twig.loader', 'liip_theme.twig.loader.filesystem'); + if (!$container->getParameter('liip_theme.cache_warming')) { $container->getDefinition('liip_theme.templating.cache_warmer.template_paths') ->replaceArgument(2, null); diff --git a/Loader/FilesystemLoader.php b/Loader/FilesystemLoader.php new file mode 100644 index 0000000..c549123 --- /dev/null +++ b/Loader/FilesystemLoader.php @@ -0,0 +1,42 @@ +activeTheme = $activeTheme; + parent::__construct($locator, $parser); + } + + protected function findTemplate($template) + { + $logicalName = (string)$template; + $cacheKey = $logicalName; + if($theme = $this->activeTheme->getName()) { + $cacheKey = $cacheKey . '|' . $theme; + } + + if(isset($this->cache[$cacheKey])) { + return $this->cache[$cacheKey]; + } + + $file = parent::findTemplate($template); + + unset($this->cache[$logicalName]); + $this->cache[$cacheKey] = $file; + + return $file; + } + + + + +} \ No newline at end of file diff --git a/Resources/config/templating.xml b/Resources/config/templating.xml index 56da6f0..b2ad142 100644 --- a/Resources/config/templating.xml +++ b/Resources/config/templating.xml @@ -9,6 +9,7 @@ Liip\ThemeBundle\ActiveTheme Liip\ThemeBundle\CacheWarmer\TemplatePathsCacheWarmer Liip\ThemeBundle\Helper\DeviceDetection + Liip\ThemeBundle\Loader\FilesystemLoader @@ -25,6 +26,13 @@ + + + + + + +