Skip to content

Commit

Permalink
up: compiler support unwrap php tag for custom directive
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 8, 2023
1 parent 3a85651 commit b3cb0ff
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
17 changes: 15 additions & 2 deletions src/Compiler/AbstractCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ abstract class AbstractCompiler implements CompilerInterface
*/
protected array $directiveNames = [];

/**
* @var array will not wrap PHP tag to directive result.
*/
protected array $unwrapDirectives = [];

/**
* @return static
*/
Expand Down Expand Up @@ -216,12 +221,20 @@ public function addFilter(string $name, string $callExpr): self
/**
* @param string $name
* @param callable $handler
* @param bool $unwrap
*
* @return static
*/
public function addDirective(string $name, callable $handler): static
public function addDirective(string $name, callable $handler, bool $unwrap = false): static
{
$this->directiveNames[] = $name;
if (!isset($this->customDirectives[$name])) {
$this->directiveNames[] = $name;
}

if ($unwrap) {
$this->unwrapDirectives[] = $name;
}

$this->customDirectives[$name] = $handler;

return $this;
Expand Down
23 changes: 13 additions & 10 deletions src/Compiler/PregCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace PhpPkg\EasyTpl\Compiler;

use function addslashes;
use function in_array;
use function preg_match;
use function preg_replace;
use function preg_replace_callback;
Expand Down Expand Up @@ -87,7 +88,12 @@ public function compile(string $tplCode): string
return preg_replace_callback(
"~$openTagE\s*(.+?)$closeTagE~s", // Amixu, iu, s
function (array $matches) {
return $this->parseCodeBlock($matches[1]);
// empty line, keep it.
if (!$trimmed = trim($matches[1])) {
return $matches[1];
}

return $this->parseCodeBlock($trimmed);
},
$tplCode,
-1,
Expand Down Expand Up @@ -129,24 +135,20 @@ protected function buildMatchPatterns(): void
* - 'include'
* - 'block'
*
* @param string $block
* @param string $trimmed trimmed block string.
*
* @return string
*/
public function parseCodeBlock(string $block): string
public function parseCodeBlock(string $trimmed): string
{
// empty line, keep it.
if (!$trimmed = trim($block)) {
return $block;
}

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

$directive = '';
$isInline = !str_contains($trimmed, "\n");
$unwrapRet = false;
$isInline = !str_contains($trimmed, "\n");

// default is define statement.
$type = Token::T_DEFINE;
Expand Down Expand Up @@ -197,6 +199,7 @@ public function parseCodeBlock(string $block): string
// support user add special directives.
$directive = $type = $matches[1];
$handlerFn = $this->customDirectives[$directive];
$unwrapRet = in_array($directive, $this->unwrapDirectives, true);

$trimmed = $handlerFn(substr($trimmed, strlen($directive)), $directive);
} elseif ($isInline && !str_contains($trimmed, '=')) {
Expand All @@ -207,7 +210,7 @@ public function parseCodeBlock(string $block): string

// not need continue handle
if ($directive || Token::isAloneToken($type)) {
return $open . $trimmed . $close;
return $unwrapRet ? $trimmed : $open . $trimmed . $close;
}

// inline echo support filters
Expand Down
2 changes: 1 addition & 1 deletion test/Compiler/PregCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function testCompileCode_inline_echo():void
['{{= __LINE__ }}', '<?= __LINE__ ?>'],
['{{ echo __LINE__ }}', '<?php echo __LINE__ ?>'],
['{{ SomeClass::NAME }}', '<?= SomeClass::NAME ?>'],
['{{ echo SomeClass::NAME }}', '<?= SomeClass::NAME ?>'],
['{{ echo SomeClass::NAME }}', '<?php echo SomeClass::NAME ?>'],
// prop
['{{ SomeClass::$name }}', '<?= SomeClass::$name ?>'],
// var
Expand Down

0 comments on commit b3cb0ff

Please sign in to comment.