From 19181af736fec16b3cdd58b928585730ad32629f Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 2 Oct 2023 12:31:38 +0200 Subject: [PATCH] [application] SnippetNode, SnippetAreaNode: name can be class constant [Closes nette/latte#346] nette/application@9ad504b --- .../Nodes/SnippetAreaNode.php | 9 ++++++ .../ApplicationLatte/Nodes/SnippetNode.php | 13 +++++++-- .../Bridges.Latte3/expected/n-snippet.php | 29 +++++++++++++++++-- .../tests/Bridges.Latte3/n-snippet.phpt | 7 +++++ 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/application/src/Bridges/ApplicationLatte/Nodes/SnippetAreaNode.php b/application/src/Bridges/ApplicationLatte/Nodes/SnippetAreaNode.php index 317298abd2b..0f23c4b03aa 100644 --- a/application/src/Bridges/ApplicationLatte/Nodes/SnippetAreaNode.php +++ b/application/src/Bridges/ApplicationLatte/Nodes/SnippetAreaNode.php @@ -11,6 +11,8 @@ use Latte\Compiler\Block; use Latte\Compiler\Nodes\AreaNode; +use Latte\Compiler\Nodes\Php; +use Latte\Compiler\Nodes\Php\Expression; use Latte\Compiler\Nodes\Php\Scalar; use Latte\Compiler\Nodes\StatementNode; use Latte\Compiler\PrintContext; @@ -34,6 +36,13 @@ public static function create(Tag $tag, TemplateParser $parser): \Generator { $node = new static; $name = $tag->parser->parseUnquotedStringOrExpression(); + if ( + $name instanceof Expression\ClassConstantFetchNode + && $name->class instanceof Php\NameNode + && $name->name instanceof Php\IdentifierNode + ) { + $name = new Scalar\StringNode(constant($name->class . '::' . $name->name), $name->position); + } $node->block = new Block($name, Template::LayerSnippet, $tag); $parser->checkBlockIsUnique($node->block); [$node->content, $endTag] = yield; diff --git a/application/src/Bridges/ApplicationLatte/Nodes/SnippetNode.php b/application/src/Bridges/ApplicationLatte/Nodes/SnippetNode.php index b8c47db4c95..3ea8d3b010d 100644 --- a/application/src/Bridges/ApplicationLatte/Nodes/SnippetNode.php +++ b/application/src/Bridges/ApplicationLatte/Nodes/SnippetNode.php @@ -14,8 +14,8 @@ use Latte\Compiler\Nodes\AreaNode; use Latte\Compiler\Nodes\AuxiliaryNode; use Latte\Compiler\Nodes\Html\ElementNode; -use Latte\Compiler\Nodes\Php\Expression\AssignNode; -use Latte\Compiler\Nodes\Php\Expression\VariableNode; +use Latte\Compiler\Nodes\Php; +use Latte\Compiler\Nodes\Php\Expression; use Latte\Compiler\Nodes\Php\Scalar; use Latte\Compiler\Nodes\StatementNode; use Latte\Compiler\PrintContext; @@ -49,6 +49,13 @@ public static function create(Tag $tag, TemplateParser $parser): \Generator $node->block = new Block(new Scalar\StringNode(''), Template::LayerSnippet, $tag); } else { $name = $tag->parser->parseUnquotedStringOrExpression(); + if ( + $name instanceof Expression\ClassConstantFetchNode + && $name->class instanceof Php\NameNode + && $name->name instanceof Php\IdentifierNode + ) { + $name = new Scalar\StringNode(constant($name->class . '::' . $name->name), $name->position); + } $node->block = new Block($name, Template::LayerSnippet, $tag); if (!$node->block->isDynamic()) { $parser->checkBlockIsUnique($node->block); @@ -148,7 +155,7 @@ private function printAttribute(PrintContext $context): string XX, self::$snippetAttribute, $this->block->isDynamic() - ? new AssignNode(new VariableNode('ʟ_nm'), $this->block->name) + ? new Expression\AssignNode(new Expression\VariableNode('ʟ_nm'), $this->block->name) : $this->block->name, ); } diff --git a/application/tests/Bridges.Latte3/expected/n-snippet.php b/application/tests/Bridges.Latte3/expected/n-snippet.php index edba6563045..1cf255266c1 100644 --- a/application/tests/Bridges.Latte3/expected/n-snippet.php +++ b/application/tests/Bridges.Latte3/expected/n-snippet.php @@ -3,7 +3,7 @@ final class Template%a% extends Latte\Runtime\Template { public const Blocks = [ - 'snippet' => ['outer' => 'blockOuter', 'gallery' => 'blockGallery', 'script' => 'blockScript'], + 'snippet' => ['outer' => 'blockOuter', 'gallery' => 'blockGallery', 'script' => 'blockScript', 'hello' => 'blockHello'], ]; @@ -29,6 +29,12 @@ public function main(array $ʟ_args): void echo '>'; $this->renderBlock('script', [], null, 'snippet') /* line %d% */; echo ' + + '; + $this->renderBlock('hello', [], null, 'snippet') /* line 9 */; + echo ' '; } @@ -76,10 +82,27 @@ public function blockScript(array $ʟ_args): void $this->global->snippetDriver->enter('script', 'static') /* line %d% */; try { - echo LR\Filters::escapeJs('x') /* line 7 */; + echo LR\Filters::escapeJs('x') /* line %d% */; + + } finally { + $this->global->snippetDriver->leave(); + } + } + + + /** n:snippet="Test::Foo" on line %d% */ + public function blockHello(array $ʟ_args): void + { + extract($this->params); + extract($ʟ_args); + unset($ʟ_args); + + $this->global->snippetDriver->enter('hello', 'static') /* line %d% */; + try { + echo LR\Filters::escapeJs('y') /* line %d% */; } finally { $this->global->snippetDriver->leave(); } } -} +} \ No newline at end of file diff --git a/application/tests/Bridges.Latte3/n-snippet.phpt b/application/tests/Bridges.Latte3/n-snippet.phpt index 1842e2f572f..aee56616c8c 100644 --- a/application/tests/Bridges.Latte3/n-snippet.phpt +++ b/application/tests/Bridges.Latte3/n-snippet.phpt @@ -12,6 +12,11 @@ if (version_compare(Latte\Engine::VERSION, '3', '<')) { Tester\Environment::skip('Test for Latte 3'); } +class Test +{ + public const Foo = 'hello'; +} + $latte = new Latte\Engine; $latte->setLoader(new Latte\Loaders\StringLoader); @@ -26,6 +31,8 @@ $template = <<<'EOD' + + EOD; Assert::matchFile(