Skip to content

Commit 0a7559e

Browse files
committed
Fix escaping of extensions, add extra filters for standard PHP functions
1 parent 7054c6b commit 0a7559e

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

src/Builder/BaseBuilder.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ abstract class BaseBuilder implements BuilderInterface
4949
* @var array
5050
*/
5151
protected $twigFilters = array(
52-
'addslashes',
53-
'var_export',
54-
'is_numeric',
55-
'ucfirst',
56-
'substr',
5752
);
5853

5954
/**
@@ -67,6 +62,7 @@ abstract class BaseBuilder implements BuilderInterface
6762
protected $twigExtensions = array(
6863
'\\TwigGenerator\\Extension\\PHPPrintExtension',
6964
'\\TwigGenerator\\Extension\\TwigPrintExtension',
65+
'\\TwigGenerator\\Extension\\ExtraFilterExtension',
7066
);
7167

7268
/**
Lines changed: 26 additions & 0 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 23 additions & 22 deletions
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

0 commit comments

Comments
 (0)