|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/template package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Template\Engine; |
|
11
|
|
|
|
|
12
|
|
|
use Slick\Template\TemplateEngineInterface; |
|
13
|
|
|
use Twig_Environment; |
|
14
|
|
|
use Twig_Extension_Debug; |
|
15
|
|
|
use Twig_Loader_Filesystem; |
|
16
|
|
|
use Twig_TemplateWrapper; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Twig |
|
20
|
|
|
* |
|
21
|
|
|
* @package Slick\Template\Engine |
|
22
|
|
|
*/ |
|
23
|
|
|
class Twig implements TemplateEngineInterface |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private $optionsMap = [ |
|
30
|
|
|
'debug' => 'debug', |
|
31
|
|
|
'autoEscape' => 'autoescape', |
|
32
|
|
|
'strictVariables' => 'strict_variables', |
|
33
|
|
|
'autoReload' => 'auto_reload', |
|
34
|
|
|
'cache' => 'cache', |
|
35
|
|
|
'baseTemplateClass' => 'base_template_class', |
|
36
|
|
|
'charset' => 'charset', |
|
37
|
|
|
'optimizations' => 'optimizations' |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
private $defaultOptions = [ |
|
44
|
|
|
'debug' => false, |
|
45
|
|
|
'autoEscape' => true, |
|
46
|
|
|
'strictVariables' => false, |
|
47
|
|
|
'autoReload' => false, |
|
48
|
|
|
'cache' => false, |
|
49
|
|
|
'baseTemplateClass' => 'Twig_Template', |
|
50
|
|
|
'charset' => 'utf8', |
|
51
|
|
|
'optimizations' => -1 |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var array |
|
56
|
|
|
*/ |
|
57
|
|
|
private $options = []; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var Twig_Environment |
|
61
|
|
|
*/ |
|
62
|
|
|
private $twigEnvironment; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var array |
|
66
|
|
|
*/ |
|
67
|
|
|
private $locations = []; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var Twig_Loader_Filesystem |
|
71
|
|
|
*/ |
|
72
|
|
|
private $loader; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var Twig_TemplateWrapper |
|
76
|
|
|
*/ |
|
77
|
|
|
private $template; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Creates a Twig template engine |
|
81
|
|
|
* |
|
82
|
|
|
* @param array $options |
|
83
|
|
|
* @param Twig_Environment $twigEnvironment |
|
84
|
|
|
*/ |
|
85
|
|
|
public function __construct(array $options = [], Twig_Environment $twigEnvironment = null) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->options = array_merge($this->defaultOptions, $options); |
|
88
|
|
|
$this->twigEnvironment = $twigEnvironment; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Engine configuration options |
|
93
|
|
|
* |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
public function options() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->options; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Parses the source template code. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $source The template to parse |
|
105
|
|
|
* |
|
106
|
|
|
* @return TemplateEngineInterface|self|$this |
|
107
|
|
|
* |
|
108
|
|
|
* @throws \Twig_Error_Loader When the template cannot be found |
|
109
|
|
|
* @throws \Twig_Error_Runtime When a previously generated cache is corrupted |
|
110
|
|
|
* @throws \Twig_Error_Syntax When an error occurred during compilation |
|
111
|
|
|
*/ |
|
112
|
|
|
public function parse($source) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->template = $this->getTwigEnvironment()->load($source); |
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Processes the template with data to produce the final output. |
|
120
|
|
|
* |
|
121
|
|
|
* @param mixed $data The data that will be used to process the view. |
|
122
|
|
|
* |
|
123
|
|
|
* @return string Returns processed output string. |
|
124
|
|
|
*/ |
|
125
|
|
|
public function process($data = array()) |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->template->render($data); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Sets the list of available locations for template files. |
|
132
|
|
|
* |
|
133
|
|
|
* @param array $locations |
|
134
|
|
|
* |
|
135
|
|
|
* @return TemplateEngineInterface|self|$this |
|
136
|
|
|
*/ |
|
137
|
|
|
public function setLocations(array $locations) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->locations = $locations; |
|
140
|
|
|
return $this; |
|
141
|
4 |
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
4 |
|
* Returns the source template engine |
|
145
|
4 |
|
* |
|
146
|
3 |
|
* @return object |
|
147
|
2 |
|
*/ |
|
148
|
2 |
|
public function getSourceEngine() |
|
149
|
2 |
|
{ |
|
150
|
|
|
return $this->getTwigEnvironment(); |
|
151
|
1 |
|
} |
|
152
|
|
|
|
|
153
|
2 |
|
/** |
|
154
|
|
|
* Creates a twig environment if not injected |
|
155
|
|
|
* |
|
156
|
|
|
* @return Twig_Environment |
|
157
|
|
|
*/ |
|
158
|
|
|
private function getTwigEnvironment() |
|
159
|
|
|
{ |
|
160
|
|
|
if (null == $this->twigEnvironment) { |
|
161
|
|
|
$this->twigEnvironment = $this->createTwigEnvironment(); |
|
162
|
|
|
} |
|
163
|
4 |
|
return $this->twigEnvironment; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
4 |
|
/** |
|
167
|
2 |
|
* Creates the twig environment |
|
168
|
2 |
|
* |
|
169
|
2 |
|
* @return Twig_Environment |
|
170
|
2 |
|
*/ |
|
171
|
|
|
private function createTwigEnvironment() |
|
172
|
1 |
|
{ |
|
173
|
|
|
$twigEnv = new Twig_Environment( |
|
174
|
|
|
$this->getLoader(), |
|
175
|
|
|
$this->getOptions() |
|
176
|
|
|
); |
|
177
|
|
|
|
|
178
|
|
|
if ($this->options['debug']) { |
|
179
|
|
|
$twigEnv->addExtension(new Twig_Extension_Debug()); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $twigEnv; |
|
183
|
14 |
|
} |
|
184
|
|
|
|
|
185
|
14 |
|
/** |
|
186
|
14 |
|
* Creates a file system loader |
|
187
|
|
|
* |
|
188
|
|
|
* @return Twig_Loader_Filesystem |
|
189
|
|
|
*/ |
|
190
|
|
|
private function getLoader() |
|
191
|
|
|
{ |
|
192
|
|
|
if (null == $this->loader) { |
|
193
|
|
|
$this->loader = new Twig_Loader_Filesystem( |
|
194
|
|
|
$this->locations |
|
195
|
2 |
|
); |
|
196
|
|
|
} |
|
197
|
2 |
|
return $this->loader; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Returns current configured options |
|
202
|
|
|
* |
|
203
|
|
|
* @return array |
|
204
|
|
|
*/ |
|
205
|
6 |
|
private function getOptions() |
|
206
|
|
|
{ |
|
207
|
6 |
|
$options = []; |
|
208
|
2 |
|
foreach($this->optionsMap as $property => $name) { |
|
209
|
2 |
|
$options[$name] = $this->options[$property]; |
|
210
|
2 |
|
} |
|
211
|
1 |
|
return $options; |
|
212
|
2 |
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|