generated from inherelab/php-pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pref: prepare some match pattern before parse
- Loading branch information
Showing
5 changed files
with
182 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PhpPkg\EasyTpl\Compiler; | ||
|
||
/** | ||
* class CompileUtil | ||
* | ||
* @author inhere | ||
* @date 2022/12/30 | ||
*/ | ||
class CompileUtil | ||
{ | ||
/** | ||
* will match: | ||
* - varName | ||
* - top.subKey | ||
*/ | ||
protected const REGEX_VAR_NAME = '@^[a-zA-Z_][\w.-]*$@'; | ||
|
||
/** | ||
* Check is var output. | ||
* | ||
* @param string $line | ||
* | ||
* @return bool | ||
*/ | ||
public static function canAddVarPrefix(string $line): bool | ||
{ | ||
return $line[0] !== '$' && preg_match(self::REGEX_VAR_NAME, $line) === 1; | ||
} | ||
|
||
/** | ||
* convert quick access array key path to php expression. | ||
* | ||
* - convert $ctx.top.sub to $ctx['top']['sub'] | ||
* | ||
* @param string $line | ||
* | ||
* @return string | ||
*/ | ||
public static function pathToArrayAccess(string $line): string | ||
{ | ||
// - convert $ctx.top.sub to $ctx['top']['sub'] | ||
$pattern = '~(' . implode(')|(', [ | ||
'\$[\w.-]+\w', // array key path. | ||
]) . ')~'; | ||
|
||
// https://www.php.net/manual/zh/reference.pcre.pattern.modifiers.php | ||
return preg_replace_callback($pattern, static function (array $matches) { | ||
$varName = $matches[0]; | ||
// convert $ctx.top.sub to $ctx[top][sub] | ||
if (str_contains($varName, '.')) { | ||
$nodes = []; | ||
foreach (explode('.', $varName) as $key) { | ||
if ($key[0] === '$') { | ||
$nodes[] = $key; | ||
} else { | ||
$nodes[] = is_numeric($key) ? "[$key]" : "['$key']"; | ||
} | ||
} | ||
|
||
$varName = implode('', $nodes); | ||
} | ||
|
||
return $varName; | ||
}, $line); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PhpPkg\EasyTplTest\Compiler; | ||
|
||
use PhpPkg\EasyTpl\Compiler\CompileUtil; | ||
use PhpPkg\EasyTplTest\BaseTestCase; | ||
|
||
/** | ||
* class CompileUtilTest | ||
* | ||
* @author inhere | ||
* @date 2022/12/30 | ||
*/ | ||
class CompileUtilTest extends BaseTestCase | ||
{ | ||
public function testCanAddVarPrefix(): void | ||
{ | ||
$this->assertTrue(CompileUtil::canAddVarPrefix('abc')); | ||
$this->assertTrue(CompileUtil::canAddVarPrefix('top.abc')); | ||
$this->assertTrue(CompileUtil::canAddVarPrefix('top.sub-key')); | ||
|
||
$this->assertFalse(CompileUtil::canAddVarPrefix("top['abc']")); | ||
$this->assertFalse(CompileUtil::canAddVarPrefix('abc()')); | ||
} | ||
|
||
public function testPathToArrayAccess(): void | ||
{ | ||
$this->assertEquals('ctx.top.sub', CompileUtil::pathToArrayAccess('ctx.top.sub')); | ||
$this->assertEquals("\$ctx['top']['sub']", CompileUtil::pathToArrayAccess('$ctx.top.sub')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters