Skip to content

Commit 7e03516

Browse files
authored
Merge pull request #66 from kenjis/feat-override-CustomizedFunctions
feat: can override customized functions
2 parents e2d85f6 + 6549b70 commit 7e03516

File tree

6 files changed

+66
-32
lines changed

6 files changed

+66
-32
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
- PHP 7.4 or later.
99
- Twig 3.4.3 or later.
1010
- CodeIgniter 4.2.11 or later.
11+
12+
### Added
13+
1114
- Add functionality to add your filters. See [README](README.md#adding-your-functions--filters).
15+
- Add functionality to override functions that are customized by *CodeIgniter Simple and Secure Twig*.
1216

1317
## v4.1.0 (2022-09-20)
1418

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,16 @@ $twig = $this->twig->getTwig();
7979
* `form_error()`
8080
* `form_hidden()`
8181
* `set_value()`
82+
* `csrf_field()`
83+
* `validation_list_errors()`
8284

8385
Some helpers are added the functionality of auto-escaping for security.
8486

87+
> **Warning**
88+
> `validation_list_errors()` shows Validation Errors by `Services::validation()->listErrors()`,
89+
> and if you use user input for Validation Error messages, attackers may do XSS.
90+
> In such a case, validate user input and escape it by yourself.
91+
8592
### Adding Your Functions & Filters
8693

8794
You can add your functions and filters with configuration:
@@ -95,7 +102,9 @@ $config = [
95102
$this->twig = new \Kenjis\CI4Twig\Twig($config);
96103
~~~
97104

98-
If your function explicitly outputs HTML code, you will want the raw output to be printed. In such a case, use `functions_safe`, and **you have to make sure the output of the function is XSS free**.
105+
If your function explicitly outputs HTML code, you want the raw output to be printed.
106+
In such a case, use `functions_safe`, and **you have to make sure the output of
107+
the function is XSS free**.
99108

100109
### References
101110

src/CI4Twig/Twig.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,16 @@ protected function addFunctions()
253253
}
254254

255255
// customized functions
256-
if (function_exists('anchor')) {
256+
$this->addCustomizedFunctions();
257+
258+
$this->functions_added = true;
259+
}
260+
261+
protected function addCustomizedFunctions()
262+
{
263+
$functions = array_merge($this->functions_asis, $this->functions_safe);
264+
265+
if (! in_array('anchor', $functions, true) && function_exists('anchor')) {
257266
$this->twig->addFunction(
258267
new TwigFunction(
259268
'anchor',
@@ -263,15 +272,15 @@ protected function addFunctions()
263272
);
264273
}
265274

266-
$this->twig->addFunction(
267-
new TwigFunction(
268-
'validation_list_errors',
269-
[$this, 'validation_list_errors'],
270-
['is_safe' => ['html']]
271-
)
272-
);
273-
274-
$this->functions_added = true;
275+
if (! in_array('validation_list_errors', $functions, true)) {
276+
$this->twig->addFunction(
277+
new TwigFunction(
278+
'validation_list_errors',
279+
[$this, 'validation_list_errors'],
280+
['is_safe' => ['html']]
281+
)
282+
);
283+
}
275284
}
276285

277286
/**

tests/CI4Twig/TwigTest.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,41 +85,47 @@ public function testAddFunctionsRunsOnlyOnce()
8585

8686
public function testFunctionAsIs()
8787
{
88-
$obj = new Twig(
89-
[
90-
'paths' => __DIR__ . '/../templates/',
91-
'functions' => ['md5'],
92-
'cache' => false,
93-
]
94-
);
88+
$obj = new Twig([
89+
'paths' => __DIR__ . '/../templates/',
90+
'functions' => ['md5'],
91+
'cache' => false,
92+
]);
9593

9694
$output = $obj->render('functions_asis');
9795
$this->assertSame("900150983cd24fb0d6963f7d28e17f72\n", $output);
9896
}
9997

10098
public function testFunctionSafe()
10199
{
102-
$obj = new Twig(
103-
[
104-
'paths' => __DIR__ . '/../templates/',
105-
'functions_safe' => ['test_safe'],
106-
'cache' => false,
107-
]
108-
);
100+
$obj = new Twig([
101+
'paths' => __DIR__ . '/../templates/',
102+
'functions_safe' => ['test_safe'],
103+
'cache' => false,
104+
]);
109105

110106
$output = $obj->render('functions_safe');
111107
$this->assertSame("<s>test</s>\n", $output);
112108
}
113109

110+
public function testFunctionCustomized()
111+
{
112+
$obj = new Twig([
113+
'paths' => __DIR__ . '/../templates/',
114+
'functions' => ['validation_list_errors'],
115+
'cache' => false,
116+
]);
117+
118+
$output = $obj->render('functions_customized_override');
119+
$this->assertSame("override\n", $output);
120+
}
121+
114122
public function testFilter()
115123
{
116-
$obj = new Twig(
117-
[
118-
'paths' => __DIR__ . '/../templates/',
119-
'filters' => ['str_rot13'],
120-
'cache' => false,
121-
]
122-
);
124+
$obj = new Twig([
125+
'paths' => __DIR__ . '/../templates/',
126+
'filters' => ['str_rot13'],
127+
'cache' => false,
128+
]);
123129

124130
$output = $obj->render('filters');
125131
$this->assertSame("PbqrVtavgre Fvzcyr naq Frpher Gjvt\n", $output);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ validation_list_errors() }}

tests/twig_functions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ function test_safe(): string
44
{
55
return '<s>test</s>';
66
}
7+
8+
function validation_list_errors(): string
9+
{
10+
return 'override';
11+
}

0 commit comments

Comments
 (0)