Skip to content

Commit

Permalink
Remove the new yield nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 5, 2024
1 parent 2b06e49 commit 89a8f59
Show file tree
Hide file tree
Showing 22 changed files with 61 additions and 213 deletions.
2 changes: 0 additions & 2 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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;
Expand Down Expand Up @@ -130,7 +129,6 @@ public function __construct(LoaderInterface $loader, $options = [])
$this->addExtension(new CoreExtension());
$this->addExtension(new EscaperExtension($options['autoescape']));
$this->addExtension(new OptimizerExtension($options['optimizations']));
$this->addExtension(new YieldingExtension($options['use_yield']));
}

/**
Expand Down
29 changes: 0 additions & 29 deletions src/Extension/YieldingExtension.php

This file was deleted.

10 changes: 8 additions & 2 deletions src/Node/PrintNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ public function __construct(AbstractExpression $expr, int $lineno, string $tag =

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")
;
Expand Down
10 changes: 8 additions & 2 deletions src/Node/TextNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ public function __construct(string $data, int $lineno)

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")
;
Expand Down
32 changes: 0 additions & 32 deletions src/Node/YieldExpressionNode.php

This file was deleted.

32 changes: 0 additions & 32 deletions src/Node/YieldTextNode.php

This file was deleted.

81 changes: 0 additions & 81 deletions src/NodeVisitor/YieldingNodeVisitor.php

This file was deleted.

7 changes: 3 additions & 4 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
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;

/**
Expand Down Expand Up @@ -120,14 +119,14 @@ public function subparse($test, bool $dropNeedle = false): Node
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:
Expand Down
13 changes: 10 additions & 3 deletions src/Test/NodeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

abstract class NodeTestCase extends TestCase
{
private Environment $currentEnv;

abstract public function getTests();

/**
Expand Down Expand Up @@ -48,7 +50,7 @@ protected function getCompiler(Environment $environment = null)

protected function getEnvironment()
{
return new Environment(new ArrayLoader([]));
return $this->currentEnv = new Environment(new ArrayLoader([]));
}

protected function getVariableGetter($name, $line = false)
Expand All @@ -63,13 +65,18 @@ protected function getAttributeGetter()
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);
}
}
4 changes: 2 additions & 2 deletions src/TokenParser/ApplyTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

use Twig\Node\Expression\TempNameExpression;
use Twig\Node\Node;
use Twig\Node\PrintNode;
use Twig\Node\SetNode;
use Twig\Node\YieldExpressionNode;
use Twig\Token;

/**
Expand Down Expand Up @@ -44,7 +44,7 @@ public function parse(Token $token): Node

return new Node([
new SetNode(true, $ref, $body, $lineno, $this->getTag()),
new YieldExpressionNode($filter, $lineno, $this->getTag()),
new PrintNode($filter, $lineno, $this->getTag()),
]);
}

Expand Down
4 changes: 2 additions & 2 deletions src/TokenParser/BlockTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Twig\Node\BlockNode;
use Twig\Node\BlockReferenceNode;
use Twig\Node\Node;
use Twig\Node\YieldExpressionNode;
use Twig\Node\PrintNode;
use Twig\Token;

/**
Expand Down Expand Up @@ -54,7 +54,7 @@ public function parse(Token $token): Node
}
} 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);
Expand Down
4 changes: 2 additions & 2 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
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;
Expand Down Expand Up @@ -135,7 +135,7 @@ public function parse(Token $token): Node
{
$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
Expand Down
3 changes: 2 additions & 1 deletion tests/Node/AutoEscapeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public function getTests()
{
$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\";"],
];
}
}
3 changes: 1 addition & 2 deletions tests/Node/BlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Twig\Node\BlockNode;
use Twig\Node\Node;
use Twig\Node\TextNode;
use Twig\Node\YieldTextNode;
use Twig\Test\NodeTestCase;

class BlockTest extends NodeTestCase
Expand All @@ -34,7 +33,7 @@ public function getTests()
{
$tests = [];

$tests[] = [new BlockNode('foo', new YieldTextNode('foo', 1), 1), <<<EOF
$tests[] = [new BlockNode('foo', new TextNode('foo', 1), 1), <<<EOF
// line 1
public function block_foo(\$context, array \$blocks = [])
{
Expand Down
Loading

0 comments on commit 89a8f59

Please sign in to comment.