1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\View\Twig; |
4
|
|
|
|
5
|
|
|
// From Twig |
6
|
|
|
use Twig_LoaderInterface; |
7
|
|
|
use Twig_Source; |
8
|
|
|
|
9
|
|
|
// From 'charcoal-view' |
10
|
|
|
use Charcoal\View\AbstractLoader; |
11
|
|
|
use Charcoal\View\LoaderInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The Charcoal View Twig Loader implements both Twig_LoaderInterface and its own LoaderInterface. |
15
|
|
|
*/ |
16
|
|
|
class TwigLoader extends AbstractLoader implements |
17
|
|
|
LoaderInterface, |
18
|
|
|
Twig_LoaderInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Determine if the variable is a template literal. |
22
|
|
|
* |
23
|
|
|
* This method looks for any tag delimiters in the given string, |
24
|
|
|
* which a file path would most likely not have. |
25
|
|
|
* |
26
|
|
|
* @todo Add support for custom delimiters. |
27
|
|
|
* @param string $ident The template being evaluated. |
28
|
|
|
* @return boolean |
29
|
|
|
*/ |
30
|
|
|
protected function isTemplateString($ident) |
31
|
|
|
{ |
32
|
|
|
return strpos($ident, '{%') !== false || parent::isTemplateString($ident); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Convert an identifier to a file path. |
37
|
|
|
* |
38
|
|
|
* @param string $ident The identifier to convert. |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
protected function filenameFromIdent($ident) |
42
|
|
|
{ |
43
|
|
|
$filename = str_replace([ '\\' ], '.', $ident); |
44
|
|
|
$filename .= '.twig'; |
45
|
|
|
|
46
|
|
|
return $filename; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Twig_LoaderInterface > getSource() |
51
|
|
|
* |
52
|
|
|
* Gets the source code of a template, given its name. |
53
|
|
|
* |
54
|
|
|
* @param string $name The name of the template to load. |
55
|
|
|
* @return string The template source code. |
56
|
|
|
*/ |
57
|
|
|
public function getSource($name) |
58
|
|
|
{ |
59
|
|
|
return $this->load($name); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Twig_LoaderInterface > getSourceContext() |
64
|
|
|
* |
65
|
|
|
* Gets the source code of a template, given its name. |
66
|
|
|
* For Twig 2.x |
67
|
|
|
* |
68
|
|
|
* @param string $name The name of the template to load. |
69
|
|
|
* @return Twig_Source The template source object. |
70
|
|
|
*/ |
71
|
|
|
public function getSourceContext($name) |
72
|
|
|
{ |
73
|
|
|
$source = $this->load($name); |
|
|
|
|
74
|
|
|
return new Twig_Source($source, $name); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Twig_LoaderInterface > exists() |
79
|
|
|
* |
80
|
|
|
* For Twig 2.x |
81
|
|
|
* |
82
|
|
|
* @param string $name The name of the template to load. |
83
|
|
|
* @return boolean |
84
|
|
|
*/ |
85
|
|
|
public function exists($name) |
86
|
|
|
{ |
87
|
|
|
return !!$this->findTemplateFile($name); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Twig_LoaderInterface > getCacheKey() |
92
|
|
|
* |
93
|
|
|
* Gets the cache key to use for the cache for a given template name. |
94
|
|
|
* |
95
|
|
|
* @param string $name The name of the template to load. |
96
|
|
|
* @return string|null The cache key |
97
|
|
|
*/ |
98
|
|
|
public function getCacheKey($name) |
99
|
|
|
{ |
100
|
|
|
$key = $this->findTemplateFile($name); |
101
|
|
|
return $key; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Twig_LoaderInterface > isFresh() |
106
|
|
|
* |
107
|
|
|
* Returns true if the template is still fresh. |
108
|
|
|
* |
109
|
|
|
* @param string $name The template name. |
110
|
|
|
* @param integer $time The last modification time of the cached template. |
111
|
|
|
* @return boolean |
112
|
|
|
*/ |
113
|
|
|
public function isFresh($name, $time) |
114
|
|
|
{ |
115
|
|
|
$file = $this->findTemplateFile($name); |
116
|
|
|
$fresh = (filemtime($file) <= $time); |
117
|
|
|
return $fresh; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.