TemplatePathsCacheWarmer.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\FrameworkBundle\CacheWarmer;
  11. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
  12. use Symfony\Component\HttpKernel\KernelInterface;
  13. use Symfony\Component\Finder\Finder;
  14. use Symfony\Component\Config\FileLocatorInterface;
  15. use Symfony\Bundle\FrameworkBundle\Templating\Template;
  16. use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
  17. /**
  18. * Computes the association between template names and their paths on the disk.
  19. *
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. */
  22. class TemplatePathsCacheWarmer extends CacheWarmer
  23. {
  24. protected $locator;
  25. protected $kernel;
  26. protected $rootDir;
  27. protected $parser;
  28. /**
  29. * Constructor.
  30. *
  31. * @param KernelInterface $kernel A KernelInterface instance
  32. * @param FileLocatorInterface $locator A FileLocatorInterface instance
  33. * @param TemplateNameParser $parser A TemplateNameParser instance
  34. * @param string $rootDir The directory where global templates can be stored
  35. */
  36. public function __construct(KernelInterface $kernel, FileLocatorInterface $locator, TemplateNameParser $parser, $rootDir)
  37. {
  38. $this->kernel = $kernel;
  39. $this->locator = $locator;
  40. $this->parser = $parser;
  41. $this->rootDir = $rootDir;
  42. }
  43. /**
  44. * Warms up the cache.
  45. *
  46. * @param string $cacheDir The cache directory
  47. */
  48. public function warmUp($cacheDir)
  49. {
  50. $templates = $this->computeTemplatePaths();
  51. $this->writeCacheFile($cacheDir.'/templates.php', sprintf('<?php return %s;', var_export($templates, true)));
  52. }
  53. /**
  54. * Checks whether this warmer is optional or not.
  55. *
  56. * @return Boolean always false
  57. */
  58. public function isOptional()
  59. {
  60. return false;
  61. }
  62. protected function computeTemplatePaths()
  63. {
  64. $prefix = '/Resources/views';
  65. $templates = array();
  66. foreach ($this->kernel->getBundles() as $name => $bundle) {
  67. if (!is_dir($dir = $bundle->getPath().$prefix)) {
  68. continue;
  69. }
  70. $finder = new Finder();
  71. foreach ($finder->files()->followLinks()->in($dir) as $file) {
  72. if (false !== $template = $this->parseTemplateName($file, $prefix.'/', $bundle->getName())) {
  73. $templates[$template->getSignature()] = $this->locator->locate($template->getPath(), $this->rootDir);
  74. }
  75. }
  76. }
  77. if (is_dir($this->rootDir)) {
  78. $finder = new Finder();
  79. foreach ($finder->files()->followLinks()->in($this->rootDir) as $file) {
  80. if (false !== $template = $this->parseTemplateName($file, strtr($this->rootDir, '\\', '/').'/')) {
  81. $templates[$template->getSignature()] = $file->getRealPath();
  82. }
  83. }
  84. }
  85. return $templates;
  86. }
  87. protected function parseTemplateName($file, $prefix, $bundle = '')
  88. {
  89. $prefix = strtr($prefix, '\\', '/');
  90. $path = strtr($file->getPathname(), '\\', '/');
  91. list(, $file) = explode($prefix, $path, 2);
  92. $template = $this->parser->parseFromFilename($file);
  93. if (false !== $template) {
  94. $template->set('bundle', $bundle);
  95. }
  96. return $template;
  97. }
  98. }