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

Conversation

xanido
Copy link

@xanido xanido commented Feb 9, 2015

Proof of concept solution for #79.

Apologies if I have missed something, but this seems as though it can be solved relatively simply by using a custom Twig filesystem loader, as stof suggested. This solution does not solve the 'referencing a non-themed template from a themed template' issue.

Suggestions are welcome.

fix #79

@lsmith77
Copy link
Contributor

one of the unit tests needs to be fixed. also there are some minor CS issues (specifically missing space after if and before (

@stof any feedback here?

@lsmith77
Copy link
Contributor

also it would be good to have a functional test to illustrate that this works.

@@ -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');
Copy link
Contributor

Choose a reason for hiding this comment

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

this is wrong. You should not replace all loaders by the liip_theme one, because it breaks things for people using more than the Twig filesystem loader

Copy link
Author

Choose a reason for hiding this comment

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

I knew that this wasn't ideal when I did it. I've had a go at doing this in a more flexible way. Do you have any thoughts on this?

@stof
Copy link
Contributor

stof commented Feb 10, 2015

This won't work as is, because the class name for the compiled template still does not take the theme into account, so the Twig_Environment will not load the template a second time

@xanido
Copy link
Author

xanido commented Feb 10, 2015

Ah! Good point, I hadn't considered that. I'll have a look and see what changes would be necessary to solve the compiled class name issue.

@xanido
Copy link
Author

xanido commented Feb 10, 2015

@stof, I believe you are incorrect in your diagnosis. When determining the class cache name, Twig_Environment::getTemplateClass() uses the return value from Twig_LoaderInterface::getCacheKey(). The behaviour of the default Twig_Loader_Filesystem::getCacheKey() is to simply use the value returned by findTemplate(), which returns the full path to the file.

Twig_FilesystemLoader is an ancestor of our new loader so we have no problem, because findTemplate() returns the full path to the template, including the theme name (e.g. 'themes/themename').

I will fix the CS, the broken test, and create a new test for this feature.

@lsmith77
Copy link
Contributor

@stof can you check once more?

@lsmith77
Copy link
Contributor

alternatively @dantleech do you have time to look at this PR?

@@ -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);
}

@dantleech
Copy link
Contributor

Would be great to add some tests for the compiler pass and the Loader (using prophecy would make this task easier).

@@ -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);
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?

@xanido
Copy link
Author

xanido commented Mar 15, 2015

Thanks for taking a look @dantleech! Lots of feedback there, I'll try to address it today and update this PR.

@dbu
Copy link
Member

dbu commented Mar 24, 2015

ping @xanido

@lsmith77
Copy link
Contributor

anyone have time to wrap this up?

@lsmith77 lsmith77 added this to the 1.3.0 milestone May 10, 2015
@ahilles107
Copy link
Contributor

it wasn't fixed with #123 ?

@lsmith77 lsmith77 modified the milestones: 1.3.0, 1.4.0 Aug 28, 2015
@lsmith77
Copy link
Contributor

@dantleech / @xanido can you get this wrapped up?

@lsmith77
Copy link
Contributor

ping

@lsmith77 lsmith77 removed this from the 1.4.0 milestone Jan 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using several themes in the same process
6 participants