vendor/symfony/framework-bundle/CacheWarmer/TemplateFinder.php line 14

Open in your IDE?
  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. @trigger_error('The '.TemplateFinder::class.' class is deprecated since version 4.3 and will be removed in 5.0; use Twig instead.', \E_USER_DEPRECATED);
  12. use Symfony\Component\Finder\Finder;
  13. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  14. use Symfony\Component\HttpKernel\KernelInterface;
  15. use Symfony\Component\Templating\TemplateNameParserInterface;
  16. use Symfony\Component\Templating\TemplateReferenceInterface;
  17. /**
  18.  * Finds all the templates.
  19.  *
  20.  * @author Victor Berchet <victor@suumit.com>
  21.  *
  22.  * @deprecated since version 4.3, to be removed in 5.0; use Twig instead.
  23.  */
  24. class TemplateFinder implements TemplateFinderInterface
  25. {
  26.     private $kernel;
  27.     private $parser;
  28.     private $rootDir;
  29.     private $templates;
  30.     /**
  31.      * @param string $rootDir The directory where global templates can be stored
  32.      */
  33.     public function __construct(KernelInterface $kernelTemplateNameParserInterface $parserstring $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 TemplateReferenceInterface[]
  43.      */
  44.     public function findAllTemplates()
  45.     {
  46.         if (null !== $this->templates) {
  47.             return $this->templates;
  48.         }
  49.         $templates = [];
  50.         foreach ($this->kernel->getBundles() as $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.      * @return TemplateReferenceInterface[]
  60.      */
  61.     private function findTemplatesInFolder(string $dir): array
  62.     {
  63.         $templates = [];
  64.         if (is_dir($dir)) {
  65.             $finder = new Finder();
  66.             foreach ($finder->files()->followLinks()->in($dir) as $file) {
  67.                 $template $this->parser->parse($file->getRelativePathname());
  68.                 if (false !== $template) {
  69.                     $templates[] = $template;
  70.                 }
  71.             }
  72.         }
  73.         return $templates;
  74.     }
  75.     /**
  76.      * Find templates in the given bundle.
  77.      *
  78.      * @param BundleInterface $bundle The bundle where to look for templates
  79.      *
  80.      * @return TemplateReferenceInterface[]
  81.      */
  82.     private function findTemplatesInBundle(BundleInterface $bundle): array
  83.     {
  84.         $name $bundle->getName();
  85.         $templates array_unique(array_merge(
  86.             $this->findTemplatesInFolder($bundle->getPath().'/Resources/views'),
  87.             $this->findTemplatesInFolder($this->rootDir.'/'.$name.'/views')
  88.         ));
  89.         foreach ($templates as $i => $template) {
  90.             $templates[$i] = $template->set('bundle'$name);
  91.         }
  92.         return $templates;
  93.     }
  94. }