Skip to content

[2.8]Fix optional cache warmers are always instantiated whereas they should be lazy-loaded #23014

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

Merged
merged 1 commit into from
Jun 2, 2017

Conversation

romainneutron
Copy link
Contributor

Q A
Branch? 2.8
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets N/A
License MIT
Doc PR N/A

Since version 2.8, if a Twig extension throws an exception in constructor, then the Kernel can not boot anymore because a service used by the Twig cache warmer instantiates Twig. As Twig cache warmer is optional, Twig should not be loaded at Kernel boot, and this patch fixes the issue

@romainneutron
Copy link
Contributor Author

romainneutron commented Jun 1, 2017

For the record, passing a Twig_Environment on constructor might be deprecated on 3.4. Are you okay with this ? In this case i'd do the PR once this one is merged

@romainneutron romainneutron force-pushed the fix-twig-template-cache branch from 7ac016f to 70cd98f Compare June 1, 2017 12:50
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

👍

@nicolas-grekas nicolas-grekas added this to the 2.8 milestone Jun 1, 2017
@stof
Copy link
Member

stof commented Jun 1, 2017

Isn't it true for any cache warmer though ? As soon as a cache warmer has deps, booting requires to instantiate the object graph

@romainneutron
Copy link
Contributor Author

@stof I don't know. What I'm sure is that is a regression from Symfony 2.7

@romainneutron
Copy link
Contributor Author

FYI here are the services that are instanciated on cache warm:
router / translator / doctrine manager registry

@romainneutron
Copy link
Contributor Author

romainneutron commented Jun 1, 2017

FYI the Symfony\Bridge\Doctrine\CacheWarmer\ProxyCacheWarmer is not optional, so there's no optimization to do here.
However, the same patch can be applied to
Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer and
Symfony\Bundle\FrameworkBundle\CacheWarmer\TranslationsCacheWarmer

@romainneutron romainneutron force-pushed the fix-twig-template-cache branch from 70cd98f to 2050e03 Compare June 1, 2017 16:00
@romainneutron
Copy link
Contributor Author

PR updated, I've fixed the three optional cache warmers

@romainneutron romainneutron changed the title [2.8][TwigBundle] Fix kernel bootability even if a twig extension fails on constructor [2.8]Fix optional cache warmers are always instantiated whereas they should be lazy-loaded Jun 1, 2017
Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

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

For the two additional cache warmers, you forgot to update the XML definitions.

@romainneutron romainneutron force-pushed the fix-twig-template-cache branch from 2050e03 to 922e699 Compare June 1, 2017 20:03
@romainneutron
Copy link
Contributor Author

Woops ! It's now fixed

@fabpot
Copy link
Member

fabpot commented Jun 1, 2017

Can you add a note on each cache warmer class to explain why we want to inject the Container instead of the real service? That would avoid PRs that remove that later on.

@@ -22,16 +23,18 @@
*/
class RouterCacheWarmer implements CacheWarmerInterface
{
private $container;
protected $router;
Copy link
Member

Choose a reason for hiding this comment

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

Let's revert changes on this file, it's a BC break because of this protected property.

* @param RouterInterface $router A Router instance
*/
public function __construct(RouterInterface $router)
public function __construct($container)
Copy link
Member

Choose a reason for hiding this comment

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

I would had a phpdocs with the 2 possible classes

@romainneutron romainneutron force-pushed the fix-twig-template-cache branch 2 times, most recently from 8f7bf58 to b93d61b Compare June 1, 2017 20:29
@romainneutron
Copy link
Contributor Author

PR updated comments addressed

@romainneutron romainneutron force-pushed the fix-twig-template-cache branch from b93d61b to 97bdf40 Compare June 1, 2017 20:30
{
$this->translator = $translator;
/**
Copy link
Member

Choose a reason for hiding this comment

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

fabbot is right :-)

* This is an optional cache warmer.
* It means this cache is not warm on boot but on explicit cache warm.
* To avoid loading the translator service on boot, we inject the container here.
*/
Copy link
Member

Choose a reason for hiding this comment

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

What about:

As this cache warmer is optional, dependencies should be lazy-loaded, that's why a container should be injected.

private $translator;

public function __construct(TranslatorInterface $translator)
public function __construct($container)
Copy link
Member

Choose a reason for hiding this comment

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

Can you add the phpdoc about the possible classes accepted for $container?

@romainneutron romainneutron force-pushed the fix-twig-template-cache branch from 97bdf40 to 3371db9 Compare June 2, 2017 07:54
@romainneutron
Copy link
Contributor Author

PR Updated comments addressed

@nicolas-grekas
Copy link
Member

Thank you @romainneutron.

@nicolas-grekas nicolas-grekas merged commit 3371db9 into symfony:2.8 Jun 2, 2017
nicolas-grekas added a commit that referenced this pull request Jun 2, 2017
…ereas they should be lazy-loaded (romainneutron)

This PR was merged into the 2.8 branch.

Discussion
----------

[2.8]Fix optional cache warmers are always instantiated whereas they should be lazy-loaded

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | yes
| New feature?  | no <!-- don't forget updating src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget updating UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | N/A

Since version 2.8, if a Twig extension throws an exception in constructor, then the Kernel can not boot anymore because a service used by the Twig cache warmer instantiates Twig. As Twig cache warmer is optional, Twig should not be loaded at Kernel boot, and this patch fixes the issue

Commits
-------

3371db9 Fix optional cache warmers are always instantiated whereas they should be lazy-loaded
@romainneutron romainneutron deleted the fix-twig-template-cache branch June 2, 2017 09:15
This was referenced Jun 5, 2017
@fabpot fabpot mentioned this pull request Jul 4, 2017
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.

4 participants