/
SmartyEngine.php
525 lines (466 loc) · 16 KB
/
SmartyEngine.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<?php
/**
* This file is part of NoiseLabs-SmartyBundle
*
* NoiseLabs-SmartyBundle is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* NoiseLabs-SmartyBundle is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with NoiseLabs-SmartyBundle; if not, see
* <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2011-2012 Vítor Brandão
*
* @category NoiseLabs
* @package SmartyBundle
* @copyright (C) 2011-2012 Vítor Brandão <noisebleed@noiselabs.org>
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL-3
* @link http://www.noiselabs.org
*/
namespace NoiseLabs\Bundle\SmartyBundle;
use NoiseLabs\Bundle\SmartyBundle\Extension\ExtensionInterface;
use NoiseLabs\Bundle\SmartyBundle\Extension\Filter\FilterInterface;
use NoiseLabs\Bundle\SmartyBundle\Extension\Plugin\PluginInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Templating\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\Templating\TemplateNameParserInterface;
/**
* SmartyEngine is an engine able to render Smarty templates.
*
* This class is heavily inspired by \Twig_Environment.
* See {@link http://twig.sensiolabs.org/doc/api.html} for details about \Twig_Environment.
*
* Thanks to Symfony developer Christophe Coevoet (@stof) for a carefully code
* review of this bundle.
*
* @author Vítor Brandão <noisebleed@noiselabs.org>
*/
class SmartyEngine implements EngineInterface
{
protected $extensions;
protected $filters;
protected $globals;
protected $loader;
protected $parser;
protected $plugins;
protected $smarty;
/**
* Constructor.
*
* @param \Smarty $smarty A \Smarty instance
* @param ContainerInterface $container A ContainerInterface instance
* @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
* @param LoaderInterface $loader A LoaderInterface instance
* @param array $options An array of \Smarty properties
* @param GlobalVariables|null $globals A GlobalVariables instance or null
* @param LoggerInterface|null $logger A LoggerInterface instance or null
*/
public function __construct(\Smarty $smarty, ContainerInterface $container,
TemplateNameParserInterface $parser, LoaderInterface $loader, array $options,
GlobalVariables $globals = null, LoggerInterface $logger = null)
{
$this->smarty = $smarty;
$this->parser = $parser;
$this->loader = $loader;
$this->logger = $logger;
$this->globals = array();
// There are no default extensions.
$this->extensions = array();
/**
* @warning If you added template dirs to the Smarty instance prior to
* the loading of this engine these WILL BE LOST because the setter
* method setTemplateDir() is used below. Please use the following
* method instead:
* $container->get('templating.engine.smarty')->addTemplateDir(
* '/path/to/template_dir');
*/
foreach ($options as $property => $value) {
$this->smarty->{$this->smartyPropertyToSetter($property)}($value);
}
/**
* Register an handler for 'logical' filenames of the type:
* <code>file:AcmeHelloBundle:Default:layout.html.tpl</code>
*/
$this->smarty->default_template_handler_func = array($this, 'smartyDefaultTemplateHandler');
/**
* Define a set of template dirs to look for. This will allow the
* usage of the following syntax:
* <code>file:[WebkitBundle]/Default/layout.html.tpl</code>
*
* See {@link http://www.smarty.net/docs/en/resources.tpl} for details
*/
$bundlesTemplateDir = array();
foreach ($container->getParameter('kernel.bundles') as $bundle) {
$name = explode('\\', $bundle);
$name = end($name);
$reflection = new \ReflectionClass($bundle);
if (is_dir($dir = dirname($reflection->getFilename()).'/Resources/views')) {
$bundlesTemplateDir[$name] = $dir;
}
}
$this->smarty->addTemplateDir($bundlesTemplateDir);
if (null !== $globals) {
$this->addGlobal('app', $globals);
}
}
/**
* Pass methods not available in this engine to the Smarty instance.
*/
public function __call($name, $args)
{
return call_user_func_array(array($this->smarty, $name), $args);
}
/**
* Returns the Smarty instance.
*
* @return Smarty The Smarty instance
*/
public function getSmarty()
{
$this->registerFilters();
$this->registerPlugins();
$this->smarty->assign($this->getGlobals());
return $this->smarty;
}
/**
* Renders a template.
*
* @param mixed $name A template name
* @param array $parameters An array of parameters to pass to the template
*
* @return string The evaluated template as a string
*
* @throws \InvalidArgumentException if the template does not exist
* @throws \RuntimeException if the template cannot be rendered
*/
public function render($name, array $parameters = array())
{
$template = $this->load($name);
$this->registerFilters();
$this->registerPlugins();
// attach the global variables
$parameters = array_replace($this->getGlobals(), $parameters);
/**
* Assign variables/objects to the templates.
*
* Description
* void assign(mixed var);
* void assign(string varname, mixed var, bool nocache);
*
* You can explicitly pass name/value pairs, or associative arrays
* containing the name/value pairs.
*
* If you pass the optional third nocache parameter of TRUE, the
* variable is assigned as nocache variable. See {@link http://www.smarty.net/docs/en/caching.cacheable.tpl#cacheability.variables} for details.
*
* Too learn more see {@link http://www.smarty.net/docs/en/api.assign.tpl}
*/
$this->smarty->assign($parameters);
/**
* This returns the template output instead of displaying it. Supply a
* valid template resource type and path. As an optional second
* parameter, you can pass a $cache id, see the caching section for more
* information.
*
* As an optional third parameter, you can pass a $compile_id. This is
* in the event that you want to compile different versions of the same
* template, such as having separate templates compiled for different
* languages. You can also set the $compile_id variable once instead of
* passing this to each call to this function.
*
* Too learn more see {@link http://www.smarty.net/docs/en/api.fetch.tpl}
*/
return $this->smarty->fetch($template);
}
/**
* Returns true if the template exists.
*
* @param string $name A template name
*
* @return Boolean true if the template exists, false otherwise
*/
public function exists($name)
{
try {
$this->load($name);
} catch (\InvalidArgumentException $e) {
return false;
}
return true;
}
/**
* Returns true if this class is able to render the given template.
*
* @param string $name A template name
*
* @return Boolean True if this class supports the given resource, false otherwise
*/
public function supports($name)
{
if ($name instanceof \Smarty_Internal_Template) {
return true;
}
$template = $this->parser->parse($name);
// keep 'tpl' for backwards compatibility. remove when tagging '0.2.0'
return in_array($template->get('engine'), array('smarty', 'tpl'));
}
/**
* Renders a view and returns a Response.
*
* @param string $view The view name
* @param array $parameters An array of parameters to pass to the view
* @param Response $response A Response instance
*
* @return Response A Response instance
*/
public function renderResponse($view, array $parameters = array(), Response $response = null)
{
if (null === $response) {
$response = new Response();
}
$response->setContent($this->render($view, $parameters));
return $response;
}
/**
* Loads the given template.
*
* @param string $name A template name
*
* @return mixed The resource handle of the template file or template object
*
* @throws \InvalidArgumentException if the template cannot be found
*
* @todo Check windows filepaths as defined in
* {@link http://www.smarty.net/docs/en/resources.tpl#templates.windows.filepath}.
*/
public function load($name)
{
if ($name instanceof \Smarty_Internal_Template) {
return $name;
}
$template = $this->parser->parse($name);
$template = $this->loader->load($template);
if (false === $template) {
throw new \InvalidArgumentException(sprintf('The template "%s" does not exist.', $name));
}
return (string) $template;
}
/**
* Returns true if the given extension is registered.
*
* @param string $name The extension name
*
* @return Boolean Whether the extension is registered or not
*/
public function hasExtension($name)
{
return isset($this->extensions[$name]);
}
/**
* Gets an extension by name.
*
* @param string $name The extension name
*
* @return ExtensionInterface An ExtensionInterface instance
*/
public function getExtension($name)
{
if (!isset($this->extensions[$name])) {
throw new \InvalidArgumentException(sprintf('The "%s" extension is not enabled.', $name));
}
return $this->extensions[$name];
}
/**
* Registers an extension.
*
* @param ExtensionInterface $extension An ExtensionInterface instance
*/
public function addExtension(ExtensionInterface $extension)
{
$this->extensions[$extension->getName()] = $extension;
}
/**
* Removes an extension by name.
*
* @param string $name The extension name
*/
public function removeExtension($name)
{
unset($this->extensions[$name]);
}
/**
* Registers an array of extensions.
*
* @param array $extensions An array of extensions
*/
public function setExtensions(array $extensions)
{
$this->extensions = array();
foreach ($extensions as $extension) {
$this->addExtension($extension);
}
}
/**
* Returns all registered extensions.
*
* @return array An array of extensions
*/
public function getExtensions()
{
return $this->extensions;
}
/**
* Adds a filter to the collection.
*
* @param mixed $filter A FilterInterface instance
*/
public function addFilter(FilterInterface $filter)
{
if (null === $this->filters) {
$this->getFilters();
}
$this->filters[] = $filter;
}
/**
* Gets the collection of filters.
*
* @return array An array of Filters
*/
public function getFilters()
{
if (null === $this->filters) {
$this->filters = array();
foreach ($this->getExtensions() as $extension) {
$this->filters = array_merge($this->filters, $extension->getFilters());
}
}
return $this->filters;
}
/**
* Dynamically register filters to Smarty.
*/
public function registerFilters()
{
foreach ($this->getFilters() as $filter) {
try {
$this->smarty->registerFilter($filter->getType(), $filter->getCallback());
}
catch (\SmartyException $e) {
if (null !== $this->logger) {
$this->logger->warn(sprintf("SmartyException caught: %s.", $e->getMessage()));
}
}
}
}
/**
* Adds a plugin to the collection.
*
* @param mixed $plugin A PluginInterface instance
*/
public function addPlugin(PluginInterface $plugin)
{
if (null === $this->plugins) {
$this->getPlugins();
}
$this->plugins[] = $plugin;
}
/**
* Gets the collection of plugins, optionally filtered by an extension
* name.
*
* @return array An array of plugins
*/
public function getPlugins($extensionName = false)
{
if (null === $this->plugins) {
$this->plugins = array();
foreach ($this->getExtensions() as $extension) {
$this->plugins = array_merge($this->plugins, $extension->getPlugins());
}
}
// filter plugins that belong to $extension
if ($extensionName) {
$plugins = array();
foreach (array_keys($this->plugins) as $k) {
if ($extensionName == $this->plugins[$k]->getExtension()->getName()) {
$plugins[] = $this->plugins[$k];
}
}
return $plugins;
}
return $this->plugins;
}
/**
* Dynamically register plugins to Smarty.
*/
public function registerPlugins()
{
foreach ($this->getPlugins() as $plugin) {
try {
$this->smarty->registerPlugin($plugin->getType(), $plugin->getName(), $plugin->getCallback());
}
catch (\SmartyException $e) {
if (null !== $this->logger) {
$this->logger->warn(sprintf("SmartyException caught: %s.", $e->getMessage()));
}
}
}
}
/**
* Registers a Global.
*
* @param string $name The global name
* @param mixed $value The global value
*/
public function addGlobal($name, $value)
{
$this->globals[$name] = $value;
}
/**
* Gets the registered Globals.
*
* @return array An array of Globals
*/
public function getGlobals($load_extensions = true)
{
if (true === $load_extensions) {
foreach ($this->getExtensions() as $extension) {
$this->globals = array_merge($this->globals, $extension->getGlobals());
}
}
return $this->globals;
}
/**
* This method is called whenever Smarty fails to find a resource. We use
* this to load a 'real' template from a 'logical' one.
*
* To learn more see {@link http://www.smarty.net/docs/en/variable.default.template.handler.func.tpl}
*/
public function smartyDefaultTemplateHandler($type, $name, &$content, &$modified, \Smarty $smarty)
{
return ($type == 'file') ? (string) $this->load($name) : false;
}
/**
* Get the setter method for a Smarty class variable (property).
*/
protected function smartyPropertyToSetter($property)
{
$words = explode('_', strtolower($property));
$setter = 'set';
foreach ($words as $word) {
$setter .= ucfirst(trim($word));
}
return $setter;
}
}