|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\View\Twig; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
// From `twig/twig` |
|
9
|
|
|
use Twig_LoaderInterface; |
|
10
|
|
|
use Twig_Source; |
|
11
|
|
|
|
|
12
|
|
|
// Parent namespace dependencies |
|
13
|
|
|
use Charcoal\View\AbstractLoader; |
|
14
|
|
|
use Charcoal\View\LoaderInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The Charcoal View Twig Loader implements both Twig_LoaderInterface and its own LoaderInterface. |
|
18
|
|
|
*/ |
|
19
|
|
|
class TwigLoader extends AbstractLoader implements |
|
20
|
|
|
LoaderInterface, |
|
21
|
|
|
Twig_LoaderInterface |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Convert an identifier to a file path. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $ident The identifier to convert. |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function filenameFromIdent($ident) |
|
31
|
|
|
{ |
|
32
|
|
|
$filename = str_replace([ '\\' ], '.', $ident); |
|
33
|
|
|
$filename .= '.twig'; |
|
34
|
|
|
|
|
35
|
|
|
return $filename; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Twig_LoaderInterface > getSource() |
|
40
|
|
|
* |
|
41
|
|
|
* Gets the source code of a template, given its name. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $name The name of the template to load. |
|
44
|
|
|
* @return string The template source code. |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getSource($name) |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->load($name); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Twig_LoaderInterface > getSourceContext() |
|
53
|
|
|
* |
|
54
|
|
|
* Gets the source code of a template, given its name. |
|
55
|
|
|
* For Twig 2.x |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $name The name of the template to load. |
|
58
|
|
|
* @return Twig_Source The template source object. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getSourceContext($name) |
|
61
|
|
|
{ |
|
62
|
|
|
$source = $this->load($name); |
|
63
|
|
|
return new Twig_Source($source, $name); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Twig_LoaderInterface > exists() |
|
68
|
|
|
* |
|
69
|
|
|
* For Twig 2.x |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $name The name of the template to load. |
|
72
|
|
|
* @return boolean |
|
73
|
|
|
*/ |
|
74
|
|
|
public function exists($name) |
|
75
|
|
|
{ |
|
76
|
|
|
return !!$this->findTemplateFile($name); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Twig_LoaderInterface > getCacheKey() |
|
81
|
|
|
* |
|
82
|
|
|
* Gets the cache key to use for the cache for a given template name. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $name The name of the template to load. |
|
85
|
|
|
* @return string The cache key |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getCacheKey($name) |
|
88
|
|
|
{ |
|
89
|
|
|
$key = $this->findTemplateFile($name); |
|
90
|
|
|
return $key; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Twig_LoaderInterface > isFresh() |
|
95
|
|
|
* |
|
96
|
|
|
* Returns true if the template is still fresh. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $name The template name. |
|
99
|
|
|
* @param integer $time The last modification time of the cached template. |
|
100
|
|
|
* @return boolean |
|
101
|
|
|
*/ |
|
102
|
|
|
public function isFresh($name, $time) |
|
103
|
|
|
{ |
|
104
|
|
|
$file = $this->findTemplateFile($name); |
|
105
|
|
|
$fresh = (filemtime($file) <= $time); |
|
106
|
|
|
return $fresh; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|