Skip to content

Commit 8f5f680

Browse files
committed
fix. Parser use tempData
1 parent 88e73cb commit 8f5f680

6 files changed

Lines changed: 53 additions & 29 deletions

File tree

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: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public function render(string $view, array $options = null, bool $saveData = nul
194194
// Store the results here so even if
195195
// multiple views are called in a view, it won't
196196
// clean it unless we mean it to.
197-
if ($saveData === null)
197+
if (is_null($saveData))
198198
{
199199
$saveData = $this->saveData;
200200
}
@@ -230,7 +230,7 @@ public function render(string $view, array $options = null, bool $saveData = nul
230230

231231
// Make our view data available to the view.
232232

233-
if(is_null($this->tempData))
233+
if (is_null($this->tempData))
234234
{
235235
$this->tempData = $this->data;
236236
}
@@ -286,7 +286,9 @@ public function render(string $view, array $options = null, bool $saveData = nul
286286
{
287287
cache()->save($this->renderVars['cacheName'], $output, (int) $this->renderVars['options']['cache']);
288288
}
289+
289290
$this->tempData = null;
291+
290292
return $output;
291293
}
292294

@@ -316,7 +318,7 @@ public function renderString(string $view, array $options = null, bool $saveData
316318
$saveData = $this->saveData;
317319
}
318320

319-
if(is_null($this->tempData))
321+
if (is_null($this->tempData))
320322
{
321323
$this->tempData = $this->data;
322324
}
@@ -335,7 +337,9 @@ public function renderString(string $view, array $options = null, bool $saveData
335337
@ob_end_clean();
336338

337339
$this->logPerformance($start, microtime(true), $this->excerpt($view));
340+
338341
$this->tempData = null;
342+
339343
return $output;
340344
}
341345

@@ -370,6 +374,7 @@ public function setData(array $data = [], string $context = null): RendererInter
370374
{
371375
$data = \esc($data, $context);
372376
}
377+
373378
$this->tempData = $this->tempData ?? $this->data;
374379
$this->tempData = array_merge($this->tempData, $data);
375380

@@ -395,7 +400,7 @@ public function setVar(string $name, $value = null, string $context = null): Ren
395400
$value = \esc($value, $context);
396401
}
397402

398-
$this->tempData = $this->tempData ?? $this->data;
403+
$this->tempData = $this->tempData ?? $this->data;
399404
$this->tempData[$name] = $value;
400405

401406
return $this;
@@ -424,7 +429,7 @@ public function resetData(): RendererInterface
424429
*/
425430
public function getData(): array
426431
{
427-
return $this->tempData ?? $this->data;
432+
return is_null($this->tempData) ? $this->data : $this->tempData;
428433
}
429434

430435
//--------------------------------------------------------------------
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: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -432,16 +432,12 @@ public function testTrace()
432432

433433
public function testViewNotSaveData()
434434
{
435-
436-
$data = [
435+
$data = [
437436
'testString' => 'bar',
438437
'bar' => 'baz',
439438
];
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();
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'));
445441
}
446442

447443
}

tests/system/View/ParserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,10 +957,10 @@ public function testRenderStringSavingData()
957957

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());
962960
$this->assertEquals($expected, $parser->renderString($pattern, [], false));
963961
$this->assertArrayNotHasKey('testString', $parser->getData());
962+
$this->assertEquals($expected, $parser->renderString($pattern, [], true));
963+
$this->assertArrayHasKey('testString', $parser->getData());
964964
}
965965

966966
public function testRenderFindsOtherView()

tests/system/View/ViewTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ 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);
234234
$expected = '<h1>Hello World</h1>';
235235

236236
//I think saveData is sava current data, is not clean already set data.

0 commit comments

Comments
 (0)