shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

Array.php (2484B)


      1 <?php
      2 
      3 /*
      4  * This file is part of Twig.
      5  *
      6  * (c) 2009 Fabien Potencier
      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 /**
     13  * Loads a template from an array.
     14  *
     15  * When using this loader with a cache mechanism, you should know that a new cache
     16  * key is generated each time a template content "changes" (the cache key being the
     17  * source code of the template). If you don't want to see your cache grows out of
     18  * control, you need to take care of clearing the old cache file by yourself.
     19  *
     20  * This loader should only be used for unit testing.
     21  *
     22  * @author Fabien Potencier <fabien@symfony.com>
     23  */
     24 class Twig_Loader_Array implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
     25 {
     26     protected $templates = array();
     27 
     28     /**
     29      * Constructor.
     30      *
     31      * @param array $templates An array of templates (keys are the names, and values are the source code)
     32      */
     33     public function __construct(array $templates)
     34     {
     35         $this->templates = $templates;
     36     }
     37 
     38     /**
     39      * Adds or overrides a template.
     40      *
     41      * @param string $name     The template name
     42      * @param string $template The template source
     43      */
     44     public function setTemplate($name, $template)
     45     {
     46         $this->templates[(string) $name] = $template;
     47     }
     48 
     49     /**
     50      * {@inheritdoc}
     51      */
     52     public function getSource($name)
     53     {
     54         $name = (string) $name;
     55         if (!isset($this->templates[$name])) {
     56             throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
     57         }
     58 
     59         return $this->templates[$name];
     60     }
     61 
     62     /**
     63      * {@inheritdoc}
     64      */
     65     public function exists($name)
     66     {
     67         return isset($this->templates[(string) $name]);
     68     }
     69 
     70     /**
     71      * {@inheritdoc}
     72      */
     73     public function getCacheKey($name)
     74     {
     75         $name = (string) $name;
     76         if (!isset($this->templates[$name])) {
     77             throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
     78         }
     79 
     80         return $this->templates[$name];
     81     }
     82 
     83     /**
     84      * {@inheritdoc}
     85      */
     86     public function isFresh($name, $time)
     87     {
     88         $name = (string) $name;
     89         if (!isset($this->templates[$name])) {
     90             throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
     91         }
     92 
     93         return true;
     94     }
     95 }