1 |
1686
|
darkviper
|
<?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[(string) $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 |
|
|
$name = (string) $name;
|
63 |
|
|
if (!isset($this->templates[$name])) {
|
64 |
|
|
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
return $this->templates[$name];
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
/**
|
71 |
|
|
* Gets the cache key to use for the cache for a given template name.
|
72 |
|
|
*
|
73 |
|
|
* @param string $name The name of the template to load
|
74 |
|
|
*
|
75 |
|
|
* @return string The cache key
|
76 |
|
|
*/
|
77 |
|
|
public function getCacheKey($name)
|
78 |
|
|
{
|
79 |
|
|
$name = (string) $name;
|
80 |
|
|
if (!isset($this->templates[$name])) {
|
81 |
|
|
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
return $this->templates[$name];
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
/**
|
88 |
|
|
* Returns true if the template is still fresh.
|
89 |
|
|
*
|
90 |
|
|
* @param string $name The template name
|
91 |
|
|
* @param timestamp $time The last modification time of the cached template
|
92 |
|
|
*/
|
93 |
|
|
public function isFresh($name, $time)
|
94 |
|
|
{
|
95 |
|
|
$name = (string) $name;
|
96 |
|
|
if (!isset($this->templates[$name])) {
|
97 |
|
|
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
return true;
|
101 |
|
|
}
|
102 |
|
|
}
|