1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of TwigView. |
5
|
|
|
* |
6
|
|
|
** (c) 2014 Cees-Jan Kiewiet |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace WyriHaximus\TwigView\Lib\Twig; |
12
|
|
|
|
13
|
|
|
use Cake\Core\App; |
14
|
|
|
use Cake\Core\Plugin; |
15
|
|
|
use WyriHaximus\TwigView\View\TwigView; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Loader |
19
|
|
|
* @package WyriHaximus\TwigView\Lib\Twig |
20
|
|
|
*/ |
21
|
|
|
class Loader implements \Twig_LoaderInterface, \Twig_SourceContextLoaderInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get the file contents of a template. |
26
|
|
|
* |
27
|
|
|
* @param string $name Template. |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
4 |
|
public function getSource($name) |
32
|
|
|
{ |
33
|
4 |
|
$name = $this->resolveFileName($name); |
34
|
|
|
|
35
|
3 |
|
return file_get_contents($name); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns the source context for a given template logical name. |
40
|
|
|
* |
41
|
|
|
* @param string $name The template logical name |
42
|
|
|
* |
43
|
|
|
* @return Twig_Source |
44
|
|
|
* |
45
|
|
|
* @throws Twig_Error_Loader When $name is not found |
46
|
|
|
*/ |
47
|
2 |
|
public function getSourceContext($name) |
48
|
|
|
{ |
49
|
2 |
|
$code = $this->getSource($name); |
50
|
2 |
|
$path = $this->getFilename($name); |
51
|
|
|
|
52
|
2 |
|
return new \Twig_Source($code, $name, $path); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get cache key for template. |
57
|
|
|
* |
58
|
|
|
* @param string $name Template. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
5 |
|
public function getCacheKey($name) |
63
|
|
|
{ |
64
|
5 |
|
return $this->resolveFileName($name); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Check if template is still fresh. |
69
|
|
|
* |
70
|
|
|
* @param string $name Template. |
71
|
|
|
* @param integer $time Timestamp. |
72
|
|
|
* |
73
|
|
|
* @return boolean |
74
|
|
|
*/ |
75
|
2 |
|
public function isFresh($name, $time) |
76
|
|
|
{ |
77
|
2 |
|
$name = $this->resolveFileName($name); |
78
|
|
|
|
79
|
1 |
|
return filemtime($name) < $time; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Check if we have the source code of a template, given its name. |
84
|
|
|
* |
85
|
|
|
* @param string $name The name of the template to check if we can load |
86
|
|
|
* |
87
|
|
|
* @return bool If the template source code is handled by this loader or not |
88
|
|
|
*/ |
89
|
|
|
// @codingStandardsIgnoreStart |
90
|
|
|
public function exists($name) { |
91
|
|
|
// @codingStandardsIgnoreEnd |
92
|
|
|
$filename = $this->getFilename($name); |
93
|
|
|
if ($filename === false) { |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Resolve template name to filename. |
102
|
|
|
* |
103
|
|
|
* @param string $name Template. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
* |
107
|
|
|
* @throws \Twig_Error_Loader Thrown when template file isn't found. |
108
|
|
|
*/ |
109
|
|
|
// @codingStandardsIgnoreStart |
110
|
9 |
|
protected function resolveFileName($name) |
111
|
|
|
{ |
112
|
9 |
|
$filename = $this->getFilename($name); |
113
|
9 |
|
if ($filename === false) { |
114
|
3 |
|
throw new \Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name)); |
115
|
|
|
} |
116
|
|
|
|
117
|
6 |
|
return $filename; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get template filename |
122
|
|
|
* |
123
|
|
|
* @param string $name Template. |
124
|
|
|
* |
125
|
|
|
* @return string|false |
126
|
|
|
* |
127
|
|
|
*/ |
128
|
|
|
// @codingStandardsIgnoreStart |
129
|
9 |
|
protected function getFilename($name) |
130
|
|
|
{ |
131
|
|
|
// @codingStandardsIgnoreEnd |
132
|
9 |
|
if (file_exists($name)) { |
133
|
3 |
|
return $name; |
134
|
|
|
} |
135
|
|
|
|
136
|
6 |
|
list($plugin, $file) = pluginSplit($name); |
137
|
6 |
|
foreach ([null, $plugin] as $scope) { |
138
|
6 |
|
$paths = $this->getPaths($scope); |
139
|
6 |
|
foreach ($paths as $path) { |
140
|
6 |
|
$filePath = $path . $file; |
141
|
6 |
|
if (file_exists($filePath)) { |
142
|
2 |
|
return $filePath; |
143
|
|
|
} |
144
|
|
|
|
145
|
6 |
|
$filePath = $path . $file . TwigView::EXT; |
146
|
6 |
|
if (file_exists($filePath)) { |
147
|
3 |
|
return $filePath; |
148
|
|
|
} |
149
|
5 |
|
} |
150
|
5 |
|
} |
151
|
|
|
|
152
|
3 |
|
return false; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Check if $plugin is active and return it's template paths or return the aps template paths. |
157
|
|
|
* |
158
|
|
|
* @param string|null $plugin The plugin in question. |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
6 |
|
protected function getPaths($plugin) |
163
|
|
|
{ |
164
|
6 |
|
if ($plugin === null || !Plugin::loaded($plugin)) { |
165
|
6 |
|
return App::path('Template'); |
166
|
|
|
} |
167
|
|
|
|
168
|
4 |
|
return App::path('Template', $plugin); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|