From c501b2da4d6c67e591ee332bd3e8a8393a7eed35 Mon Sep 17 00:00:00 2001 From: Tim Anido <tim@pictura.com.au> Date: Mon, 9 Feb 2015 14:21:44 +1100 Subject: [PATCH 1/2] 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 @@ +<?php + +namespace Liip\ThemeBundle\Loader; + + +use Liip\ThemeBundle\ActiveTheme; +use Symfony\Bundle\TwigBundle\Loader\FilesystemLoader as BaseFilesystemLoader; +use Symfony\Component\Config\FileLocatorInterface; +use Symfony\Component\Templating\TemplateNameParserInterface; + +class FilesystemLoader extends BaseFilesystemLoader +{ + public function __construct(FileLocatorInterface $locator, TemplateNameParserInterface $parser, ActiveTheme $activeTheme) + { + $this->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 @@ <parameter key="liip_theme.active_theme.class">Liip\ThemeBundle\ActiveTheme</parameter> <parameter key="liip_theme.cache_warmer.class">Liip\ThemeBundle\CacheWarmer\TemplatePathsCacheWarmer</parameter> <parameter key="liip_theme.theme_auto_detect.class">Liip\ThemeBundle\Helper\DeviceDetection</parameter> + <parameter key="liip_theme.twig.loader.filesystem.class">Liip\ThemeBundle\Loader\FilesystemLoader</parameter> </parameters> <services> @@ -25,6 +26,13 @@ <argument type="service" id="liip_theme.active_theme" /> </service> + <service id="liip_theme.twig.loader.filesystem" class="%liip_theme.twig.loader.filesystem.class%" public="false"> + <argument type="service" id="templating.locator" /> + <argument type="service" id="templating.name_parser" /> + <argument type="service" id="liip_theme.active_theme" /> + <tag name="twig.loader"/> + </service> + <service id="liip_theme.file_locator" class="%liip_theme.file_locator.class%" public="false"> <argument type="service" id="kernel" /> <argument type="service" id="liip_theme.active_theme" /> From e7510799b5fefb3b9ac816697c8c4f90d9e06437 Mon Sep 17 00:00:00 2001 From: Tim Anido <tim@pictura.com.au> Date: Wed, 11 Feb 2015 11:20:22 +1100 Subject: [PATCH 2/2] Replace default twig.loader.filesystem in more flexible way --- .../Compiler/ThemeCompilerPass.php | 25 ++++++++++++++++++- Loader/FilesystemLoader.php | 3 --- Resources/config/templating.xml | 1 - 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/DependencyInjection/Compiler/ThemeCompilerPass.php b/DependencyInjection/Compiler/ThemeCompilerPass.php index 30949cc..a5cde62 100644 --- a/DependencyInjection/Compiler/ThemeCompilerPass.php +++ b/DependencyInjection/Compiler/ThemeCompilerPass.php @@ -23,11 +23,34 @@ 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 (true === $container->hasDefinition('twig')) { + $twigLoader = $container->findDefinition('twig.loader'); + $aliasedTo = $this->resolveAlias('twig.loader', $container); + if ($aliasedTo == 'twig.loader.chain') { + $methodCalls = $twigLoader->getMethodCalls(); + foreach($methodCalls as $index => $methodCall) { + if ($methodCall[0] == 'addLoader' && (string) $methodCall[1][0] == 'twig.loader.filesystem') { + $methodCalls[$index] = array($methodCall[0], array(new Reference('liip_theme.twig.loader.filesystem'))); + } + } + $twigLoader->setMethodCalls($methodCalls); + } elseif ($aliasedTo == 'twig.loader.filesystem') { + $container->setAlias('twig.loader.filesystem', 'liip_theme.twig.loader.filesystem'); + } + } if (!$container->getParameter('liip_theme.cache_warming')) { $container->getDefinition('liip_theme.templating.cache_warmer.template_paths') ->replaceArgument(2, null); } } + + public function resolveContainerAlias($id, ContainerBuilder $container) { + while($container->hasAlias($id)) { + $id = $container->getAlias($id); + } + + return $id; + } + } diff --git a/Loader/FilesystemLoader.php b/Loader/FilesystemLoader.php index c549123..0fd2de5 100644 --- a/Loader/FilesystemLoader.php +++ b/Loader/FilesystemLoader.php @@ -36,7 +36,4 @@ protected function findTemplate($template) return $file; } - - - } \ No newline at end of file diff --git a/Resources/config/templating.xml b/Resources/config/templating.xml index b2ad142..e405a52 100644 --- a/Resources/config/templating.xml +++ b/Resources/config/templating.xml @@ -30,7 +30,6 @@ <argument type="service" id="templating.locator" /> <argument type="service" id="templating.name_parser" /> <argument type="service" id="liip_theme.active_theme" /> - <tag name="twig.loader"/> </service> <service id="liip_theme.file_locator" class="%liip_theme.file_locator.class%" public="false">