-
Notifications
You must be signed in to change notification settings - Fork 14
/
Environment.php
executable file
·392 lines (353 loc) · 7.61 KB
/
Environment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/*
* This file is part of Spoon Library.
*
* (c) Davy Hellemans <[email protected]>
*
* For the full copyright and license information, please view the license
* file that was distributed with this source code.
*/
namespace Spoon\Template;
/**
* Used to hold all the environment specific options for templates.
*
* @author Davy Hellemans <[email protected]>
*/
class Environment
{
/**
* If enabled, variables will be automatically escaped.
*
* @var bool
*/
protected $autoEscape;
/**
* If enabled, cached templates will be automatically reloaded if the template changes.
*
* @var bool
*/
protected $autoReload;
/**
* Location where the cached templates will be stored.
*
* @var string
*/
protected $cache;
/**
* Charset that will be used internally.
*
* @var string
*/
protected $charset;
/**
* If enabled, debug info will be available in the templates.
*
* @var bool
*/
protected $debug;
/**
* List of all modifiers that apply in this environment.
*
* @var array
*/
protected $modifiers;
/**
* List of all tags and the class they are mapped to. These classes need to extend from
* the base Spoon\Template\Parser\Node class.
*
* @example array('if' => 'Spoon\Template\Parser\IfNode')
*
* @var array
*/
protected $tags;
/**
* @param array[optional] $options
*/
public function __construct(array $options = array())
{
// default configuration
$options = array_merge(array(
'auto_escape' => true,
'auto_reload' => true,
'cache' => '.',
'charset' => 'utf-8',
'debug' => false,
), $options);
// load configuration
$this->autoEscape = (bool) $options['auto_escape'];
$this->autoReload = (bool) $options['auto_reload'];
$this->cache = (string) $options['cache'];
$this->charset = (string) $options['charset'];
$this->debug = (bool) $options['debug'];
// load default modifiers
$this->modifiers = array(
'date' => 'date',
'dump' => 'var_dump',
'escape' => 'htmlspecialchars', // @todo should use the correct charset
'lowercase' => 'strtolower', // @todo what about mb_strtolower
'nl2br' => 'nl2br',
'repeat' => 'str_repeat',
'replace' => function($text, $search, $replace) { return str_replace($search, $replace, $text); },
'safe' => 'safe',
'shuffle' => 'str_shuffle',
'sprintf' => 'sprintf',
'substring' => 'substr',
'trim' => 'trim',
'uppercase' => 'strtoupper'
);
// load default tags
$this->tags = array(
'include' => 'Spoon\Template\Parser\IncludeNode',
'if' => 'Spoon\Template\Parser\IfNode',
'elseif' => 'Spoon\Template\Parser\ElseIfNode',
'else' => 'Spoon\Template\Parser\ElseNode',
'endif' => 'Spoon\Template\Parser\EndIfNode',
'for' => 'Spoon\Template\Parser\ForNode',
'endfor' => 'Spoon\Template\Parser\EndForNode',
'debug' => 'Spoon\Template\Parser\DebugNode'
);
}
/**
* Add a modifier with a callback.
*
* @param string $name
* @param mixed $value The value should be a valid callback (see http://php.net/manual/en/language.pseudo-types.php)
* @return \Spoon\Template\Environment
*/
public function addModifier($name, $value)
{
if(!preg_match('/^[a-z]+[a-z0-9_]*$/i', $name))
{
throw new Exception(sprintf('Modifier "%s" is not following the naming conventions', $name));
}
$this->modifiers[(string) $name] = $value;
return $this;
}
/**
* Add a custom tag linked to a class.
*
* @todo write some tests
*
* @param string $name
* @param string $class
* @return \Spoon\Template\Environment
*/
public function addTag($name, $class)
{
if(!preg_match('/^[a-z]+[a-z0-9_]*$/i', $name))
{
throw new Exception(sprintf('Tag "%s" is not following the naming conventions', $name));
}
// all nodes must extend from the basic one
if(!is_subclass_of($class, 'Spoon\Template\Parser\Node'))
{
throw new Exception(
sprintf('Custom tag classes need to extend from Spoon\Template\Parser\Node')
);
}
$this->tags[(string) $name] = $class;
return $this;
}
/**
* Disable auto escaping of variables.
*
* @return \Spoon\Template\Environment
*/
public function disableAutoEscape()
{
$this->autoEscape = false;
return $this;
}
/**
* Disable automatically reloading templates based on the modification date.
*
* @return \Spoon\Template\Environment
*/
public function disableAutoReload()
{
$this->autoReload = false;
return $this;
}
/**
* Disable debug mode.
*
* @return Spoon\Template\Environment
*/
public function disableDebug()
{
$this->debug = false;
return $this;
}
/**
* Enable auto escaping of variables.
*
* @return \Spoon\Template\Environment
*/
public function enableAutoEscape()
{
$this->autoEscape = true;
return $this;
}
/**
* Enable auto reload.
*
* @return \Spoon\Template\Environment
*/
public function enableAutoReload()
{
$this->autoReload = true;
return $this;
}
/**
* Enable debug mode.
*
* @return \Spoon\Template\Environment
*/
public function enableDebug()
{
$this->debug = true;
return $this;
}
/**
* Get cache location.
*
* @return string
*/
public function getCache()
{
return $this->cache;
}
/**
* Fetch the cache filename.
*
* @param string $filename The filename (including the path) you want to know the filename for.
* @return string
*/
public function getCacheFilename($filename)
{
return 'S' . md5(realpath($filename)) . '_' . basename($filename) . '.php';
}
/**
* Get charset.
*
* @return string
*/
public function getCharset()
{
return $this->charset;
}
/**
* Get a specific modifier callback.
*
* @param string $name The name of the extension.
* @return mixed
*/
public function getModifier($name)
{
if(!isset($this->modifiers[$name]))
{
throw new Exception(sprintf('There is no "%s" modifier.', (string) $name));
}
return $this->modifiers[$name];
}
/**
* Get all the modifiers.
*
* @return array
*/
public function getModifiers()
{
return $this->modifiers;
}
/**
* Fetch a list of all tags currently enabled for this environment.
*
* @return array
*/
public function getTags()
{
return $this->tags;
}
/**
* Is auto escaping enabled.
*
* @return bool
*/
public function isAutoEscape()
{
return $this->autoEscape;
}
/**
* Is auto reload enabled.
*
* @return bool
*/
public function isAutoReload()
{
return $this->autoReload;
}
/**
* Is the cached template modified.
*
* @param string $template The location of the cached template file.
* @param int $time The timestamp to compare with.
* @return bool
*/
public function isChanged($template, $time)
{
return (filemtime((string) $template) > (int) $time);
}
/**
* Is debug mode enabled.
*
* @return bool
*/
public function isDebug()
{
return $this->debug;
}
/**
* Remove a modifier from the list.
*
* @param string $name
* @return \Spoon\Template\Environment
*/
public function removeModifier($name)
{
unset($this->modifiers[$name]);
return $this;
}
/**
* Remove a tag from the list.
*
* @param string $name
* @return \Spoon\Template\Environment
*/
public function removeTag($name)
{
unset($this->tags[$name]);
return $this;
}
/**
* Set the template caching directory.
*
* @param string $cache The location where the cached templates should be stored.
* @return \Spoon\Template\Environment
*/
public function setCache($cache)
{
$this->cache = (string) $cache;
return $this;
}
/**
* Set the charset.
*
* @param string $value The default charset to use.
* @return \Spoon\Template\Environment
*/
public function setCharset($value)
{
$this->charset = (string) $value;
return $this;
}
}