Skip to content

Commit

Permalink
Add custom twig loader to workaround cache issue
Browse files Browse the repository at this point in the history
  • Loading branch information
xanido committed Feb 9, 2015
1 parent a594cc6 commit c501b2d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DependencyInjection/Compiler/ThemeCompilerPass.php
Expand Up @@ -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);
Expand Down
42 changes: 42 additions & 0 deletions 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;
}




}
8 changes: 8 additions & 0 deletions Resources/config/templating.xml
Expand Up @@ -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>
Expand All @@ -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" />
Expand Down

0 comments on commit c501b2d

Please sign in to comment.