| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of Twig. |
| 5 |
* |
| 6 |
* (c) 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 |
* @final |
| 23 |
* |
| 24 |
* @author Fabien Potencier <fabien@symfony.com> |
| 25 |
*/ |
| 26 |
class Twig_Loader_Array implements Twig_LoaderInterface, Twig_ExistsLoaderInterface, Twig_SourceContextLoaderInterface |
| 27 |
{ |
| 28 |
protected $templates = array(); |
| 29 |
|
| 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 = array()) |
| 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 |
public function getSource($name) |
| 50 |
{ |
| 51 |
@trigger_error(sprintf('Calling "getSource" on "%s" is deprecated since 1.27. Use getSourceContext() instead.', get_class($this)), E_USER_DEPRECATED); |
| 52 |
|
| 53 |
$name = (string) $name; |
| 54 |
if (!isset($this->templates[$name])) { |
| 55 |
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name)); |
| 56 |
} |
| 57 |
|
| 58 |
return $this->templates[$name]; |
| 59 |
} |
| 60 |
|
| 61 |
public function getSourceContext($name) |
| 62 |
{ |
| 63 |
$name = (string) $name; |
| 64 |
if (!isset($this->templates[$name])) { |
| 65 |
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name)); |
| 66 |
} |
| 67 |
|
| 68 |
return new Twig_Source($this->templates[$name], $name); |
| 69 |
} |
| 70 |
|
| 71 |
public function exists($name) |
| 72 |
{ |
| 73 |
return isset($this->templates[(string) $name]); |
| 74 |
} |
| 75 |
|
| 76 |
public function getCacheKey($name) |
| 77 |
{ |
| 78 |
$name = (string) $name; |
| 79 |
if (!isset($this->templates[$name])) { |
| 80 |
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name)); |
| 81 |
} |
| 82 |
|
| 83 |
return $this->templates[$name]; |
| 84 |
} |
| 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 |
} |
| 96 |
|