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
|
* @author Fabien Potencier <fabien@symfony.com>
|
21
|
*/
|
22
|
class Twig_Loader_Array implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
|
23
|
{
|
24
|
protected $templates;
|
25
|
|
26
|
/**
|
27
|
* Constructor.
|
28
|
*
|
29
|
* @param array $templates An array of templates (keys are the names, and values are the source code)
|
30
|
*
|
31
|
* @see Twig_Loader
|
32
|
*/
|
33
|
public function __construct(array $templates)
|
34
|
{
|
35
|
$this->templates = array();
|
36
|
foreach ($templates as $name => $template) {
|
37
|
$this->templates[$name] = $template;
|
38
|
}
|
39
|
}
|
40
|
|
41
|
/**
|
42
|
* Adds or overrides a template.
|
43
|
*
|
44
|
* @param string $name The template name
|
45
|
* @param string $template The template source
|
46
|
*/
|
47
|
public function setTemplate($name, $template)
|
48
|
{
|
49
|
$this->templates[(string) $name] = $template;
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* {@inheritdoc}
|
54
|
*/
|
55
|
public function getSource($name)
|
56
|
{
|
57
|
$name = (string) $name;
|
58
|
if (!isset($this->templates[$name])) {
|
59
|
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
|
60
|
}
|
61
|
|
62
|
return $this->templates[$name];
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* {@inheritdoc}
|
67
|
*/
|
68
|
public function exists($name)
|
69
|
{
|
70
|
return isset($this->templates[(string) $name]);
|
71
|
}
|
72
|
|
73
|
/**
|
74
|
* {@inheritdoc}
|
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
|
/**
|
87
|
* {@inheritdoc}
|
88
|
*/
|
89
|
public function isFresh($name, $time)
|
90
|
{
|
91
|
$name = (string) $name;
|
92
|
if (!isset($this->templates[$name])) {
|
93
|
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
|
94
|
}
|
95
|
|
96
|
return true;
|
97
|
}
|
98
|
}
|