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.potencier@symfony-project.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 | * Gets the source code of a template, given its name. |
---|
44 | * |
---|
45 | * @param string $name The name of the template to load |
---|
46 | * |
---|
47 | * @return string The template source code |
---|
48 | */ |
---|
49 | public function getSource($name) |
---|
50 | { |
---|
51 | if (!isset($this->templates[$name])) { |
---|
52 | throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name)); |
---|
53 | } |
---|
54 | |
---|
55 | return $this->templates[$name]; |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * Gets the cache key to use for the cache for a given template name. |
---|
60 | * |
---|
61 | * @param string $name The name of the template to load |
---|
62 | * |
---|
63 | * @return string The cache key |
---|
64 | */ |
---|
65 | public function getCacheKey($name) |
---|
66 | { |
---|
67 | if (!isset($this->templates[$name])) { |
---|
68 | throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name)); |
---|
69 | } |
---|
70 | |
---|
71 | return $this->templates[$name]; |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * Returns true if the template is still fresh. |
---|
76 | * |
---|
77 | * @param string $name The template name |
---|
78 | * @param timestamp $time The last modification time of the cached template |
---|
79 | */ |
---|
80 | public function isFresh($name, $time) |
---|
81 | { |
---|
82 | return true; |
---|
83 | } |
---|
84 | } |
---|