Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom twig loader to workaround cache issue #116

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions DependencyInjection/Compiler/ThemeCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,34 @@ public function process(ContainerBuilder $container)

$container->setAlias('templating.cache_warmer.template_paths', 'liip_theme.templating.cache_warmer.template_paths');

if (true === $container->hasDefinition('twig')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to delegate this to a method:

if (true == ...) {
    $this->overrideTwigLoader($container);
}

$twigLoader = $container->findDefinition('twig.loader');
$aliasedTo = $this->resolveAlias('twig.loader', $container);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method is called resolveContainerAlias no?

if ($aliasedTo == 'twig.loader.chain') {
$methodCalls = $twigLoader->getMethodCalls();
foreach($methodCalls as $index => $methodCall) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just overwrite the twig.loader.filesystem?

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be private

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bracket should be on new line

private function (...)
{
 // ...
}

while($container->hasAlias($id)) {
$id = $container->getAlias($id);
}

return $id;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra line

}
39 changes: 39 additions & 0 deletions Loader/FilesystemLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing header

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing doc

{
public function __construct(FileLocatorInterface $locator, TemplateNameParserInterface $parser, ActiveTheme $activeTheme)
{
$this->activeTheme = $activeTheme;
parent::__construct($locator, $parser);
}

protected function findTemplate($template)
{
$logicalName = (string)$template;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a space after (string): (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]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we unset the local name cache here? If no theme is set then the "logical name" templates would not change, or am I wrong?

$this->cache[$cacheKey] = $file;

return $file;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra line

}
7 changes: 7 additions & 0 deletions Resources/config/templating.xml
Original file line number Diff line number Diff line change
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,12 @@
<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" />
</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