TemplatePathsCacheWarmer.php 3.0 KB

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