vendor/symfony/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.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\TwigBundle\Loader;
       
    13 
       
    14 use Symfony\Component\Templating\TemplateNameParserInterface;
       
    15 use Symfony\Component\Config\FileLocatorInterface;
       
    16 
       
    17 /**
       
    18  * FilesystemLoader extends the default Twig filesystem loader
       
    19  * to work with the Symfony2 paths.
       
    20  *
       
    21  * @author Fabien Potencier <fabien@symfony.com>
       
    22  */
       
    23 class FilesystemLoader extends \Twig_Loader_Filesystem
       
    24 {
       
    25     protected $locator;
       
    26     protected $parser;
       
    27 
       
    28     /**
       
    29      * Constructor.
       
    30      *
       
    31      * @param FileLocatorInterface        $locator A FileLocatorInterface instance
       
    32      * @param TemplateNameParserInterface $parser  A TemplateNameParserInterface instance
       
    33      */
       
    34     public function __construct(FileLocatorInterface $locator, TemplateNameParserInterface $parser)
       
    35     {
       
    36         $this->locator = $locator;
       
    37         $this->parser = $parser;
       
    38         $this->cache = array();
       
    39     }
       
    40 
       
    41     /**
       
    42      * Returns the path to the template file.
       
    43      *
       
    44      * The file locator is used to locate the template when the naming convention
       
    45      * is the symfony one (i.e. the name can be parsed).
       
    46      * Otherwise the template is located using the locator from the twig library.
       
    47      *
       
    48      * @param string|TemplateReferenceInterface $template The template
       
    49      *
       
    50      * @return string The path to the template file
       
    51      *
       
    52      * @throws \Twig_Error_Loader if the template could not be found
       
    53      */
       
    54     protected function findTemplate($template)
       
    55     {
       
    56         $logicalName = (string) $template;
       
    57 
       
    58         if (isset($this->cache[$logicalName])) {
       
    59             return $this->cache[$logicalName];
       
    60         }
       
    61 
       
    62         $file = null;
       
    63         $previous = null;
       
    64         try {
       
    65             $template = $this->parser->parse($template);
       
    66             try {
       
    67                 $file = $this->locator->locate($template);
       
    68             } catch (\InvalidArgumentException $e) {
       
    69                 $previous = $e;
       
    70             }
       
    71         } catch (\Exception $e) {
       
    72             try {
       
    73                 $file = parent::findTemplate($template);
       
    74             } catch (\Twig_Error_Loader $e) {
       
    75                 $previous = $e;
       
    76             }
       
    77         }
       
    78 
       
    79         if (false === $file || null === $file) {
       
    80             throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', $logicalName), -1, null, $previous);
       
    81         }
       
    82 
       
    83         return $this->cache[$logicalName] = $file;
       
    84     }
       
    85 }