Skip to content

Commit 6bdb7f8

Browse files
Merge pull request #6 from tobias-93/master
Fix escaping of extensions, add extra filters for standard PHP functions
2 parents 5834ba9 + cd12b4f commit 6bdb7f8

File tree

5 files changed

+71
-39
lines changed

5 files changed

+71
-39
lines changed

src/Builder/BaseBuilder.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
namespace TwigGenerator\Builder;
1212

13+
use Twig\Environment;
14+
use Twig\Loader\FilesystemLoader;
15+
use Twig\TwigFilter;
16+
1317
/**
1418
* @author Cédric Lombardot
1519
*/
@@ -49,11 +53,6 @@ abstract class BaseBuilder implements BuilderInterface
4953
* @var array
5054
*/
5155
protected $twigFilters = array(
52-
'addslashes',
53-
'var_export',
54-
'is_numeric',
55-
'ucfirst',
56-
'substr',
5756
);
5857

5958
/**
@@ -67,6 +66,7 @@ abstract class BaseBuilder implements BuilderInterface
6766
protected $twigExtensions = array(
6867
'\\TwigGenerator\\Extension\\PHPPrintExtension',
6968
'\\TwigGenerator\\Extension\\TwigPrintExtension',
69+
'\\TwigGenerator\\Extension\\ExtraFilterExtension',
7070
);
7171

7272
/**
@@ -302,12 +302,12 @@ public function addTwigExtensions(array $extensions)
302302
* Initialize the Twig Environment which automatically loads
303303
* extensions and filters.
304304
*
305-
* @return \Twig_Environment
305+
* @return Environment
306306
*/
307307
protected function getTwigEnvironment()
308308
{
309-
$loader = new \Twig_Loader_Filesystem($this->getTemplateDirs());
310-
$twig = new \Twig_Environment($loader, array(
309+
$loader = new FilesystemLoader($this->getTemplateDirs());
310+
$twig = new Environment($loader, array(
311311
'autoescape' => false,
312312
'strict_variables' => true,
313313
'debug' => true,
@@ -320,7 +320,7 @@ protected function getTwigEnvironment()
320320
return $twig;
321321
}
322322

323-
protected function loadTwigFilters(\Twig_Environment $twig)
323+
protected function loadTwigFilters(Environment $twig)
324324
{
325325
foreach ($this->twigFilters as $twigFilter) {
326326
if (is_object($twigFilter)) {
@@ -331,11 +331,11 @@ protected function loadTwigFilters(\Twig_Environment $twig)
331331
} else {
332332
$twigFilterName = $twigFilter;
333333
}
334-
$twig->addFilter(new \Twig_SimpleFilter($twigFilterName, $twigFilter));
334+
$twig->addFilter(new TwigFilter($twigFilterName, $twigFilter));
335335
}
336336
}
337337

338-
protected function loadTwigExtensions(\Twig_Environment $twig)
338+
protected function loadTwigExtensions(Environment $twig)
339339
{
340340
foreach ($this->twigExtensions as $twigExtensionName) {
341341
if (is_object($twigExtensionName)) {
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
4+
namespace TwigGenerator\Extension;
5+
6+
7+
use Twig\Extension\AbstractExtension;
8+
use Twig\TwigFilter;
9+
10+
class ExtraFilterExtension extends AbstractExtension
11+
{
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
public function getFilters()
16+
{
17+
$options = ['is_safe' => ['html']];
18+
return array(
19+
'addslashes' => new TwigFilter('addslashes', 'addslashes', $options),
20+
'var_export' => new TwigFilter('var_export', 'var_export', $options),
21+
'is_numeric' => new TwigFilter('is_numeric', 'is_numeric', $options),
22+
'ucfirst' => new TwigFilter('ucfirst', 'ucfirst', $options),
23+
'substr' => new TwigFilter('substr', 'substr', $options),
24+
);
25+
}
26+
}

src/Extension/PHPPrintExtension.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ class PHPPrintExtension extends AbstractExtension
1717
*/
1818
public function getFilters()
1919
{
20+
$options = ['is_safe' => ['html']];
2021
return array(
21-
'as_php' => new TwigFilter('as_php' , array($this, 'asPhp')),
22-
'php_name' => new TwigFilter('php_name', array($this, 'phpName')),
22+
'as_php' => new TwigFilter('as_php' , array($this, 'asPhp'), $options),
23+
'php_name' => new TwigFilter('php_name', array($this, 'phpName'), $options),
2324
);
2425
}
2526

src/Extension/TwigPrintExtension.php

+23-22
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,30 @@ class TwigPrintExtension extends AbstractExtension
2222
*/
2323
public function getFunctions()
2424
{
25+
$options = ['is_safe' => ['html']];
2526
return array(
26-
'echo_twig' => new TwigFunction('echo_twig' , array($this, 'getEchoTwig')),
27-
'echo_block' => new TwigFunction('echo_block' , array($this, 'getEchoBlock')),
28-
'echo_endblock' => new TwigFunction('echo_endblock' , array($this, 'getEchoEndBlock')),
29-
'echo_for' => new TwigFunction('echo_for' , array($this, 'getEchoFor')),
30-
'echo_endfor' => new TwigFunction('echo_endfor' , array($this, 'getEchoEndFor')),
31-
'echo_raw' => new TwigFunction('echo_raw' , array($this, 'getEchoRaw')),
32-
'echo_endraw' => new TwigFunction('echo_endraw' , array($this, 'getEchoEndRaw')),
33-
'echo_spaceless' => new TwigFunction('echo_spaceless' , array($this, 'getEchoSpaceless')),
34-
'echo_endspaceless' => new TwigFunction('echo_endspaceless', array($this, 'getEchoEndSpaceless')),
35-
'echo_extends' => new TwigFunction('echo_extends' , array($this, 'getEchoExtends')),
36-
'echo_if' => new TwigFunction('echo_if' , array($this, 'getEchoIf')),
37-
'echo_else' => new TwigFunction('echo_else' , array($this, 'getEchoElse')),
38-
'echo_elseif' => new TwigFunction('echo_elseif' , array($this, 'getEchoElseIf')),
39-
'echo_endif' => new TwigFunction('echo_endif' , array($this, 'getEchoEndIf')),
40-
'echo_set' => new TwigFunction('echo_set' , array($this, 'getEchoSet')),
41-
'echo_twig_arr' => new TwigFunction('echo_twig_arr' , array($this, 'getEchoTwigArr')),
42-
'echo_twig_assoc' => new TwigFunction('echo_twig_assoc' , array($this, 'getEchoTwigAssoc')),
43-
'echo_twig_filter' => new TwigFunction('echo_twig_filter' , array($this, 'getEchoTwigFilter')),
44-
'echo_include' => new TwigFunction('echo_include' , array($this, 'getEchoInclude')),
45-
'echo_use' => new TwigFunction('echo_use' , array($this, 'getEchoUse')),
46-
'echo_print_block' => new TwigFunction('echo_print_block' , array($this, 'getEchoPrintBlock')),
47-
'char' => new TwigFunction('char' , array($this, 'char')),
27+
'echo_twig' => new TwigFunction('echo_twig' , array($this, 'getEchoTwig'), $options),
28+
'echo_block' => new TwigFunction('echo_block' , array($this, 'getEchoBlock'), $options),
29+
'echo_endblock' => new TwigFunction('echo_endblock' , array($this, 'getEchoEndBlock'), $options),
30+
'echo_for' => new TwigFunction('echo_for' , array($this, 'getEchoFor'), $options),
31+
'echo_endfor' => new TwigFunction('echo_endfor' , array($this, 'getEchoEndFor'), $options),
32+
'echo_raw' => new TwigFunction('echo_raw' , array($this, 'getEchoRaw'), $options),
33+
'echo_endraw' => new TwigFunction('echo_endraw' , array($this, 'getEchoEndRaw'), $options),
34+
'echo_spaceless' => new TwigFunction('echo_spaceless' , array($this, 'getEchoSpaceless'), $options),
35+
'echo_endspaceless' => new TwigFunction('echo_endspaceless', array($this, 'getEchoEndSpaceless'), $options),
36+
'echo_extends' => new TwigFunction('echo_extends' , array($this, 'getEchoExtends'), $options),
37+
'echo_if' => new TwigFunction('echo_if' , array($this, 'getEchoIf'), $options),
38+
'echo_else' => new TwigFunction('echo_else' , array($this, 'getEchoElse'), $options),
39+
'echo_elseif' => new TwigFunction('echo_elseif' , array($this, 'getEchoElseIf'), $options),
40+
'echo_endif' => new TwigFunction('echo_endif' , array($this, 'getEchoEndIf'), $options),
41+
'echo_set' => new TwigFunction('echo_set' , array($this, 'getEchoSet'), $options),
42+
'echo_twig_arr' => new TwigFunction('echo_twig_arr' , array($this, 'getEchoTwigArr'), $options),
43+
'echo_twig_assoc' => new TwigFunction('echo_twig_assoc' , array($this, 'getEchoTwigAssoc'), $options),
44+
'echo_twig_filter' => new TwigFunction('echo_twig_filter' , array($this, 'getEchoTwigFilter'), $options),
45+
'echo_include' => new TwigFunction('echo_include' , array($this, 'getEchoInclude'), $options),
46+
'echo_use' => new TwigFunction('echo_use' , array($this, 'getEchoUse'), $options),
47+
'echo_print_block' => new TwigFunction('echo_print_block' , array($this, 'getEchoPrintBlock'), $options),
48+
'char' => new TwigFunction('char' , array($this, 'char'), $options),
4849
);
4950
}
5051

tests/TwigGenerator/Tests/Extension/BaseExtensionTest.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php
22
namespace TwigGenerator\Tests\Extension;
33

4+
use Twig\Environment;
5+
use Twig\Extension\AbstractExtension;
6+
use Twig\Loader\ArrayLoader;
7+
48
/**
59
*
610
* Base class to test extensions. Provide builtin functions to initialize
@@ -20,12 +24,12 @@ abstract class BaseExtensionTest extends \PHPUnit_Framework_TestCase
2024
protected $twigVariables = array();
2125

2226
/**
23-
* @var \Twig_Extension
27+
* @var AbstractExtension
2428
*/
2529
protected $extension;
2630

2731
/**
28-
* @return \Twig_Extension
32+
* @return AbstractExtension
2933
*/
3034
abstract protected function getTestedExtension();
3135

@@ -63,8 +67,8 @@ protected function runTwigTests(array $templates, array $returns)
6367

6468
protected function getEnvironment($templates, $options = array())
6569
{
66-
$twig = new \Twig_Environment(
67-
new \Twig_Loader_Array($templates),
70+
$twig = new Environment(
71+
new ArrayLoader($templates),
6872
array_merge(
6973
array(
7074
'debug' => true,

0 commit comments

Comments
 (0)