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
|
|
|
public function getSource($name) |
34
|
|
|
{ |
35
|
|
|
$name = $this->resolveFileName($name); |
36
|
|
|
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
|
|
|
public function getCacheKey($name) |
47
|
|
|
{ |
48
|
|
|
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
|
|
|
public function isFresh($name, $time) |
60
|
|
|
{ |
61
|
|
|
$name = $this->resolveFileName($name); |
62
|
|
|
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
|
|
|
protected function resolveFileName($name) |
76
|
|
|
{ |
77
|
|
|
// @codingStandardsIgnoreEnd |
78
|
|
|
if (file_exists($name)) { |
79
|
|
|
return $name; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
list($plugin, $file) = pluginSplit($name); |
83
|
|
|
foreach ([ |
84
|
|
|
null, |
85
|
|
|
$plugin, |
86
|
|
|
] as $scope) { |
87
|
|
|
$paths = $this->getPaths($scope); |
88
|
|
|
foreach ($paths as $path) { |
89
|
|
|
$filePath = $path . $file; |
90
|
|
|
if (file_exists($filePath)) { |
91
|
|
|
return $filePath; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$filePath = $path . $file . TwigView::EXT; |
95
|
|
|
if (file_exists($filePath)) { |
96
|
|
|
return $filePath; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
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
|
|
|
protected function getPaths($plugin) |
112
|
|
|
{ |
113
|
|
|
if ($plugin === null || !Plugin::loaded($plugin)) { |
114
|
|
|
return App::path('Template'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
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
|
|
|
public function getSourceContext($name) |
128
|
|
|
{ |
129
|
|
|
$path = $this->resolveFileName($name); |
130
|
|
|
|
131
|
|
|
return new Twig_Source(file_get_contents($path), $name, $path); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.