symfony / symfony

The Symfony PHP framework
https://symfony.com
MIT License
29.75k stars 9.46k forks source link

Symfony 2.3 to 2.8 / CacheWarmer->warmUp is called later? #18919

Closed wuestkamp closed 8 years ago

wuestkamp commented 8 years ago

Hi there,

we updated our application from 2.3 to 2.8. All went kind of well except one error. We have a custom CmsWarmer extending CacheWarmer.

It seems that since 2.8 the CmsWarmer->warmUp gets called after the constructor of a custom Twig_Extension. This breaks because our Twig_Extension relies on file generated during CmsWarmer->warmUp.

Setting priority with _kernel.cachewarmer does make the CmsWarmer->__construct to be executed before the Twig_Extension->__construct, but the CmsWarmer->warmUp still gets called at the very end.

Any documentation on that or idea how to solve?

Thanks, Kim

stof commented 8 years ago

@kimwue Symfony makes absolutely no guarantee about when a particular service will get instantiated compared to calls to another service, as this depends on the whole object graph of dependencies between services. For your case, this is because there is now a cache warmer in TwigBundle warming up the Twig template, and so Twig needs to be instantiated when getting cache warmers.

This is why it is not recommended to put logic in the constructor (this is a general recommendation when using dependency injection, to avoid making heavy computation just because some other class asked for its dependency). Giving you more idea about how to refactor the logic to account for it is not possible with the amount of info available here, as I don't know what these classes are doing.

wuestkamp commented 8 years ago

Thanks, that clears things up. We'll adjust our logic based on your info. Thanks for fast and great support!