TemplateFinder.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\FrameworkBundle\CacheWarmer;
  11. use Symfony\Component\Finder\Finder;
  12. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  13. use Symfony\Component\HttpKernel\KernelInterface;
  14. use Symfony\Component\Templating\TemplateNameParserInterface;
  15. use Symfony\Component\Templating\TemplateReferenceInterface;
  16. /**
  17. * Finds all the templates.
  18. *
  19. * @author Victor Berchet <victor@suumit.com>
  20. */
  21. class TemplateFinder implements TemplateFinderInterface
  22. {
  23. private $kernel;
  24. private $parser;
  25. private $rootDir;
  26. private $templates;
  27. /**
  28. * @param KernelInterface $kernel A KernelInterface instance
  29. * @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
  30. * @param string $rootDir The directory where global templates can be stored
  31. */
  32. public function __construct(KernelInterface $kernel, TemplateNameParserInterface $parser, $rootDir)
  33. {
  34. $this->kernel = $kernel;
  35. $this->parser = $parser;
  36. $this->rootDir = $rootDir;
  37. }
  38. /**
  39. * Find all the templates in the bundle and in the kernel Resources folder.
  40. *
  41. * @return TemplateReferenceInterface[]
  42. */
  43. public function findAllTemplates()
  44. {
  45. if (null !== $this->templates) {
  46. return $this->templates;
  47. }
  48. $templates = array();
  49. foreach ($this->kernel->getBundles() as $bundle) {
  50. $templates = array_merge($templates, $this->findTemplatesInBundle($bundle));
  51. }
  52. $templates = array_merge($templates, $this->findTemplatesInFolder($this->rootDir.'/views'));
  53. return $this->templates = $templates;
  54. }
  55. /**
  56. * Find templates in the given directory.
  57. *
  58. * @param string $dir The folder where to look for templates
  59. *
  60. * @return TemplateReferenceInterface[]
  61. */
  62. private function findTemplatesInFolder($dir)
  63. {
  64. $templates = array();
  65. if (is_dir($dir)) {
  66. $finder = new Finder();
  67. foreach ($finder->files()->followLinks()->in($dir) as $file) {
  68. $template = $this->parser->parse($file->getRelativePathname());
  69. if (false !== $template) {
  70. $templates[] = $template;
  71. }
  72. }
  73. }
  74. return $templates;
  75. }
  76. /**
  77. * Find templates in the given bundle.
  78. *
  79. * @param BundleInterface $bundle The bundle where to look for templates
  80. *
  81. * @return TemplateReferenceInterface[]
  82. */
  83. private function findTemplatesInBundle(BundleInterface $bundle)
  84. {
  85. $name = $bundle->getName();
  86. $templates = array_unique(array_merge(
  87. $this->findTemplatesInFolder($bundle->getPath().'/Resources/views'),
  88. $this->findTemplatesInFolder($this->rootDir.'/'.$name.'/views')
  89. ));
  90. foreach ($templates as $i => $template) {
  91. $templates[$i] = $template->set('bundle', $name);
  92. }
  93. return $templates;
  94. }
  95. }