Twig-1.3.0/lib/Twig/Loader/Array.php
changeset 4 9a001a04b634
equal deleted inserted replaced
3:6d109e3804ac 4:9a001a04b634
       
     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  * @package    twig
       
    21  * @author     Fabien Potencier <fabien@symfony.com>
       
    22  */
       
    23 class Twig_Loader_Array implements Twig_LoaderInterface
       
    24 {
       
    25     protected $templates;
       
    26 
       
    27     /**
       
    28      * Constructor.
       
    29      *
       
    30      * @param array $templates An array of templates (keys are the names, and values are the source code)
       
    31      *
       
    32      * @see Twig_Loader
       
    33      */
       
    34     public function __construct(array $templates)
       
    35     {
       
    36         $this->templates = array();
       
    37         foreach ($templates as $name => $template) {
       
    38             $this->templates[$name] = $template;
       
    39         }
       
    40     }
       
    41 
       
    42     /**
       
    43      * Adds or overrides a template.
       
    44      *
       
    45      * @param string $name     The template name
       
    46      * @param string $template The template source
       
    47      */
       
    48     public function setTemplate($name, $template)
       
    49     {
       
    50         $this->templates[$name] = $template;
       
    51     }
       
    52 
       
    53     /**
       
    54      * Gets the source code of a template, given its name.
       
    55      *
       
    56      * @param  string $name The name of the template to load
       
    57      *
       
    58      * @return string The template source code
       
    59      */
       
    60     public function getSource($name)
       
    61     {
       
    62         if (!isset($this->templates[$name])) {
       
    63             throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
       
    64         }
       
    65 
       
    66         return $this->templates[$name];
       
    67     }
       
    68 
       
    69     /**
       
    70      * Gets the cache key to use for the cache for a given template name.
       
    71      *
       
    72      * @param  string $name The name of the template to load
       
    73      *
       
    74      * @return string The cache key
       
    75      */
       
    76     public function getCacheKey($name)
       
    77     {
       
    78         if (!isset($this->templates[$name])) {
       
    79             throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
       
    80         }
       
    81 
       
    82         return $this->templates[$name];
       
    83     }
       
    84 
       
    85     /**
       
    86      * Returns true if the template is still fresh.
       
    87      *
       
    88      * @param string    $name The template name
       
    89      * @param timestamp $time The last modification time of the cached template
       
    90      */
       
    91     public function isFresh($name, $time)
       
    92     {
       
    93         return true;
       
    94     }
       
    95 }