Skip to content

Commit 88e73cb

Browse files
committed
fix. saveData not work
1 parent 05e1734 commit 88e73cb

4 files changed

Lines changed: 77 additions & 22 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/View.php

Lines changed: 33 additions & 16 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 ($saveData === null)
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))
234+
{
235+
$this->tempData = $this->data;
236+
}
237+
238+
extract($this->tempData);
239+
240+
if ($saveData)
231241
{
232-
$this->data = [];
242+
$this->data = $this->tempData;
233243
}
234244

235245
ob_start();
@@ -276,7 +286,7 @@ public function render(string $view, array $options = null, bool $saveData = nul
276286
{
277287
cache()->save($this->renderVars['cacheName'], $output, (int) $this->renderVars['options']['cache']);
278288
}
279-
289+
$this->tempData = null;
280290
return $output;
281291
}
282292

@@ -300,16 +310,22 @@ public function render(string $view, array $options = null, bool $saveData = nul
300310
public function renderString(string $view, array $options = null, bool $saveData = null): string
301311
{
302312
$start = microtime(true);
313+
303314
if (is_null($saveData))
304315
{
305-
$saveData = $this->config->saveData;
316+
$saveData = $this->saveData;
306317
}
307318

308-
extract($this->data);
319+
if(is_null($this->tempData))
320+
{
321+
$this->tempData = $this->data;
322+
}
309323

310-
if (! $saveData)
324+
extract($this->tempData);
325+
326+
if ($saveData)
311327
{
312-
$this->data = [];
328+
$this->data = $this->tempData;
313329
}
314330

315331
ob_start();
@@ -319,7 +335,7 @@ public function renderString(string $view, array $options = null, bool $saveData
319335
@ob_end_clean();
320336

321337
$this->logPerformance($start, microtime(true), $this->excerpt($view));
322-
338+
$this->tempData = null;
323339
return $output;
324340
}
325341

@@ -354,8 +370,8 @@ public function setData(array $data = [], string $context = null): RendererInter
354370
{
355371
$data = \esc($data, $context);
356372
}
357-
358-
$this->data = array_merge($this->data, $data);
373+
$this->tempData = $this->tempData ?? $this->data;
374+
$this->tempData = array_merge($this->tempData, $data);
359375

360376
return $this;
361377
}
@@ -379,7 +395,8 @@ public function setVar(string $name, $value = null, string $context = null): Ren
379395
$value = \esc($value, $context);
380396
}
381397

382-
$this->data[$name] = $value;
398+
$this->tempData = $this->tempData ?? $this->data;
399+
$this->tempData[$name] = $value;
383400

384401
return $this;
385402
}
@@ -407,7 +424,7 @@ public function resetData(): RendererInterface
407424
*/
408425
public function getData(): array
409426
{
410-
return $this->data;
427+
return $this->tempData ?? $this->data;
411428
}
412429

413430
//--------------------------------------------------------------------

tests/system/CommonFunctionsTest.php

Lines changed: 16 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,18 @@ public function testTrace()
429430
$this->assertStringContainsString('Debug Backtrace', $content);
430431
}
431432

433+
public function testViewNotSaveData()
434+
{
435+
436+
$data = [
437+
'testString' => 'bar',
438+
'bar' => 'baz',
439+
];
440+
view('\Tests\Support\View\Views\simple', $data, ['saveData' => false]);
441+
ob_get_clean();
442+
$this->expectException('Exception');
443+
view('\Tests\Support\View\Views\simple');
444+
ob_get_clean();
445+
}
446+
432447
}

tests/system/View/ViewTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,16 @@ public function testCachedRender()
231231
public function testRenderStringSavingData()
232232
{
233233
$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->assertEquals('test', $view->render('simple', null, false));
365+
}
366+
346367
}

0 commit comments

Comments
 (0)