Skip to content

Commit 27dc596

Browse files
authored
Merge pull request #2861 from Instrye/develop
fix. saveData not work
2 parents 6006d09 + 7f717ae commit 27dc596

8 files changed

Lines changed: 127 additions & 44 deletions

File tree

system/Common.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
*/
3939

4040
use Config\App;
41+
use Config\View;
4142
use Config\Logger;
4243
use Config\Database;
4344
use Config\Services;
@@ -1069,8 +1070,9 @@ function view(string $name, array $data = [], array $options = []): string
10691070
*/
10701071
$renderer = Services::renderer();
10711072

1072-
$saveData = true;
1073-
if (array_key_exists('saveData', $options) && $options['saveData'] === true)
1073+
$saveData = config(View::class)->saveData;
1074+
1075+
if (array_key_exists('saveData', $options))
10741076
{
10751077
$saveData = (bool) $options['saveData'];
10761078
unset($options['saveData']);

system/View/Parser.php

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ class Parser extends View
9393
/**
9494
* Constructor
9595
*
96-
* @param \Config\View $config
97-
* @param string $viewPath
98-
* @param mixed $loader
99-
* @param boolean $debug
100-
* @param LoggerInterface $logger
96+
* @param \Config\View $config
97+
* @param string $viewPath
98+
* @param mixed $loader
99+
* @param boolean $debug
100+
* @param LoggerInterface $logger
101101
*/
102102
public function __construct($config, string $viewPath = null, $loader = null, bool $debug = null, LoggerInterface $logger = null)
103103
{
@@ -157,20 +157,25 @@ public function render(string $view, array $options = null, bool $saveData = nul
157157
throw ViewException::forInvalidFile($file);
158158
}
159159

160+
if (is_null($this->tempData))
161+
{
162+
$this->tempData = $this->data;
163+
}
164+
160165
$template = file_get_contents($file);
161-
$output = $this->parse($template, $this->data, $options);
166+
$output = $this->parse($template, $this->tempData, $options);
162167
$this->logPerformance($start, microtime(true), $view);
163168

164-
if (! $saveData)
169+
if ($saveData)
165170
{
166-
$this->data = [];
171+
$this->data = $this->tempData;
167172
}
168173
// Should we cache?
169174
if (isset($options['cache']))
170175
{
171176
cache()->save($cacheName, $output, (int) $options['cache']);
172177
}
173-
178+
$this->tempData = null;
174179
return $output;
175180
}
176181

@@ -196,14 +201,22 @@ public function renderString(string $template, array $options = null, bool $save
196201
$saveData = $this->config->saveData;
197202
}
198203

199-
$output = $this->parse($template, $this->data, $options);
204+
if (is_null($this->tempData))
205+
{
206+
$this->tempData = $this->data;
207+
}
208+
209+
$output = $this->parse($template, $this->tempData, $options);
200210

201211
$this->logPerformance($start, microtime(true), $this->excerpt($template));
202212

203-
if (! $saveData)
213+
if ($saveData)
204214
{
205-
$this->data = [];
215+
$this->data = $this->tempData;
206216
}
217+
218+
$this->tempData = null;
219+
207220
return $output;
208221
}
209222

@@ -243,7 +256,8 @@ public function setData(array $data = [], string $context = null): RendererInter
243256
}
244257
}
245258

246-
$this->data = array_merge($this->data, $data);
259+
$this->tempData = $this->tempData ?? $this->data;
260+
$this->tempData = array_merge($this->tempData, $data);
247261

248262
return $this;
249263
}
@@ -532,7 +546,14 @@ protected function parseConditionals(string $template): string
532546

533547
// Parse the PHP itself, or insert an error so they can debug
534548
ob_start();
535-
extract($this->data);
549+
550+
if (is_null($this->tempData))
551+
{
552+
$this->tempData = $this->data;
553+
}
554+
555+
extract($this->tempData);
556+
536557
try
537558
{
538559
eval('?>' . $template . '<?php ');
@@ -542,6 +563,7 @@ protected function parseConditionals(string $template): string
542563
ob_end_clean();
543564
throw ViewException::forTagSyntaxError(str_replace(['?>', '<?php '], '', $template));
544565
}
566+
545567
return ob_get_clean();
546568
}
547569

system/View/View.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ class View implements RendererInterface
5858
*/
5959
protected $data = [];
6060

61+
/**
62+
* Merge savedData and userData
63+
*/
64+
protected $tempData = null;
65+
6166
/**
6267
* The base directory to look in for our Views.
6368
*
@@ -189,11 +194,10 @@ public function render(string $view, array $options = null, bool $saveData = nul
189194
// Store the results here so even if
190195
// multiple views are called in a view, it won't
191196
// clean it unless we mean it to.
192-
if ($saveData !== null)
197+
if (is_null($saveData))
193198
{
194-
$this->saveData = $saveData;
199+
$saveData = $this->saveData;
195200
}
196-
197201
$fileExt = pathinfo($view, PATHINFO_EXTENSION);
198202
$realPath = empty($fileExt) ? $view . '.php' : $view; // allow Views as .html, .tpl, etc (from CI3)
199203
$this->renderVars['view'] = $realPath;
@@ -225,11 +229,17 @@ public function render(string $view, array $options = null, bool $saveData = nul
225229
}
226230

227231
// Make our view data available to the view.
228-
extract($this->data);
229232

230-
if (! $this->saveData)
233+
if (is_null($this->tempData))
231234
{
232-
$this->data = [];
235+
$this->tempData = $this->data;
236+
}
237+
238+
extract($this->tempData);
239+
240+
if ($saveData)
241+
{
242+
$this->data = $this->tempData;
233243
}
234244

235245
ob_start();
@@ -277,6 +287,8 @@ public function render(string $view, array $options = null, bool $saveData = nul
277287
cache()->save($this->renderVars['cacheName'], $output, (int) $this->renderVars['options']['cache']);
278288
}
279289

290+
$this->tempData = null;
291+
280292
return $output;
281293
}
282294

@@ -300,16 +312,22 @@ public function render(string $view, array $options = null, bool $saveData = nul
300312
public function renderString(string $view, array $options = null, bool $saveData = null): string
301313
{
302314
$start = microtime(true);
315+
303316
if (is_null($saveData))
304317
{
305-
$saveData = $this->config->saveData;
318+
$saveData = $this->saveData;
306319
}
307320

308-
extract($this->data);
321+
if (is_null($this->tempData))
322+
{
323+
$this->tempData = $this->data;
324+
}
325+
326+
extract($this->tempData);
309327

310-
if (! $saveData)
328+
if ($saveData)
311329
{
312-
$this->data = [];
330+
$this->data = $this->tempData;
313331
}
314332

315333
ob_start();
@@ -320,6 +338,8 @@ public function renderString(string $view, array $options = null, bool $saveData
320338

321339
$this->logPerformance($start, microtime(true), $this->excerpt($view));
322340

341+
$this->tempData = null;
342+
323343
return $output;
324344
}
325345

@@ -355,7 +375,8 @@ public function setData(array $data = [], string $context = null): RendererInter
355375
$data = \esc($data, $context);
356376
}
357377

358-
$this->data = array_merge($this->data, $data);
378+
$this->tempData = $this->tempData ?? $this->data;
379+
$this->tempData = array_merge($this->tempData, $data);
359380

360381
return $this;
361382
}
@@ -379,7 +400,8 @@ public function setVar(string $name, $value = null, string $context = null): Ren
379400
$value = \esc($value, $context);
380401
}
381402

382-
$this->data[$name] = $value;
403+
$this->tempData = $this->tempData ?? $this->data;
404+
$this->tempData[$name] = $value;
383405

384406
return $this;
385407
}
@@ -407,7 +429,7 @@ public function resetData(): RendererInterface
407429
*/
408430
public function getData(): array
409431
{
410-
return $this->data;
432+
return is_null($this->tempData) ? $this->data : $this->tempData;
411433
}
412434

413435
//--------------------------------------------------------------------
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1><?= $testString ?? 'is_not' ?></h1>

tests/system/CommonFunctionsTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class CommonFunctionsTest extends \CodeIgniter\Test\CIUnitTestCase
2525
protected function setUp(): void
2626
{
2727
parent::setUp();
28-
28+
$renderer = Services::renderer();
29+
$renderer->resetData();
2930
unset($_ENV['foo'], $_SERVER['foo']);
3031
}
3132

@@ -429,4 +430,14 @@ public function testTrace()
429430
$this->assertStringContainsString('Debug Backtrace', $content);
430431
}
431432

433+
public function testViewNotSaveData()
434+
{
435+
$data = [
436+
'testString' => 'bar',
437+
'bar' => 'baz',
438+
];
439+
$this->assertStringContainsString('<h1>bar</h1>', view('\Tests\Support\View\Views\simples', $data, ['saveData' => false]));
440+
$this->assertStringContainsString('<h1>is_not</h1>', view('\Tests\Support\View\Views\simples'));
441+
}
442+
432443
}

tests/system/View/ParserTest.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -940,27 +940,31 @@ public function testRenderCantFindView()
940940

941941
public function testRenderSavingData()
942942
{
943-
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
943+
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
944+
$expected = "<h1>Hello World</h1>\n";
945+
944946
$parser->setData(['testString' => 'Hello World']);
947+
$this->assertEquals($expected, $parser->render('Simpler', [], false));
948+
$this->assertArrayNotHasKey('testString', $parser->getData());
945949

946-
$expected = "<h1>Hello World</h1>\n";
950+
$parser->setData(['testString' => 'Hello World']);
947951
$this->assertEquals($expected, $parser->render('Simpler', [], true));
948952
$this->assertArrayHasKey('testString', $parser->getData());
949-
$this->assertEquals($expected, $parser->render('Simpler', [], false));
950-
$this->assertArrayNotHasKey('testString', $parser->getData());
951953
}
952954

953955
public function testRenderStringSavingData()
954956
{
955-
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
956-
$parser->setData(['testString' => 'Hello World']);
957-
957+
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
958958
$expected = '<h1>Hello World</h1>';
959959
$pattern = '<h1>{testString}</h1>';
960-
$this->assertEquals($expected, $parser->renderString($pattern, [], true));
961-
$this->assertArrayHasKey('testString', $parser->getData());
960+
961+
$parser->setData(['testString' => 'Hello World']);
962962
$this->assertEquals($expected, $parser->renderString($pattern, [], false));
963963
$this->assertArrayNotHasKey('testString', $parser->getData());
964+
//last set data is not saved
965+
$parser->setData(['testString' => 'Hello World']);
966+
$this->assertEquals($expected, $parser->renderString($pattern, [], true));
967+
$this->assertArrayHasKey('testString', $parser->getData());
964968
}
965969

966970
public function testRenderFindsOtherView()

tests/system/View/ViewTest.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,17 @@ public function testCachedRender()
230230

231231
public function testRenderStringSavingData()
232232
{
233-
$view = new View($this->config, $this->viewsDir, $this->loader);
233+
$view = new View($this->config, $this->viewsDir, $this->loader);
234+
$expected = '<h1>Hello World</h1>';
234235

236+
//I think saveData is sava current data, is not clean already set data.
235237
$view->setVar('testString', 'Hello World');
236-
$expected = '<h1>Hello World</h1>';
237-
$this->assertEquals($expected, $view->renderString('<h1><?= $testString ?></h1>', [], true));
238-
$this->assertArrayHasKey('testString', $view->getData());
239238
$this->assertEquals($expected, $view->renderString('<h1><?= $testString ?></h1>', [], false));
240239
$this->assertArrayNotHasKey('testString', $view->getData());
240+
241+
$view->setVar('testString', 'Hello World');
242+
$this->assertEquals($expected, $view->renderString('<h1><?= $testString ?></h1>', [], true));
243+
$this->assertArrayHasKey('testString', $view->getData());
241244
}
242245

243246
//--------------------------------------------------------------------
@@ -343,4 +346,22 @@ public function testRenderLayoutNoContentSection()
343346
$this->assertStringContainsString($expected, $view->render('apples'));
344347
}
345348

349+
public function testRenderSaveDataCover()
350+
{
351+
$view = new View($this->config, $this->viewsDir, $this->loader);
352+
$this->setPrivateProperty($view, 'saveData', true);
353+
$view->setVar('testString', 'test');
354+
$view->render('simple', null, false);
355+
$this->assertEquals(true, $this->getPrivateProperty($view, 'saveData'));
356+
}
357+
358+
public function testRenderSaveDataUseAflterSaveDataFalse()
359+
{
360+
$view = new View($this->config, $this->viewsDir, $this->loader);
361+
$view->setVar('testString', 'test');
362+
$view->render('simple', null, true);
363+
$view->render('simple', null, false);
364+
$this->assertStringContainsString('<h1>test</h1>', $view->render('simple', null, false));
365+
}
366+
346367
}

tests/system/View/Views/simple.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<h1><?= $testString ?></h1>
1+
<h1><?= $testString ?></h1>

0 commit comments

Comments
 (0)