Skip to content

Commit

Permalink
Merge pull request #2861 from Instrye/develop
Browse files Browse the repository at this point in the history
fix. saveData not work
  • Loading branch information
lonnieezell authored Apr 22, 2020
2 parents 6006d09 + 7f717ae commit 27dc596
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 44 deletions.
6 changes: 4 additions & 2 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
*/

use Config\App;
use Config\View;
use Config\Logger;
use Config\Database;
use Config\Services;
Expand Down Expand Up @@ -1069,8 +1070,9 @@ function view(string $name, array $data = [], array $options = []): string
*/
$renderer = Services::renderer();

$saveData = true;
if (array_key_exists('saveData', $options) && $options['saveData'] === true)
$saveData = config(View::class)->saveData;

if (array_key_exists('saveData', $options))
{
$saveData = (bool) $options['saveData'];
unset($options['saveData']);
Expand Down
50 changes: 36 additions & 14 deletions system/View/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ class Parser extends View
/**
* Constructor
*
* @param \Config\View $config
* @param string $viewPath
* @param mixed $loader
* @param boolean $debug
* @param LoggerInterface $logger
* @param \Config\View $config
* @param string $viewPath
* @param mixed $loader
* @param boolean $debug
* @param LoggerInterface $logger
*/
public function __construct($config, string $viewPath = null, $loader = null, bool $debug = null, LoggerInterface $logger = null)
{
Expand Down Expand Up @@ -157,20 +157,25 @@ public function render(string $view, array $options = null, bool $saveData = nul
throw ViewException::forInvalidFile($file);
}

if (is_null($this->tempData))
{
$this->tempData = $this->data;
}

$template = file_get_contents($file);
$output = $this->parse($template, $this->data, $options);
$output = $this->parse($template, $this->tempData, $options);
$this->logPerformance($start, microtime(true), $view);

if (! $saveData)
if ($saveData)
{
$this->data = [];
$this->data = $this->tempData;
}
// Should we cache?
if (isset($options['cache']))
{
cache()->save($cacheName, $output, (int) $options['cache']);
}

$this->tempData = null;
return $output;
}

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

$output = $this->parse($template, $this->data, $options);
if (is_null($this->tempData))
{
$this->tempData = $this->data;
}

$output = $this->parse($template, $this->tempData, $options);

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

if (! $saveData)
if ($saveData)
{
$this->data = [];
$this->data = $this->tempData;
}

$this->tempData = null;

return $output;
}

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

$this->data = array_merge($this->data, $data);
$this->tempData = $this->tempData ?? $this->data;
$this->tempData = array_merge($this->tempData, $data);

return $this;
}
Expand Down Expand Up @@ -532,7 +546,14 @@ protected function parseConditionals(string $template): string

// Parse the PHP itself, or insert an error so they can debug
ob_start();
extract($this->data);

if (is_null($this->tempData))
{
$this->tempData = $this->data;
}

extract($this->tempData);

try
{
eval('?>' . $template . '<?php ');
Expand All @@ -542,6 +563,7 @@ protected function parseConditionals(string $template): string
ob_end_clean();
throw ViewException::forTagSyntaxError(str_replace(['?>', '<?php '], '', $template));
}

return ob_get_clean();
}

Expand Down
48 changes: 35 additions & 13 deletions system/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class View implements RendererInterface
*/
protected $data = [];

/**
* Merge savedData and userData
*/
protected $tempData = null;

/**
* The base directory to look in for our Views.
*
Expand Down Expand Up @@ -189,11 +194,10 @@ public function render(string $view, array $options = null, bool $saveData = nul
// Store the results here so even if
// multiple views are called in a view, it won't
// clean it unless we mean it to.
if ($saveData !== null)
if (is_null($saveData))
{
$this->saveData = $saveData;
$saveData = $this->saveData;
}

$fileExt = pathinfo($view, PATHINFO_EXTENSION);
$realPath = empty($fileExt) ? $view . '.php' : $view; // allow Views as .html, .tpl, etc (from CI3)
$this->renderVars['view'] = $realPath;
Expand Down Expand Up @@ -225,11 +229,17 @@ public function render(string $view, array $options = null, bool $saveData = nul
}

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

if (! $this->saveData)
if (is_null($this->tempData))
{
$this->data = [];
$this->tempData = $this->data;
}

extract($this->tempData);

if ($saveData)
{
$this->data = $this->tempData;
}

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

$this->tempData = null;

return $output;
}

Expand All @@ -300,16 +312,22 @@ public function render(string $view, array $options = null, bool $saveData = nul
public function renderString(string $view, array $options = null, bool $saveData = null): string
{
$start = microtime(true);

if (is_null($saveData))
{
$saveData = $this->config->saveData;
$saveData = $this->saveData;
}

extract($this->data);
if (is_null($this->tempData))
{
$this->tempData = $this->data;
}

extract($this->tempData);

if (! $saveData)
if ($saveData)
{
$this->data = [];
$this->data = $this->tempData;
}

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

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

$this->tempData = null;

return $output;
}

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

$this->data = array_merge($this->data, $data);
$this->tempData = $this->tempData ?? $this->data;
$this->tempData = array_merge($this->tempData, $data);

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

$this->data[$name] = $value;
$this->tempData = $this->tempData ?? $this->data;
$this->tempData[$name] = $value;

return $this;
}
Expand Down Expand Up @@ -407,7 +429,7 @@ public function resetData(): RendererInterface
*/
public function getData(): array
{
return $this->data;
return is_null($this->tempData) ? $this->data : $this->tempData;
}

//--------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions tests/_support/View/Views/simples.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1><?= $testString ?? 'is_not' ?></h1>
13 changes: 12 additions & 1 deletion tests/system/CommonFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class CommonFunctionsTest extends \CodeIgniter\Test\CIUnitTestCase
protected function setUp(): void
{
parent::setUp();

$renderer = Services::renderer();
$renderer->resetData();
unset($_ENV['foo'], $_SERVER['foo']);
}

Expand Down Expand Up @@ -429,4 +430,14 @@ public function testTrace()
$this->assertStringContainsString('Debug Backtrace', $content);
}

public function testViewNotSaveData()
{
$data = [
'testString' => 'bar',
'bar' => 'baz',
];
$this->assertStringContainsString('<h1>bar</h1>', view('\Tests\Support\View\Views\simples', $data, ['saveData' => false]));
$this->assertStringContainsString('<h1>is_not</h1>', view('\Tests\Support\View\Views\simples'));
}

}
22 changes: 13 additions & 9 deletions tests/system/View/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -940,27 +940,31 @@ public function testRenderCantFindView()

public function testRenderSavingData()
{
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
$expected = "<h1>Hello World</h1>\n";

$parser->setData(['testString' => 'Hello World']);
$this->assertEquals($expected, $parser->render('Simpler', [], false));
$this->assertArrayNotHasKey('testString', $parser->getData());

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

public function testRenderStringSavingData()
{
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
$parser->setData(['testString' => 'Hello World']);

$parser = new Parser($this->config, $this->viewsDir, $this->loader);
$expected = '<h1>Hello World</h1>';
$pattern = '<h1>{testString}</h1>';
$this->assertEquals($expected, $parser->renderString($pattern, [], true));
$this->assertArrayHasKey('testString', $parser->getData());

$parser->setData(['testString' => 'Hello World']);
$this->assertEquals($expected, $parser->renderString($pattern, [], false));
$this->assertArrayNotHasKey('testString', $parser->getData());
//last set data is not saved
$parser->setData(['testString' => 'Hello World']);
$this->assertEquals($expected, $parser->renderString($pattern, [], true));
$this->assertArrayHasKey('testString', $parser->getData());
}

public function testRenderFindsOtherView()
Expand Down
29 changes: 25 additions & 4 deletions tests/system/View/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,17 @@ public function testCachedRender()

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

//I think saveData is sava current data, is not clean already set data.
$view->setVar('testString', 'Hello World');
$expected = '<h1>Hello World</h1>';
$this->assertEquals($expected, $view->renderString('<h1><?= $testString ?></h1>', [], true));
$this->assertArrayHasKey('testString', $view->getData());
$this->assertEquals($expected, $view->renderString('<h1><?= $testString ?></h1>', [], false));
$this->assertArrayNotHasKey('testString', $view->getData());

$view->setVar('testString', 'Hello World');
$this->assertEquals($expected, $view->renderString('<h1><?= $testString ?></h1>', [], true));
$this->assertArrayHasKey('testString', $view->getData());
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -343,4 +346,22 @@ public function testRenderLayoutNoContentSection()
$this->assertStringContainsString($expected, $view->render('apples'));
}

public function testRenderSaveDataCover()
{
$view = new View($this->config, $this->viewsDir, $this->loader);
$this->setPrivateProperty($view, 'saveData', true);
$view->setVar('testString', 'test');
$view->render('simple', null, false);
$this->assertEquals(true, $this->getPrivateProperty($view, 'saveData'));
}

public function testRenderSaveDataUseAflterSaveDataFalse()
{
$view = new View($this->config, $this->viewsDir, $this->loader);
$view->setVar('testString', 'test');
$view->render('simple', null, true);
$view->render('simple', null, false);
$this->assertStringContainsString('<h1>test</h1>', $view->render('simple', null, false));
}

}
2 changes: 1 addition & 1 deletion tests/system/View/Views/simple.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h1><?= $testString ?></h1>
<h1><?= $testString ?></h1>

0 comments on commit 27dc596

Please sign in to comment.