TemplateCacheWarmer.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\TwigBundle\CacheWarmer;
  11. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
  12. /**
  13. * Generates the Twig cache for all templates.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class TemplateCacheWarmer implements CacheWarmerInterface
  18. {
  19. private $twig;
  20. private $iterator;
  21. public function __construct(\Twig_Environment $twig, \Traversable $iterator)
  22. {
  23. $this->twig = $twig;
  24. $this->iterator = $iterator;
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function warmUp($cacheDir)
  30. {
  31. foreach ($this->iterator as $template) {
  32. try {
  33. $this->twig->loadTemplate($template);
  34. } catch (\Twig_Error $e) {
  35. // problem during compilation, give up
  36. // might be a syntax error or a non-Twig template
  37. }
  38. }
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function isOptional()
  44. {
  45. return true;
  46. }
  47. }