vendor/symfony/twig-bundle/TemplateIterator.php line 22

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\TwigBundle;
  11. use Symfony\Component\Finder\Finder;
  12. use Symfony\Component\HttpKernel\KernelInterface;
  13. /**
  14.  * Iterator for all templates in bundles and in the application Resources directory.
  15.  *
  16.  * @author Fabien Potencier <fabien@symfony.com>
  17.  */
  18. class TemplateIterator implements \IteratorAggregate
  19. {
  20.     private $kernel;
  21.     private $rootDir;
  22.     private $templates;
  23.     private $paths;
  24.     private $defaultPath;
  25.     /**
  26.      * @param KernelInterface $kernel      A KernelInterface instance
  27.      * @param string          $rootDir     The directory where global templates can be stored
  28.      * @param array           $paths       Additional Twig paths to warm
  29.      * @param string          $defaultPath The directory where global templates can be stored
  30.      */
  31.     public function __construct(KernelInterface $kernel$rootDir, array $paths = [], $defaultPath null)
  32.     {
  33.         $this->kernel $kernel;
  34.         $this->rootDir $rootDir;
  35.         $this->paths $paths;
  36.         $this->defaultPath $defaultPath;
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function getIterator()
  42.     {
  43.         if (null !== $this->templates) {
  44.             return $this->templates;
  45.         }
  46.         $this->templates array_merge(
  47.             $this->findTemplatesInDirectory($this->rootDir.'/Resources/views'),
  48.             $this->findTemplatesInDirectory($this->defaultPathnull, ['bundles'])
  49.         );
  50.         foreach ($this->kernel->getBundles() as $bundle) {
  51.             $name $bundle->getName();
  52.             if ('Bundle' === substr($name, -6)) {
  53.                 $name substr($name0, -6);
  54.             }
  55.             $this->templates array_merge(
  56.                 $this->templates,
  57.                 $this->findTemplatesInDirectory($bundle->getPath().'/Resources/views'$name),
  58.                 $this->findTemplatesInDirectory($this->rootDir.'/Resources/'.$bundle->getName().'/views'$name),
  59.                 $this->findTemplatesInDirectory($this->defaultPath.'/bundles/'.$bundle->getName(), $name)
  60.             );
  61.         }
  62.         foreach ($this->paths as $dir => $namespace) {
  63.             $this->templates array_merge($this->templates$this->findTemplatesInDirectory($dir$namespace));
  64.         }
  65.         return $this->templates = new \ArrayIterator(array_unique($this->templates));
  66.     }
  67.     /**
  68.      * Find templates in the given directory.
  69.      *
  70.      * @param string      $dir       The directory where to look for templates
  71.      * @param string|null $namespace The template namespace
  72.      *
  73.      * @return array
  74.      */
  75.     private function findTemplatesInDirectory($dir$namespace null, array $excludeDirs = [])
  76.     {
  77.         if (!is_dir($dir)) {
  78.             return [];
  79.         }
  80.         $templates = [];
  81.         foreach (Finder::create()->files()->followLinks()->in($dir)->exclude($excludeDirs) as $file) {
  82.             $templates[] = (null !== $namespace '@'.$namespace.'/' '').str_replace('\\''/'$file->getRelativePathname());
  83.         }
  84.         return $templates;
  85.     }
  86. }