Skip to content

Commit

Permalink
up: auto add var prefix support const
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 6, 2023
1 parent 7b4da5d commit a3adea0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 60 deletions.
21 changes: 20 additions & 1 deletion src/Compiler/CompileUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace PhpPkg\EasyTpl\Compiler;

use function defined;
use function str_contains;
use function str_starts_with;

/**
* class CompileUtil
*
Expand All @@ -26,7 +30,22 @@ class CompileUtil
*/
public static function canAddVarPrefix(string $line): bool
{
return $line[0] !== '$' && preg_match(self::REGEX_VAR_NAME, $line) === 1;
// has prefix or is magic const. (eg: __LINE__)
if ($line[0] === '$' || str_starts_with($line, '__')) {
return false;
}

if (preg_match(self::REGEX_VAR_NAME, $line) === 1) {
if (str_contains($line, '.') || str_contains($line, '-')) {
return true;
}

// up: check is const name
// - defined() cannot check magic const. (eg: __LINE__)
return !defined($line);
}

return false;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions test/Compiler/CompileUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public function testCanAddVarPrefix(): void

$this->assertFalse(CompileUtil::canAddVarPrefix("top['abc']"));
$this->assertFalse(CompileUtil::canAddVarPrefix('abc()'));
$this->assertFalse(CompileUtil::canAddVarPrefix('PHP_OS'));
$this->assertFalse(CompileUtil::canAddVarPrefix('__LINE__'));
}

public function testPathToArrayAccess(): void
Expand Down
119 changes: 60 additions & 59 deletions test/Compiler/PregCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testToken_getBlockNamePattern():void
];

$pattern = Token::getBlockNamePattern();
vdump($pattern);
// vdump($pattern);
foreach ($tests as [$in, $out]) {
$ret = preg_match($pattern, $in, $matches);
if ($out) {
Expand All @@ -79,10 +79,27 @@ public function testCompileCode_inline_echo():void
{
$p = new PregCompiler();

$this->assertNotEquals('<?= $name ?>', $p->compile('{{= name }}'));

$tests = [
['{{ "a" . "b" }}', '<?= "a" . "b" ?>'],
// const
['{{ PHP_OS }}', '<?= PHP_OS ?>'],
['{{= PHP_OS }}', '<?= PHP_OS ?>'],
["{{\n PHP_OS }}", "<?= PHP_OS ?>"],
['{{ echo PHP_OS }}', '<?php echo PHP_OS ?>'],
['{{ __LINE__ }}', '<?= __LINE__ ?>'],
['{{= __LINE__ }}', '<?= __LINE__ ?>'],
['{{ echo __LINE__ }}', '<?php echo __LINE__ ?>'],
['{{ SomeClass::NAME }}', '<?= SomeClass::NAME ?>'],
['{{ echo SomeClass::NAME }}', '<?= SomeClass::NAME ?>'],
// prop
['{{ SomeClass::$name }}', '<?= SomeClass::$name ?>'],
// var
['{{ name }}', '<?= $name ?>'],
['{{ $name }}', '<?= $name ?>'],
['{{= $name }}', '<?= $name ?>'],
['{{ echo $name }}', '<?php echo $name ?>'],
['{{ $name; }}', '<?= $name; ?>'],
['{{ $name ?: "inhere" }}', '<?= $name ?: "inhere" ?>'],
['{{ $name ?? "inhere" }}', '<?= $name ?? "inhere" ?>'],
Expand All @@ -104,37 +121,21 @@ public function testCompileCode_inline_echo():void
// func
['{{ some_func() }}', '<?= some_func() ?>'],
['{{ some_func(); }}', '<?= some_func(); ?>'],
['{{ SomeClass::func(); }}', '<?= SomeClass::func(); ?>'],
['{{ $this->include("header.tpl") }}', '<?= $this->include("header.tpl") ?>'],
// more
[
'{{= $ctx.pkgName ?? "org.example.entity" }}',
'<?= $ctx[\'pkgName\'] ?? "org.example.entity" ?>'
],
[
'{{= $ctx->pkgName ?? "org.example.entity" }}',
'<?= $ctx->pkgName ?? "org.example.entity" ?>'
],
];
foreach ($tests as [$in, $out]) {
$this->assertEquals($out, $p->compile($in));
}

$tplCode = <<<'TPL'
{{= $ctx.pkgName ?? "org.example.entity" }}
TPL;
$compiled = $p->compile($tplCode);
// vdump($tplCode, $compiled);
$this->assertNotEmpty($compiled);
$this->assertEquals(<<<'CODE'
<?= $ctx['pkgName'] ?? "org.example.entity" ?>
CODE
,$compiled);

$tplCode = <<<'TPL'
{{= $ctx->pkgName ?? "org.example.entity" }}
TPL;
$compiled = $p->compile($tplCode);
// vdump($tplCode, $compiled);
$this->assertNotEmpty($compiled);
$this->assertEquals(<<<'CODE'
<?= $ctx->pkgName ?? "org.example.entity" ?>
CODE
,$compiled);
}

public function testCompile_inline_echo_with_filters():void
Expand Down Expand Up @@ -182,6 +183,38 @@ public function testCompile_disableEchoFilter():void
}
}

public function testCompile_ml_define():void
{
$p = new PregCompiler();

$code = <<<'CODE'
{{
$a = random_int(1, 10);
}}
CODE;
$compiled = $p->compile($code);
$this->assertEquals(<<<'CODE'
<?php $a = random_int(1, 10); ?>
CODE
,$compiled);

$code = <<<'CODE'
{{
// comments
$a = random_int(1, 10);
}}
CODE;
$compiled = $p->compile($code);
$this->assertEquals(<<<'CODE'
<?php
// comments
$a = random_int(1, 10);
?>
CODE
,$compiled);
}

public function testCompile_comments():void
{
$p = new PregCompiler();
Expand Down Expand Up @@ -274,38 +307,6 @@ public function testCompile_foreach():void
,$compiled);
}

public function testCompile_ml_define():void
{
$p = new PregCompiler();

$code = <<<'CODE'
{{
$a = random_int(1, 10);
}}
CODE;
$compiled = $p->compile($code);
$this->assertEquals(<<<'CODE'
<?php $a = random_int(1, 10); ?>
CODE
,$compiled);

$code = <<<'CODE'
{{
// comments
$a = random_int(1, 10);
}}
CODE;
$compiled = $p->compile($code);
$this->assertEquals(<<<'CODE'
<?php
// comments
$a = random_int(1, 10);
?>
CODE
,$compiled);
}

public function testCompile_customDirective_include():void
{
$p = new PregCompiler();
Expand Down

0 comments on commit a3adea0

Please sign in to comment.