From 15879406c82a6067652ee604944fc38628126c62 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 5 Jan 2024 19:12:23 +0100 Subject: [PATCH] Remove the new yield nodes --- src/Environment.php | 2 - src/Extension/YieldingExtension.php | 29 --------- src/Node/PrintNode.php | 10 ++- src/Node/TextNode.php | 10 ++- src/Node/YieldExpressionNode.php | 32 ---------- src/Node/YieldTextNode.php | 32 ---------- src/NodeVisitor/YieldingNodeVisitor.php | 81 ------------------------- src/Parser.php | 7 +-- src/Test/NodeTestCase.php | 13 +++- src/TokenParser/ApplyTokenParser.php | 4 +- src/TokenParser/BlockTokenParser.php | 4 +- tests/IntegrationTest.php | 4 +- tests/Node/AutoEscapeTest.php | 3 +- tests/Node/BlockTest.php | 3 +- tests/Node/ForTest.php | 11 ++-- tests/Node/IfTest.php | 11 ++-- tests/Node/MacroTest.php | 3 +- tests/Node/ModuleTest.php | 3 +- tests/Node/PrintTest.php | 4 +- tests/Node/SandboxTest.php | 3 +- tests/Node/SetTest.php | 2 +- tests/Node/TextTest.php | 3 +- 22 files changed, 61 insertions(+), 213 deletions(-) delete mode 100644 src/Extension/YieldingExtension.php delete mode 100644 src/Node/YieldExpressionNode.php delete mode 100644 src/Node/YieldTextNode.php delete mode 100644 src/NodeVisitor/YieldingNodeVisitor.php diff --git a/src/Environment.php b/src/Environment.php index b4719b1b5..030e86bc9 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -22,7 +22,6 @@ use Twig\Extension\CoreExtension; use Twig\Extension\EscaperExtension; use Twig\Extension\ExtensionInterface; use Twig\Extension\OptimizerExtension; -use Twig\Extension\YieldingExtension; use Twig\Loader\ArrayLoader; use Twig\Loader\ChainLoader; use Twig\Loader\LoaderInterface; @@ -130,7 +129,6 @@ class Environment $this->addExtension(new CoreExtension()); $this->addExtension(new EscaperExtension($options['autoescape'])); $this->addExtension(new OptimizerExtension($options['optimizations'])); - $this->addExtension(new YieldingExtension($options['use_yield'])); } /** diff --git a/src/Extension/YieldingExtension.php b/src/Extension/YieldingExtension.php deleted file mode 100644 index f2a3fcbec..000000000 --- a/src/Extension/YieldingExtension.php +++ /dev/null @@ -1,29 +0,0 @@ -yielding = $yielding; - } - - public function getNodeVisitors(): array - { - return [new YieldingNodeVisitor($this->yielding)]; - } -} diff --git a/src/Node/PrintNode.php b/src/Node/PrintNode.php index 60386d299..78c67fa2e 100644 --- a/src/Node/PrintNode.php +++ b/src/Node/PrintNode.php @@ -29,9 +29,15 @@ class PrintNode extends Node implements NodeOutputInterface public function compile(Compiler $compiler): void { + $compiler->addDebugInfo($this); + + if ($compiler->getEnvironment()->useYield()) { + $compiler->write('yield '); + } else { + $compiler->write('echo '); + } + $compiler - ->addDebugInfo($this) - ->write('echo ') ->subcompile($this->getNode('expr')) ->raw(";\n") ; diff --git a/src/Node/TextNode.php b/src/Node/TextNode.php index d74ebe630..561288ca7 100644 --- a/src/Node/TextNode.php +++ b/src/Node/TextNode.php @@ -28,9 +28,15 @@ class TextNode extends Node implements NodeOutputInterface public function compile(Compiler $compiler): void { + $compiler->addDebugInfo($this); + + if ($compiler->getEnvironment()->useYield()) { + $compiler->write('yield '); + } else { + $compiler->write('echo '); + } + $compiler - ->addDebugInfo($this) - ->write('echo ') ->string($this->getAttribute('data')) ->raw(";\n") ; diff --git a/src/Node/YieldExpressionNode.php b/src/Node/YieldExpressionNode.php deleted file mode 100644 index e71c3e83e..000000000 --- a/src/Node/YieldExpressionNode.php +++ /dev/null @@ -1,32 +0,0 @@ - - */ -class YieldExpressionNode extends PrintNode -{ - public function compile(Compiler $compiler): void - { - $compiler - ->addDebugInfo($this) - ->write('yield ') - ->subcompile($this->getNode('expr')) - ->raw(";\n") - ; - } -} diff --git a/src/Node/YieldTextNode.php b/src/Node/YieldTextNode.php deleted file mode 100644 index 2da21fe0e..000000000 --- a/src/Node/YieldTextNode.php +++ /dev/null @@ -1,32 +0,0 @@ - - */ -class YieldTextNode extends TextNode -{ - public function compile(Compiler $compiler): void - { - $compiler - ->addDebugInfo($this) - ->write('yield ') - ->string($this->getAttribute('data')) - ->raw(";\n") - ; - } -} diff --git a/src/NodeVisitor/YieldingNodeVisitor.php b/src/NodeVisitor/YieldingNodeVisitor.php deleted file mode 100644 index 8d897690d..000000000 --- a/src/NodeVisitor/YieldingNodeVisitor.php +++ /dev/null @@ -1,81 +0,0 @@ - - * - * @internal - */ -final class YieldingNodeVisitor implements NodeVisitorInterface -{ - private $yielding; - - public function __construct(bool $yielding) - { - $this->yielding = $yielding; - } - - public function enterNode(Node $node, Environment $env): Node - { - if ($node instanceof YieldExpressionNode) { - if ($this->yielding) { - return $node; - } - - return new PrintNode($node->getNode('expr'), $node->getTemplateLine(), $node->getNodeTag()); - } - if ($node instanceof YieldTextNode) { - if ($this->yielding) { - return $node; - } - - return new TextNode($node->getAttribute('data'), $node->getTemplateLine()); - } - - if ($node instanceof PrintNode) { - // FIXME: deprecation - if (!$this->yielding) { - return $node; - } - - return new YieldExpressionNode($node->getNode('expr'), $node->getTemplateLine(), $node->getNodeTag()); - } - if ($node instanceof TextNode) { - // FIXME: deprecation - if (!$this->yielding) { - return $node; - } - - return new YieldTextNode($node->getAttribute('data'), $node->getTemplateLine()); - } - - return $node; - } - - public function leaveNode(Node $node, Environment $env): ?Node - { - return $node; - } - - public function getPriority(): int - { - return 255; - } -} diff --git a/src/Parser.php b/src/Parser.php index a24b7aa68..4016a5f39 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -22,9 +22,8 @@ use Twig\Node\ModuleNode; use Twig\Node\Node; use Twig\Node\NodeCaptureInterface; use Twig\Node\NodeOutputInterface; +use Twig\Node\PrintNode; use Twig\Node\TextNode; -use Twig\Node\YieldExpressionNode; -use Twig\Node\YieldTextNode; use Twig\TokenParser\TokenParserInterface; /** @@ -120,14 +119,14 @@ class Parser switch ($this->getCurrentToken()->getType()) { case /* Token::TEXT_TYPE */ 0: $token = $this->stream->next(); - $rv[] = new YieldTextNode($token->getValue(), $token->getLine()); + $rv[] = new TextNode($token->getValue(), $token->getLine()); break; case /* Token::VAR_START_TYPE */ 2: $token = $this->stream->next(); $expr = $this->expressionParser->parseExpression(); $this->stream->expect(/* Token::VAR_END_TYPE */ 4); - $rv[] = new YieldExpressionNode($expr, $token->getLine()); + $rv[] = new PrintNode($expr, $token->getLine()); break; case /* Token::BLOCK_START_TYPE */ 1: diff --git a/src/Test/NodeTestCase.php b/src/Test/NodeTestCase.php index 187d3bfc6..b10ae11d7 100644 --- a/src/Test/NodeTestCase.php +++ b/src/Test/NodeTestCase.php @@ -19,6 +19,8 @@ use Twig\Node\Node; abstract class NodeTestCase extends TestCase { + private Environment $currentEnv; + abstract public function getTests(); /** @@ -48,7 +50,7 @@ abstract class NodeTestCase extends TestCase protected function getEnvironment() { - return new Environment(new ArrayLoader([])); + return $this->currentEnv = new Environment(new ArrayLoader([])); } protected function getVariableGetter($name, $line = false) @@ -63,13 +65,18 @@ abstract class NodeTestCase extends TestCase return 'CoreExtension::getAttribute($this->env, $this->source, '; } + protected function getEchoOrYield(): string + { + return ($this->currentEnv ?? $this->getEnvironment())->useYield() ? 'yield' : 'echo'; + } + protected function getDisplayOrYield(string $expr): string { - return sprintf($this->getEnvironment()->useYield() ? 'yield from %s->unwrap()->yield' : '%s->display', $expr); + return sprintf(($this->currentEnv ?? $this->getEnvironment())->useYield() ? 'yield from %s->unwrap()->yield' : '%s->display', $expr); } protected function getDisplayOrYieldBlock(string $expr): string { - return sprintf($this->getEnvironment()->useYield() ? 'yield from %s->unwrap()->yieldBlock' : '%s->displayBlock', $expr); + return sprintf(($this->currentEnv ?? $this->getEnvironment())->useYield() ? 'yield from %s->unwrap()->yieldBlock' : '%s->displayBlock', $expr); } } diff --git a/src/TokenParser/ApplyTokenParser.php b/src/TokenParser/ApplyTokenParser.php index dd22f8103..4dbf30406 100644 --- a/src/TokenParser/ApplyTokenParser.php +++ b/src/TokenParser/ApplyTokenParser.php @@ -13,8 +13,8 @@ namespace Twig\TokenParser; use Twig\Node\Expression\TempNameExpression; use Twig\Node\Node; +use Twig\Node\PrintNode; use Twig\Node\SetNode; -use Twig\Node\YieldExpressionNode; use Twig\Token; /** @@ -44,7 +44,7 @@ final class ApplyTokenParser extends AbstractTokenParser return new Node([ new SetNode(true, $ref, $body, $lineno, $this->getTag()), - new YieldExpressionNode($filter, $lineno, $this->getTag()), + new PrintNode($filter, $lineno, $this->getTag()), ]); } diff --git a/src/TokenParser/BlockTokenParser.php b/src/TokenParser/BlockTokenParser.php index d51ad3156..5878131be 100644 --- a/src/TokenParser/BlockTokenParser.php +++ b/src/TokenParser/BlockTokenParser.php @@ -16,7 +16,7 @@ use Twig\Error\SyntaxError; use Twig\Node\BlockNode; use Twig\Node\BlockReferenceNode; use Twig\Node\Node; -use Twig\Node\YieldExpressionNode; +use Twig\Node\PrintNode; use Twig\Token; /** @@ -54,7 +54,7 @@ final class BlockTokenParser extends AbstractTokenParser } } else { $body = new Node([ - new YieldExpressionNode($this->parser->getExpressionParser()->parseExpression(), $lineno), + new PrintNode($this->parser->getExpressionParser()->parseExpression(), $lineno), ]); } $stream->expect(/* Token::BLOCK_END_TYPE */ 3); diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index f2ee4eb1f..e2b211a01 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -18,7 +18,7 @@ use Twig\Extension\SandboxExtension; use Twig\Extension\StringLoaderExtension; use Twig\Node\Expression\ConstantExpression; use Twig\Node\Node; -use Twig\Node\YieldExpressionNode; +use Twig\Node\PrintNode; use Twig\Sandbox\SecurityPolicy; use Twig\Test\IntegrationTestCase; use Twig\Token; @@ -135,7 +135,7 @@ class TwigTestTokenParser_§ extends AbstractTokenParser { $this->parser->getStream()->expect(Token::BLOCK_END_TYPE); - return new YieldExpressionNode(new ConstantExpression('§', -1), -1); + return new PrintNode(new ConstantExpression('§', -1), -1); } public function getTag(): string diff --git a/tests/Node/AutoEscapeTest.php b/tests/Node/AutoEscapeTest.php index d0f641c08..b2df9b160 100644 --- a/tests/Node/AutoEscapeTest.php +++ b/tests/Node/AutoEscapeTest.php @@ -31,9 +31,10 @@ class AutoEscapeTest extends NodeTestCase { $body = new Node([new TextNode('foo', 1)]); $node = new AutoEscapeNode(true, $body, 1); + $displayStmt = $this->getEchoOrYield(); return [ - [$node, "// line 1\necho \"foo\";"], + [$node, "// line 1\n$displayStmt \"foo\";"], ]; } } diff --git a/tests/Node/BlockTest.php b/tests/Node/BlockTest.php index 07e9373db..29b2e0f9d 100644 --- a/tests/Node/BlockTest.php +++ b/tests/Node/BlockTest.php @@ -16,7 +16,6 @@ use Twig\Loader\ArrayLoader; use Twig\Node\BlockNode; use Twig\Node\Node; use Twig\Node\TextNode; -use Twig\Node\YieldTextNode; use Twig\Test\NodeTestCase; class BlockTest extends NodeTestCase @@ -34,7 +33,7 @@ class BlockTest extends NodeTestCase { $tests = []; - $tests[] = [new BlockNode('foo', new YieldTextNode('foo', 1), 1), <<setAttribute('with_loop', false); + $displayStmt = $this->getEchoOrYield(); $tests[] = [$node, <<getVariableGetter('items')}); foreach (\$context['_seq'] as \$context["key"] => \$context["item"]) { - echo {$this->getVariableGetter('foo')}; + $displayStmt {$this->getVariableGetter('foo')}; } \$_parent = \$context['_parent']; unset(\$context['_seq'], \$context['_iterated'], \$context['key'], \$context['item'], \$context['_parent'], \$context['loop']); @@ -93,7 +94,7 @@ if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_ \$context['loop']['last'] = 1 === \$length; } foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) { - echo {$this->getVariableGetter('foo')}; + $displayStmt {$this->getVariableGetter('foo')}; ++\$context['loop']['index0']; ++\$context['loop']['index']; \$context['loop']['first'] = false; @@ -135,7 +136,7 @@ if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_ \$context['loop']['last'] = 1 === \$length; } foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) { - echo {$this->getVariableGetter('foo')}; + $displayStmt {$this->getVariableGetter('foo')}; ++\$context['loop']['index0']; ++\$context['loop']['index']; \$context['loop']['first'] = false; @@ -178,7 +179,7 @@ if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_ \$context['loop']['last'] = 1 === \$length; } foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) { - echo {$this->getVariableGetter('foo')}; + $displayStmt {$this->getVariableGetter('foo')}; \$context['_iterated'] = true; ++\$context['loop']['index0']; ++\$context['loop']['index']; @@ -190,7 +191,7 @@ foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) { } } if (!\$context['_iterated']) { - echo {$this->getVariableGetter('foo')}; + $displayStmt {$this->getVariableGetter('foo')}; } \$_parent = \$context['_parent']; unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']); diff --git a/tests/Node/IfTest.php b/tests/Node/IfTest.php index d5a6eac8a..5dda061d0 100644 --- a/tests/Node/IfTest.php +++ b/tests/Node/IfTest.php @@ -47,11 +47,12 @@ class IfTest extends NodeTestCase ], [], 1); $else = null; $node = new IfNode($t, $else, 1); + $displayStmt = $this->getEchoOrYield(); $tests[] = [$node, <<getVariableGetter('foo')}; + $displayStmt {$this->getVariableGetter('foo')}; } EOF ]; @@ -68,9 +69,9 @@ EOF $tests[] = [$node, <<getVariableGetter('foo')}; + $displayStmt {$this->getVariableGetter('foo')}; } elseif (false) { - echo {$this->getVariableGetter('bar')}; + $displayStmt {$this->getVariableGetter('bar')}; } EOF ]; @@ -85,9 +86,9 @@ EOF $tests[] = [$node, <<getVariableGetter('foo')}; + $displayStmt {$this->getVariableGetter('foo')}; } else { - echo {$this->getVariableGetter('bar')}; + $displayStmt {$this->getVariableGetter('bar')}; } EOF ]; diff --git a/tests/Node/MacroTest.php b/tests/Node/MacroTest.php index 16ccd92cb..7efea3cef 100644 --- a/tests/Node/MacroTest.php +++ b/tests/Node/MacroTest.php @@ -18,7 +18,6 @@ use Twig\Node\Expression\NameExpression; use Twig\Node\MacroNode; use Twig\Node\Node; use Twig\Node\TextNode; -use Twig\Node\YieldTextNode; use Twig\Test\NodeTestCase; class MacroTest extends NodeTestCase @@ -43,7 +42,7 @@ class MacroTest extends NodeTestCase 'bar' => new ConstantExpression('Foo', 1), ], [], 1); - $body = new YieldTextNode('foo', 1); + $body = new TextNode('foo', 1); $node = new MacroNode('foo', $body, $arguments, 1); $text[] = [$node, <<getEnvironment()->useYield() ? 'YieldingTemplate' : 'Template'; + $displayStmt = $this->getEchoOrYield(); $node = new ModuleNode($body, $extends, $blocks, $macros, $traits, new Node([]), $source); $tests[] = [$node, <<macros; // line 1 - echo "foo"; + $displayStmt "foo"; } /** diff --git a/tests/Node/PrintTest.php b/tests/Node/PrintTest.php index 49f8eb498..f951c2e36 100644 --- a/tests/Node/PrintTest.php +++ b/tests/Node/PrintTest.php @@ -28,7 +28,9 @@ class PrintTest extends NodeTestCase public function getTests() { $tests = []; - $tests[] = [new PrintNode(new ConstantExpression('foo', 1), 1), "// line 1\necho \"foo\";"]; + $displayStmt = $this->getEchoOrYield(); + + $tests[] = [new PrintNode(new ConstantExpression('foo', 1), 1), "// line 1\n$displayStmt \"foo\";"]; return $tests; } diff --git a/tests/Node/SandboxTest.php b/tests/Node/SandboxTest.php index 7cbddd75f..bf16f1f03 100644 --- a/tests/Node/SandboxTest.php +++ b/tests/Node/SandboxTest.php @@ -31,6 +31,7 @@ class SandboxTest extends NodeTestCase $body = new TextNode('foo', 1); $node = new SandboxNode($body, 1); + $displayStmt = $this->getEchoOrYield(); $tests[] = [$node, <<sandbox->isSandboxed()) { \$this->sandbox->enableSandbox(); } try { - echo "foo"; + $displayStmt "foo"; } finally { if (!\$alreadySandboxed) { \$this->sandbox->disableSandbox(); diff --git a/tests/Node/SetTest.php b/tests/Node/SetTest.php index 98d4e5735..70b3530bd 100644 --- a/tests/Node/SetTest.php +++ b/tests/Node/SetTest.php @@ -55,7 +55,7 @@ EOF $tests[] = [$node, <<env->getCharset()); EOF , new Environment(new ArrayLoader(), ['use_yield' => true]), diff --git a/tests/Node/TextTest.php b/tests/Node/TextTest.php index ace191213..31639cc2d 100644 --- a/tests/Node/TextTest.php +++ b/tests/Node/TextTest.php @@ -26,7 +26,8 @@ class TextTest extends NodeTestCase public function getTests() { $tests = []; - $tests[] = [new TextNode('foo', 1), "// line 1\necho \"foo\";"]; + $displayStmt = $this->getEchoOrYield(); + $tests[] = [new TextNode('foo', 1), "// line 1\n$displayStmt \"foo\";"]; return $tests; }