vendor/symfony/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TemplateFinder.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
       
    13 
       
    14 use Symfony\Component\HttpKernel\KernelInterface;
       
    15 use Symfony\Component\Finder\Finder;
       
    16 use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
       
    17 use Symfony\Component\HttpKernel\Bundle\BundleInterface;
       
    18 
       
    19 /**
       
    20  * Finds all the templates.
       
    21  *
       
    22  * @author Victor Berchet <victor@suumit.com>
       
    23  */
       
    24 class TemplateFinder implements TemplateFinderInterface
       
    25 {
       
    26     private $kernel;
       
    27     private $parser;
       
    28     private $rootDir;
       
    29     private $templates;
       
    30 
       
    31     /**
       
    32      * Constructor.
       
    33      *
       
    34      * @param KernelInterface      $kernel  A KernelInterface instance
       
    35      * @param TemplateNameParser   $parser  A TemplateNameParser instance
       
    36      * @param string               $rootDir The directory where global templates can be stored
       
    37      */
       
    38     public function __construct(KernelInterface $kernel, TemplateNameParser $parser, $rootDir)
       
    39     {
       
    40         $this->kernel = $kernel;
       
    41         $this->parser = $parser;
       
    42         $this->rootDir = $rootDir;
       
    43     }
       
    44 
       
    45     /**
       
    46      * Find all the templates in the bundle and in the kernel Resources folder.
       
    47      *
       
    48      * @return array An array of templates of type TemplateReferenceInterface
       
    49      */
       
    50     public function findAllTemplates()
       
    51     {
       
    52         if (null !== $this->templates) {
       
    53             return $this->templates;
       
    54         }
       
    55 
       
    56         $templates = array();
       
    57 
       
    58         foreach ($this->kernel->getBundles() as $name => $bundle) {
       
    59             $templates = array_merge($templates, $this->findTemplatesInBundle($bundle));
       
    60         }
       
    61 
       
    62         $templates = array_merge($templates, $this->findTemplatesInFolder($this->rootDir.'/views'));
       
    63 
       
    64         return $this->templates = $templates;
       
    65     }
       
    66 
       
    67     /**
       
    68      * Find templates in the given directory.
       
    69      *
       
    70      * @param string $dir The folder where to look for templates
       
    71      *
       
    72      * @return array An array of templates of type TemplateReferenceInterface
       
    73      */
       
    74     private function findTemplatesInFolder($dir)
       
    75     {
       
    76         $templates = array();
       
    77 
       
    78         if (is_dir($dir)) {
       
    79             $finder = new Finder();
       
    80             foreach ($finder->files()->followLinks()->in($dir) as $file) {
       
    81                 $template = $this->parser->parseFromFilename($file->getRelativePathname());
       
    82                 if (false !== $template) {
       
    83                     $templates[] = $template;
       
    84                 }
       
    85             }
       
    86         }
       
    87 
       
    88         return $templates;
       
    89     }
       
    90 
       
    91     /**
       
    92      * Find templates in the given bundle.
       
    93      *
       
    94      * @param BundleInterface $bundle The bundle where to look for templates
       
    95      *
       
    96      * @return array An array of templates of type TemplateReferenceInterface
       
    97      */
       
    98     private function findTemplatesInBundle(BundleInterface $bundle)
       
    99     {
       
   100         $templates = $this->findTemplatesInFolder($bundle->getPath().'/Resources/views');
       
   101         $name = $bundle->getName();
       
   102 
       
   103         foreach ($templates as $i => $template) {
       
   104             $templates[$i] = $template->set('bundle', $name);
       
   105         }
       
   106 
       
   107         return $templates;
       
   108     }
       
   109 }