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 Twig_Error_Loader; |
16
|
|
|
use Twig_Source; |
17
|
|
|
use WyriHaximus\TwigView\View\TwigView; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Loader |
21
|
|
|
* @package WyriHaximus\TwigView\Lib\Twig |
22
|
|
|
*/ |
23
|
|
|
class Loader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface, \Twig_SourceContextLoaderInterface |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get the file contents of a template. |
28
|
|
|
* |
29
|
|
|
* @param string $name Template. |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
2 |
|
public function getSource($name) |
34
|
|
|
{ |
35
|
2 |
|
$name = $this->resolveFileName($name); |
36
|
1 |
|
return file_get_contents($name); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get cache key for template. |
41
|
|
|
* |
42
|
|
|
* @param string $name Template. |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
13 |
|
public function getCacheKey($name) |
47
|
|
|
{ |
48
|
13 |
|
return $this->resolveFileName($name); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Check if template is still fresh. |
53
|
|
|
* |
54
|
|
|
* @param string $name Template. |
55
|
|
|
* @param integer $time Timestamp. |
56
|
|
|
* |
57
|
|
|
* @return boolean |
58
|
|
|
*/ |
59
|
2 |
|
public function isFresh($name, $time) |
60
|
|
|
{ |
61
|
2 |
|
$name = $this->resolveFileName($name); |
62
|
1 |
|
return filemtime($name) < $time; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Resolve template name to filename. |
67
|
|
|
* |
68
|
|
|
* @param string $name Template. |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
* |
72
|
|
|
* @throws \Twig_Error_Loader Thrown when template file isn't found. |
73
|
|
|
*/ |
74
|
|
|
// @codingStandardsIgnoreStart |
75
|
17 |
|
protected function resolveFileName($name) |
76
|
|
|
{ |
77
|
|
|
// @codingStandardsIgnoreEnd |
78
|
17 |
|
if (file_exists($name)) { |
79
|
11 |
|
return $name; |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
list($plugin, $file) = pluginSplit($name); |
83
|
|
|
foreach ([ |
84
|
6 |
|
null, |
85
|
6 |
|
$plugin, |
86
|
6 |
|
] as $scope) { |
87
|
6 |
|
$paths = $this->getPaths($scope); |
88
|
6 |
|
foreach ($paths as $path) { |
89
|
6 |
|
$filePath = $path . $file; |
90
|
6 |
|
if (file_exists($filePath)) { |
91
|
2 |
|
return $filePath; |
92
|
|
|
} |
93
|
|
|
|
94
|
6 |
|
$filePath = $path . $file . TwigView::EXT; |
95
|
6 |
|
if (file_exists($filePath)) { |
96
|
3 |
|
return $filePath; |
97
|
|
|
} |
98
|
5 |
|
} |
99
|
5 |
|
} |
100
|
|
|
|
101
|
3 |
|
throw new \Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Check if $plugin is active and return it's template paths or return the aps template paths. |
106
|
|
|
* |
107
|
|
|
* @param string|null $plugin The plugin in question. |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
6 |
|
protected function getPaths($plugin) |
112
|
|
|
{ |
113
|
6 |
|
if ($plugin === null || !Plugin::loaded($plugin)) { |
114
|
6 |
|
return App::path('Template'); |
115
|
|
|
} |
116
|
|
|
|
117
|
4 |
|
return App::path('Template', $plugin); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function exists($name) |
121
|
|
|
{ |
122
|
|
|
$name = $this->resolveFileName($name); |
123
|
|
|
|
124
|
|
|
return file_exists($name); |
125
|
|
|
} |
126
|
|
|
|
127
|
6 |
|
public function getSourceContext($name) |
128
|
|
|
{ |
129
|
6 |
|
$path = $this->resolveFileName($name); |
130
|
|
|
|
131
|
6 |
|
return new Twig_Source(file_get_contents($path), $name, $path); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|