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 |
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
|
3 |
|
return file_get_contents($name); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get cache key for template. |
39
|
|
|
* |
40
|
|
|
* @param string $name Template. |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
5 |
|
public function getCacheKey($name) |
45
|
|
|
{ |
46
|
5 |
|
return $this->resolveFileName($name); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Check if template is still fresh. |
51
|
|
|
* |
52
|
|
|
* @param string $name Template. |
53
|
|
|
* @param integer $time Timestamp. |
54
|
|
|
* |
55
|
|
|
* @return boolean |
56
|
|
|
*/ |
57
|
2 |
|
public function isFresh($name, $time) |
58
|
|
|
{ |
59
|
2 |
|
$name = $this->resolveFileName($name); |
60
|
1 |
|
return filemtime($name) < $time; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Resolve template name to filename. |
65
|
|
|
* |
66
|
|
|
* @param string $name Template. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
* |
70
|
|
|
* @throws \Twig_Error_Loader Thrown when template file isn't found. |
71
|
|
|
*/ |
72
|
|
|
// @codingStandardsIgnoreStart |
73
|
9 |
|
protected function resolveFileName($name) |
74
|
|
|
{ |
75
|
|
|
// @codingStandardsIgnoreEnd |
76
|
9 |
|
if (file_exists($name)) { |
77
|
3 |
|
return $name; |
78
|
|
|
} |
79
|
|
|
|
80
|
6 |
|
list($plugin, $file) = pluginSplit($name); |
81
|
|
|
foreach ([ |
82
|
6 |
|
null, |
83
|
6 |
|
$plugin, |
84
|
6 |
|
] as $scope) { |
85
|
6 |
|
$paths = $this->getPaths($scope); |
86
|
6 |
|
foreach ($paths as $path) { |
87
|
6 |
|
$filePath = $path . $file; |
88
|
6 |
|
if (file_exists($filePath)) { |
89
|
2 |
|
return $filePath; |
90
|
|
|
} |
91
|
|
|
|
92
|
6 |
|
$filePath = $path . $file . TwigView::EXT; |
93
|
6 |
|
if (file_exists($filePath)) { |
94
|
3 |
|
return $filePath; |
95
|
|
|
} |
96
|
5 |
|
} |
97
|
5 |
|
} |
98
|
|
|
|
99
|
3 |
|
throw new \Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Check if $plugin is active and return it's template paths or return the aps template paths. |
104
|
|
|
* |
105
|
|
|
* @param string|null $plugin The plugin in question. |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
6 |
|
protected function getPaths($plugin) |
110
|
|
|
{ |
111
|
6 |
|
if ($plugin === null || !Plugin::loaded($plugin)) { |
112
|
6 |
|
return App::path('Template'); |
113
|
|
|
} |
114
|
|
|
|
115
|
4 |
|
return App::path('Template', $plugin); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|