Skip to content

Commit

Permalink
Merge pull request #123 from wizhippo/theme-aware-filesystem-loader
Browse files Browse the repository at this point in the history
Add theme aware file system loader
  • Loading branch information
lsmith77 committed Apr 23, 2015
2 parents 4bb3d28 + 96f49fc commit 2383220
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
4 changes: 4 additions & 0 deletions DependencyInjection/Compiler/ThemeCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ public function process(ContainerBuilder $container)
$container->getDefinition('liip_theme.templating.cache_warmer.template_paths')
->replaceArgument(2, null);
}

$twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.filesystem');
$twigFilesystemLoaderDefinition->setClass($container->getParameter('liip_theme.filesystem_loader.class'));
$twigFilesystemLoaderDefinition->addArgument(new Reference('liip_theme.active_theme'));
}
}
1 change: 1 addition & 0 deletions Resources/config/templating.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<parameters>
<parameter key="liip_theme.templating_locator.class">Liip\ThemeBundle\Locator\TemplateLocator</parameter>
<parameter key="liip_theme.file_locator.class">Liip\ThemeBundle\Locator\FileLocator</parameter>
<parameter key="liip_theme.filesystem_loader.class">Liip\ThemeBundle\Twig\Loader\FilesystemLoader</parameter>
<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>
Expand Down
17 changes: 14 additions & 3 deletions Tests/DependencyInjection/Compiler/ThemeCompilerPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,21 @@ public function testProcess()
->method('setAlias')
;

$containerMock->expects($this->once())
$containerMock->expects($this->exactly(2))
->method('getParameter')
->with('liip_theme.cache_warming')
->will($this->returnValue(true))
->will($this->returnValueMap(
array(
array('liip_theme.cache_warming', true),
array('liip_theme.filesystem_loader.class', 'Liip\ThemeBundle\Twig\Loader\FilesystemLoader')
)
)
)
;

$containerMock->expects($this->once())
->method('getDefinition')
->with('twig.loader.filesystem')
->willReturn($this->getMock('Symfony\Component\DependencyInjection\Definition'))
;

$themeCompiler = new ThemeCompilerPass();
Expand Down
81 changes: 81 additions & 0 deletions Twig/Loader/FilesystemLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Liip\ThemeBundle\Twig\Loader;

use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\Templating\TemplateNameParserInterface;
use Symfony\Component\Templating\TemplateReferenceInterface;
use Liip\ThemeBundle\ActiveTheme;

class FilesystemLoader extends \Twig_Loader_Filesystem
{
protected $locator;
protected $parser;
/**
* @var ActiveTheme|null
*/
protected $activeTheme;

/**
* Constructor.
*
* @param FileLocatorInterface $locator A FileLocatorInterface instance
* @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
* @param ActiveTheme $activeTheme
*/
public function __construct(FileLocatorInterface $locator, TemplateNameParserInterface $parser, ActiveTheme $activeTheme = null)
{
parent::__construct(array());
$this->locator = $locator;
$this->parser = $parser;
$this->activeTheme = $activeTheme;
}

/**
* Returns the path to the template file.
*
* The file locator is used to locate the template when the naming convention
* is the symfony one (i.e. the name can be parsed).
* Otherwise the template is located using the locator from the twig library.
*
* @param string|TemplateReferenceInterface $template The template
*
* @return string The path to the template file
*
* @throws \Twig_Error_Loader if the template could not be found
*/
protected function findTemplate($template)
{
$logicalName = (string) $template;

if ($this->activeTheme) {
$logicalName .= '|' . $this->activeTheme->getName();
}

if (isset($this->cache[$logicalName])) {
return $this->cache[$logicalName];
}

$file = null;
$previous = null;
try {
$file = parent::findTemplate((string) $template);
} catch (\Twig_Error_Loader $e) {
$previous = $e;

// for BC
try {
$template = $this->parser->parse($template);
$file = $this->locator->locate($template);
} catch (\Exception $e) {
$previous = $e;
}
}

if (false === $file || null === $file) {
throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', $logicalName), -1, null, $previous);
}

return $this->cache[$logicalName] = $file;
}
}

0 comments on commit 2383220

Please sign in to comment.