TemplateCacheCacheWarmer.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\Finder\Finder;
  12. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface;
  15. use Symfony\Component\Templating\TemplateReference;
  16. /**
  17. * Generates the Twig cache for all templates.
  18. *
  19. * This warmer must be registered after TemplatePathsCacheWarmer,
  20. * as the Twig loader will need the cache generated by it.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. */
  24. class TemplateCacheCacheWarmer implements CacheWarmerInterface
  25. {
  26. protected $container;
  27. protected $finder;
  28. private $paths;
  29. /**
  30. * Constructor.
  31. *
  32. * @param ContainerInterface $container The dependency injection container
  33. * @param TemplateFinderInterface $finder The template paths cache warmer
  34. * @param array $paths Additional twig paths to warm
  35. */
  36. public function __construct(ContainerInterface $container, TemplateFinderInterface $finder = null, array $paths = array())
  37. {
  38. // We don't inject the Twig environment directly as it depends on the
  39. // template locator (via the loader) which might be a cached one.
  40. // The cached template locator is available once the TemplatePathsCacheWarmer
  41. // has been warmed up.
  42. // But it can also be null if templating has been disabled.
  43. $this->container = $container;
  44. $this->finder = $finder;
  45. $this->paths = $paths;
  46. }
  47. /**
  48. * Warms up the cache.
  49. *
  50. * @param string $cacheDir The cache directory
  51. */
  52. public function warmUp($cacheDir)
  53. {
  54. if (null === $this->finder) {
  55. return;
  56. }
  57. $twig = $this->container->get('twig');
  58. $templates = $this->finder->findAllTemplates();
  59. foreach ($this->paths as $path => $namespace) {
  60. $templates = array_merge($templates, $this->findTemplatesInFolder($namespace, $path));
  61. }
  62. foreach ($templates as $template) {
  63. if ('twig' !== $template->get('engine')) {
  64. continue;
  65. }
  66. try {
  67. $twig->loadTemplate($template);
  68. } catch (\Twig_Error $e) {
  69. // problem during compilation, give up
  70. }
  71. }
  72. }
  73. /**
  74. * Checks whether this warmer is optional or not.
  75. *
  76. * @return bool always true
  77. */
  78. public function isOptional()
  79. {
  80. return true;
  81. }
  82. /**
  83. * Find templates in the given directory.
  84. *
  85. * @param string $namespace The namespace for these templates
  86. * @param string $dir The folder where to look for templates
  87. *
  88. * @return array An array of templates of type TemplateReferenceInterface
  89. */
  90. private function findTemplatesInFolder($namespace, $dir)
  91. {
  92. if (!is_dir($dir)) {
  93. return array();
  94. }
  95. $templates = array();
  96. $finder = new Finder();
  97. foreach ($finder->files()->followLinks()->in($dir) as $file) {
  98. $name = $file->getRelativePathname();
  99. $templates[] = new TemplateReference(
  100. $namespace ? sprintf('@%s/%s', $namespace, $name) : $name,
  101. 'twig'
  102. );
  103. }
  104. return $templates;
  105. }
  106. }