Skip to content

Commit 907ebb5

Browse files
committed
Fix timing of the addCIFunctions() call
1 parent 6feb61d commit 907ebb5

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

libraries/Twig.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class Twig
2727
'form_open', 'form_close', 'form_error', 'set_value', 'form_hidden'
2828
];
2929

30+
/**
31+
* @var bool Whether added CodeIgniter functions or not
32+
*/
33+
private $add_ci_functions = FALSE;
34+
3035
/**
3136
* @var Twig_Environment
3237
*/
@@ -82,7 +87,6 @@ public function createTwig()
8287
}
8388

8489
$this->twig = $twig;
85-
$this->addCIFunctions();
8690
}
8791

8892
public function setLoader($loader)
@@ -124,13 +128,22 @@ public function display($view, $params = [])
124128
public function render($view, $params = [])
125129
{
126130
$this->createTwig();
131+
// We call addCIFunctions() here, because we must call addCIFunctions()
132+
// after loading CodeIgniter functions in a controller.
133+
$this->addCIFunctions();
127134

128135
$view = $view . '.twig';
129136
return $this->twig->render($view, $params);
130137
}
131138

132139
private function addCIFunctions()
133140
{
141+
// Runs only once
142+
if ($this->add_ci_functions)
143+
{
144+
return;
145+
}
146+
134147
// as is functions
135148
foreach ($this->functions_asis as $function)
136149
{
@@ -171,6 +184,8 @@ private function addCIFunctions()
171184
)
172185
);
173186
}
187+
188+
$this->add_ci_functions = TRUE;
174189
}
175190

176191
/**

tests/libraries/TwigTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,36 @@ public function testAddGlobal()
4343
$output = $obj->render('global');
4444
$this->assertEquals('<title>Twig Test Site</title>'."\n", $output);
4545
}
46+
47+
public function testAddCIFunctionsRunsOnlyOnce()
48+
{
49+
$obj = new Twig(['paths' => __DIR__ . '/../templates/']);
50+
51+
$data = [
52+
'name' => 'CodeIgniter',
53+
];
54+
55+
$ref_obj = new ReflectionObject($obj);
56+
$ref_property = $ref_obj->getProperty('add_ci_functions');
57+
$ref_property->setAccessible(true);
58+
$add_ci_functions = $ref_property->getValue($obj);
59+
$this->assertEquals(false, $add_ci_functions);
60+
61+
$output = $obj->render('welcome', $data);
62+
63+
$ref_obj = new ReflectionObject($obj);
64+
$ref_property = $ref_obj->getProperty('add_ci_functions');
65+
$ref_property->setAccessible(true);
66+
$add_ci_functions = $ref_property->getValue($obj);
67+
$this->assertEquals(true, $add_ci_functions);
68+
69+
// Calls render() twice
70+
$output = $obj->render('welcome', $data);
71+
72+
$ref_obj = new ReflectionObject($obj);
73+
$ref_property = $ref_obj->getProperty('add_ci_functions');
74+
$ref_property->setAccessible(true);
75+
$add_ci_functions = $ref_property->getValue($obj);
76+
$this->assertEquals(true, $add_ci_functions);
77+
}
4678
}

0 commit comments

Comments
 (0)