Skip to content

Commit

Permalink
fix: tempalte compile error on special expr
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 7, 2021
1 parent 7ca5491 commit 756da34
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
24 changes: 13 additions & 11 deletions app/Lib/Template/Compiler/PregCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function setOpenCloseTag(string $open, string $close): self
{
parent::setOpenCloseTag($open, $close);

$this->openTagE = addslashes($open);
$this->openTagE = addslashes($open);
$this->closeTagE = addslashes($close);

return $this;
Expand All @@ -58,8 +58,7 @@ public function compile(string $tplCode): string
return $tplCode;
}

// $compiler = $this->getCompiler();
// $compiler->compile($code);
$this->prevTokenType = '';

$openTagE = $this->openTagE;
$closeTagE = $this->closeTagE;
Expand Down Expand Up @@ -102,20 +101,23 @@ public function parseCodeBlock(string $block): string
return $block;
}

// special '}' - if, for, foreach end char
if ($trimmed === '}') {
return self::PHP_TAG_OPEN . ' } ' . self::PHP_TAG_CLOSE;
}

$isInline = !str_contains($trimmed, "\n");
// ~^(if|elseif|else|endif|for|endfor|foreach|endforeach)~
$kwPattern = Token::getBlockNamePattern();

// default is define statement.
$type = Token::T_DEFINE;
$open = self::PHP_TAG_OPEN . "\n";
$close = ($isInline ? ' ' : "\n" ) . self::PHP_TAG_CLOSE;
$type = Token::T_DEFINE;
$open = self::PHP_TAG_OPEN . "\n";
$close = ($isInline ? ' ' : "\n") . self::PHP_TAG_CLOSE;

// echo statement
if ($trimmed[0] === '=') {
$type = Token::T_ECHO;
$open = self::PHP_TAG_ECHO;
} elseif (str_starts_with($trimmed, 'echo')) { // echo statement
} elseif (str_starts_with($trimmed, 'echo ')) { // echo statement
$type = Token::T_ECHO;
$open = self::PHP_TAG_OPEN . ' ';
} elseif ($isInline && ($tryType = Token::tryAloneToken($trimmed))) {
Expand All @@ -134,10 +136,10 @@ public function parseCodeBlock(string $block): string

// auto fix pad some chars.
if (Token::canAutoFixed($type)) {
$endChar = $trimmed[strlen($trimmed)-1];
$endChar = $trimmed[strlen($trimmed) - 1];

// not in raw php code AND end char != :
if ($endChar !== '}' && $endChar !== ':') {
if ($endChar !== '}' && $endChar !== '{' && $endChar !== ':') {
$close = ': ' . self::PHP_TAG_CLOSE;
}
}
Expand Down
3 changes: 1 addition & 2 deletions app/Lib/Template/TextTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use function extract;
use function file_exists;
use function file_put_contents;
use function hash;
use function md5;
use function ob_clean;
use function ob_get_clean;
Expand Down Expand Up @@ -107,7 +106,7 @@ protected function doRenderFile(string $tplFile, array $tplVars): string
return ob_get_clean();
} catch (Throwable $e) {
ob_clean();
throw $e;
throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/testdata/full-demo.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@

<?php if ($a < 2) { echo "at if\n"; }?>

if example3:

<?php if ($a < 2){ ?>
at if
<?php } ?>

if-elseif-else example1:

<?php if ($a < 3): ?>
Expand Down
4 changes: 4 additions & 0 deletions test/unittest/Lib/Template/PregCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public function testCompile_if_block():void
'{{if ($a < 4) }} hi {{endif}}',
'<?php if ($a < 4): ?> hi <?php endif ?>',
],
[
'{{if ($a < 4) { }} hi {{ } }}',
'<?php if ($a < 4) { ?> hi <?php } ?>',
],
[
'{{if ($a < 4) }}
hi
Expand Down

0 comments on commit 756da34

Please sign in to comment.