TemplateFinder.php 3.1 KB

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