-
Notifications
You must be signed in to change notification settings - Fork 74
Add custom twig loader to workaround cache issue #116
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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')) { | ||
| $twigLoader = $container->findDefinition('twig.loader'); | ||
| $aliasedTo = $this->resolveAlias('twig.loader', $container); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method is called |
||
| if ($aliasedTo == 'twig.loader.chain') { | ||
| $methodCalls = $twigLoader->getMethodCalls(); | ||
| foreach($methodCalls as $index => $methodCall) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just overwrite the |
||
| 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be private
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bracket should be on new line |
||
| while($container->hasAlias($id)) { | ||
| $id = $container->getAlias($id); | ||
| } | ||
|
|
||
| return $id; | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove extra line |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be a space after |
||
| $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]); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove extra line |
||
| } | ||
There was a problem hiding this comment.
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: