-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add costume directive use by Templator (#384)
- Loading branch information
1 parent
439fcb3
commit 5966265
Showing
5 changed files
with
156 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace System\View\Exceptions; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class DirectiveCanNotBeRegister extends \InvalidArgumentException | ||
{ | ||
public function __construct(string $name, string $use_by) | ||
{ | ||
parent::__construct("Directive '$name' cant be use, this has been use in '$use_by'."); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace System\View\Exceptions; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class DirectiveNotRegister extends \InvalidArgumentException | ||
{ | ||
public function __construct(string $name) | ||
{ | ||
parent::__construct("Directive '$name' is not registered."); | ||
} | ||
} |
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,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace System\View\Templator; | ||
|
||
use System\View\AbstractTemplatorParse; | ||
use System\View\Exceptions\DirectiveCanNotBeRegister; | ||
use System\View\Exceptions\DirectiveNotRegister; | ||
|
||
class DirectiveTemplator extends AbstractTemplatorParse | ||
{ | ||
/** | ||
* @var array<string, \Closure> | ||
*/ | ||
private static array $directive = []; | ||
|
||
/** | ||
* Excludes list of directive alredy use by Templator. | ||
* | ||
* @var array<string, string> | ||
*/ | ||
public static array $excludeList = [ | ||
'break' => BreakTemplator::class, | ||
'continue' => ContinueTemplator::class, | ||
'else' => IfTemplator::class, | ||
'extend' => SectionTemplator::class, | ||
'foreach' => EachTemplator::class, | ||
'if' => IfTemplator::class, | ||
'include' => IncludeTemplator::class, | ||
'php' => PHPTemplator::class, | ||
'raw' => NameTemplator::class, | ||
'section' => SectionTemplator::class, | ||
'set' => SetTemplator::class, | ||
'use' => UseTemplator::class, | ||
]; | ||
|
||
public static function register(string $name, \Closure $callable): void | ||
{ | ||
if (array_key_exists($name, self::$excludeList)) { | ||
throw new DirectiveCanNotBeRegister($name, self::$excludeList[$name]); | ||
} | ||
|
||
self::$directive[$name] = $callable; | ||
} | ||
|
||
public static function call(string $name, mixed ...$parameters): string | ||
{ | ||
if (false === array_key_exists($name, self::$directive)) { | ||
throw new DirectiveNotRegister($name); | ||
} | ||
|
||
$callback = self::$directive[$name]; | ||
|
||
return (string) $callback(...$parameters); | ||
} | ||
|
||
public function parse(string $template): string | ||
{ | ||
return preg_replace_callback( | ||
'/{%\s*(\w+)\((.*?)\)\s*%}/', | ||
function ($matches) { | ||
$name = $matches[1]; | ||
$params = explode(',', $matches[2]); | ||
$params = array_map(fn ($param) => ltrim($param), $params); | ||
|
||
return array_key_exists($name, self::$excludeList) | ||
? $matches[0] | ||
: '<?php echo System\View\Templator\DirectiveTemplator::call(\'' . $name . '\', ' . implode(', ', $params) . '); ?>' | ||
; | ||
}, | ||
$template | ||
); | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace System\Test\View\Templator; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use System\View\Exceptions\DirectiveCanNotBeRegister; | ||
use System\View\Exceptions\DirectiveNotRegister; | ||
use System\View\Templator; | ||
use System\View\Templator\DirectiveTemplator; | ||
use System\View\TemplatorFinder; | ||
|
||
final class DirectiveTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function itCanRenderEachBreak() | ||
{ | ||
DirectiveTemplator::register('sum', fn ($a, $b): int => $a + $b); | ||
$templator = new Templator(new TemplatorFinder([__DIR__], ['']), __DIR__); | ||
$out = $templator->templates('<html><head></head><body>{% sum(1, 2) %}</body></html>'); | ||
$this->assertEquals("<html><head></head><body><?php echo System\View\Templator\DirectiveTemplator::call('sum', 1, 2); ?></body></html>", $out); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function itThowExcaptionDueDirectiveNotRegister() | ||
{ | ||
$this->expectException(DirectiveNotRegister::class); | ||
DirectiveTemplator::call('unknow', 0); | ||
} | ||
|
||
public function itCanNotRegisterDirective(): void | ||
{ | ||
$this->expectException(DirectiveCanNotBeRegister::class); | ||
DirectiveTemplator::register('include', fn ($file): string => $file); | ||
} | ||
|
||
public function itCanRegisterAndCallDirective(): void | ||
{ | ||
DirectiveTemplator::register('sum', fn ($a, $b): int => $a + $b); | ||
$this->assertEquals(2, DirectiveTemplator::call('sum', 1, 1)); | ||
} | ||
} |