Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3d3caec

Browse filesBrowse files
Replace "use_yield" by #[YieldReady] on nodes
1 parent 7f5958c commit 3d3caec
Copy full SHA for 3d3caec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree

52 files changed

+397
-684
lines changed

‎.github/workflows/ci.yml

Copy file name to clipboardExpand all lines: .github/workflows/ci.yml
+4-16Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ permissions:
1414

1515
jobs:
1616
tests:
17-
name: "PHP ${{ matrix.php-version }} (yield: ${{ matrix.use_yield }})"
17+
name: "PHP ${{ matrix.php-version }}"
1818

1919
runs-on: 'ubuntu-latest'
2020

@@ -31,7 +31,6 @@ jobs:
3131
- '8.2'
3232
- '8.3'
3333
experimental: [false]
34-
use_yield: [true, false]
3534

3635
steps:
3736
- name: "Checkout code"
@@ -49,25 +48,20 @@ jobs:
4948

5049
- run: composer install
5150

52-
- name: "Switch use_yield to true"
53-
if: ${{ matrix.use_yield }}
54-
run: |
55-
sed -i -e "s/'use_yield' => false/'use_yield' => true/" src/Environment.php
56-
5751
- name: "Install PHPUnit"
5852
run: vendor/bin/simple-phpunit install
5953

6054
- name: "PHPUnit version"
6155
run: vendor/bin/simple-phpunit --version
6256

6357
- name: "Run tests"
64-
run: SYMFONY_DEPRECATIONS_HELPER=ignoreFile=./tests/ignore-use-yield-deprecations vendor/bin/simple-phpunit
58+
run: vendor/bin/simple-phpunit
6559

6660
extension-tests:
6761
needs:
6862
- 'tests'
6963

70-
name: "${{ matrix.extension }} PHP ${{ matrix.php-version }} (yield: ${{ matrix.use_yield }})"
64+
name: "${{ matrix.extension }} PHP ${{ matrix.php-version }}"
7165

7266
runs-on: 'ubuntu-latest'
7367

@@ -93,7 +87,6 @@ jobs:
9387
- 'string-extra'
9488
- 'twig-extra-bundle'
9589
experimental: [false]
96-
use_yield: [true, false]
9790

9891
steps:
9992
- name: "Checkout code"
@@ -122,14 +115,9 @@ jobs:
122115
working-directory: extra/${{ matrix.extension}}
123116
run: composer install
124117

125-
- name: "Switch use_yield to true"
126-
if: ${{ matrix.use_yield }}
127-
run: |
128-
sed -i -e "s/'use_yield' => false/'use_yield' => true/" extra/${{ matrix.extension }}/vendor/twig/twig/src/Environment.php
129-
130118
- name: "Run tests for ${{ matrix.extension}}"
131119
working-directory: extra/${{ matrix.extension }}
132-
run: SYMFONY_DEPRECATIONS_HELPER=ignoreFile=../../tests/ignore-use-yield-deprecations ../../vendor/bin/simple-phpunit
120+
run: ../../vendor/bin/simple-phpunit
133121

134122
integration-tests:
135123
needs:

‎src/Compiler.php

Copy file name to clipboardExpand all lines: src/Compiler.php
-28Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,10 @@ class Compiler
2727
private $sourceOffset;
2828
private $sourceLine;
2929
private $varNameSalt = 0;
30-
private $checkForOutput;
3130

3231
public function __construct(Environment $env)
3332
{
3433
$this->env = $env;
35-
$this->checkForOutput = $env->isDebug();
3634
}
3735

3836
public function getEnvironment(): Environment
@@ -87,26 +85,13 @@ public function subcompile(Node $node, bool $raw = true)
8785
return $this;
8886
}
8987

90-
/**
91-
* @return $this
92-
*/
93-
public function checkForOutput(bool $checkForOutput)
94-
{
95-
$this->checkForOutput = $checkForOutput ? $this->env->isDebug() : false;
96-
97-
return $this;
98-
}
99-
10088
/**
10189
* Adds a raw string to the compiled code.
10290
*
10391
* @return $this
10492
*/
10593
public function raw(string $string)
10694
{
107-
if ($this->checkForOutput) {
108-
$this->checkStringForOutput(trim($string));
109-
}
11095
$this->source .= $string;
11196

11297
return $this;
@@ -120,10 +105,6 @@ public function raw(string $string)
120105
public function write(...$strings)
121106
{
122107
foreach ($strings as $string) {
123-
if ($this->checkForOutput) {
124-
$this->checkStringForOutput(trim($string));
125-
}
126-
127108
$this->source .= str_repeat(' ', $this->indentation * 4).$string;
128109
}
129110

@@ -239,13 +220,4 @@ public function getVarName(): string
239220
{
240221
return sprintf('__internal_compile_%d', $this->varNameSalt++);
241222
}
242-
243-
private function checkStringForOutput(string $string): void
244-
{
245-
if (str_starts_with($string, 'echo')) {
246-
trigger_deprecation('twig/twig', '3.9.0', 'Using "echo" in a "Node::compile()" method is deprecated; use a "TextNode" or "PrintNode" instead or use "yield" when "use_yield" is "true" on the environment (triggered by "%s").', $string);
247-
} elseif (str_starts_with($string, 'print')) {
248-
trigger_deprecation('twig/twig', '3.9.0', 'Using "print" in a "Node::compile()" method is deprecated; use a "TextNode" or "PrintNode" instead or use "yield" when "use_yield" is "true" on the environment (triggered by "%s").', $string);
249-
}
250-
}
251223
}

‎src/Environment.php

Copy file name to clipboardExpand all lines: src/Environment.php
+4-21Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Twig\Extension\EscaperExtension;
2323
use Twig\Extension\ExtensionInterface;
2424
use Twig\Extension\OptimizerExtension;
25+
use Twig\Extension\YieldNotReadyExtension;
2526
use Twig\Loader\ArrayLoader;
2627
use Twig\Loader\ChainLoader;
2728
use Twig\Loader\LoaderInterface;
@@ -66,8 +67,6 @@ class Environment
6667
private $runtimeLoaders = [];
6768
private $runtimes = [];
6869
private $optionsHash;
69-
/** @var bool */
70-
private $useYield;
7170

7271
/**
7372
* Constructor.
@@ -99,10 +98,6 @@ class Environment
9998
* * optimizations: A flag that indicates which optimizations to apply
10099
* (default to -1 which means that all optimizations are enabled;
101100
* set it to 0 to disable).
102-
*
103-
* * use_yield: Enable a new mode where template are using "yield" instead of "echo"
104-
* (default to "false", but switch it to "true" when possible
105-
* as this will be the only supported mode in Twig 4.0)
106101
*/
107102
public function __construct(LoaderInterface $loader, $options = [])
108103
{
@@ -116,14 +111,8 @@ public function __construct(LoaderInterface $loader, $options = [])
116111
'cache' => false,
117112
'auto_reload' => null,
118113
'optimizations' => -1,
119-
'use_yield' => false,
120114
], $options);
121115

122-
$this->useYield = (bool) $options['use_yield'];
123-
if (!$this->useYield) {
124-
trigger_deprecation('twig/twig', '3.9.0', 'Not setting "use_yield" to "true" is deprecated.');
125-
}
126-
127116
$this->debug = (bool) $options['debug'];
128117
$this->setCharset($options['charset'] ?? 'UTF-8');
129118
$this->autoReload = null === $options['auto_reload'] ? $this->debug : (bool) $options['auto_reload'];
@@ -133,17 +122,12 @@ public function __construct(LoaderInterface $loader, $options = [])
133122

134123
$this->addExtension(new CoreExtension());
135124
$this->addExtension(new EscaperExtension($options['autoescape']));
125+
if (\PHP_VERSION_ID >= 80000) {
126+
$this->addExtension(new YieldNotReadyExtension());
127+
}
136128
$this->addExtension(new OptimizerExtension($options['optimizations']));
137129
}
138130

139-
/**
140-
* @internal
141-
*/
142-
public function useYield(): bool
143-
{
144-
return $this->useYield;
145-
}
146-
147131
/**
148132
* Enables debugging mode.
149133
*/
@@ -854,7 +838,6 @@ private function updateOptionsHash(): void
854838
self::VERSION,
855839
(int) $this->debug,
856840
(int) $this->strictVariables,
857-
$this->useYield ? '1' : '0',
858841
]);
859842
}
860843
}
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Twig.
5+
*
6+
* (c) Fabien Potencier
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Twig\Extension;
13+
14+
use Twig\NodeVisitor\YieldNotReadyNodeVisitor;
15+
16+
/**
17+
* @internal to be removed in Twig 4
18+
*/
19+
final class YieldNotReadyExtension extends AbstractExtension
20+
{
21+
public function getNodeVisitors(): array
22+
{
23+
return [new YieldNotReadyNodeVisitor()];
24+
}
25+
}

‎src/Node/AutoEscapeNode.php

Copy file name to clipboardExpand all lines: src/Node/AutoEscapeNode.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*
2525
* @author Fabien Potencier <fabien@symfony.com>
2626
*/
27+
#[YieldReady]
2728
class AutoEscapeNode extends Node
2829
{
2930
public function __construct($value, Node $body, int $lineno, string $tag = 'autoescape')

‎src/Node/BlockNode.php

Copy file name to clipboardExpand all lines: src/Node/BlockNode.php
+2-8Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*
2020
* @author Fabien Potencier <fabien@symfony.com>
2121
*/
22+
#[YieldReady]
2223
class BlockNode extends Node
2324
{
2425
public function __construct(string $name, Node $body, int $lineno, ?string $tag = null)
@@ -37,14 +38,7 @@ public function compile(Compiler $compiler): void
3738

3839
$compiler
3940
->subcompile($this->getNode('body'))
40-
;
41-
42-
if (!$this->getNode('body') instanceof NodeOutputInterface && $compiler->getEnvironment()->useYield()) {
43-
// needed when body doesn't yield anything
44-
$compiler->write("yield '';\n");
45-
}
46-
47-
$compiler
41+
->write("return; yield '';\n") // needed when body doesn't yield anything
4842
->outdent()
4943
->write("}\n\n")
5044
;

‎src/Node/BlockReferenceNode.php

Copy file name to clipboardExpand all lines: src/Node/BlockReferenceNode.php
+5-11Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*
2020
* @author Fabien Potencier <fabien@symfony.com>
2121
*/
22+
#[YieldReady]
2223
class BlockReferenceNode extends Node implements NodeOutputInterface
2324
{
2425
public function __construct(string $name, int $lineno, ?string $tag = null)
@@ -28,16 +29,9 @@ public function __construct(string $name, int $lineno, ?string $tag = null)
2829

2930
public function compile(Compiler $compiler): void
3031
{
31-
if ($compiler->getEnvironment()->useYield()) {
32-
$compiler
33-
->addDebugInfo($this)
34-
->write(sprintf("yield from \$this->unwrap()->yieldBlock('%s', \$context, \$blocks);\n", $this->getAttribute('name')))
35-
;
36-
} else {
37-
$compiler
38-
->addDebugInfo($this)
39-
->write(sprintf("\$this->displayBlock('%s', \$context, \$blocks);\n", $this->getAttribute('name')))
40-
;
41-
}
32+
$compiler
33+
->addDebugInfo($this)
34+
->write(sprintf("yield from \$this->unwrap()->yieldBlock('%s', \$context, \$blocks);\n", $this->getAttribute('name')))
35+
;
4236
}
4337
}

‎src/Node/BodyNode.php

Copy file name to clipboardExpand all lines: src/Node/BodyNode.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*
1717
* @author Fabien Potencier <fabien@symfony.com>
1818
*/
19+
#[YieldReady]
1920
class BodyNode extends Node
2021
{
2122
}

‎src/Node/CaptureNode.php

Copy file name to clipboardExpand all lines: src/Node/CaptureNode.php
+10-46Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*
1919
* @author Fabien Potencier <fabien@symfony.com>
2020
*/
21+
#[YieldReady]
2122
class CaptureNode extends Node
2223
{
2324
public function __construct(Node $body, int $lineno, ?string $tag = null)
@@ -27,62 +28,25 @@ public function __construct(Node $body, int $lineno, ?string $tag = null)
2728

2829
public function compile(Compiler $compiler): void
2930
{
30-
if ($compiler->getEnvironment()->useYield()) {
31-
if ($this->getAttribute('raw')) {
32-
$compiler->raw("implode('', iterator_to_array(");
33-
} else {
34-
$compiler->raw("('' === \$tmp = implode('', iterator_to_array(");
35-
}
36-
if ($this->getAttribute('with_blocks')) {
37-
$compiler->raw("(function () use (&\$context, \$macros, \$blocks) {\n");
38-
} else {
39-
$compiler->raw("(function () use (&\$context, \$macros) {\n");
40-
}
41-
$compiler
42-
->indent()
43-
->subcompile($this->getNode('body'))
44-
->outdent()
45-
->write("})() ?? new \EmptyIterator()))")
46-
;
47-
if (!$this->getAttribute('raw')) {
48-
$compiler->raw(") ? '' : new Markup(\$tmp, \$this->env->getCharset())");
49-
}
50-
$compiler->raw(';');
51-
52-
return;
31+
if ($this->getAttribute('raw')) {
32+
$compiler->raw("implode('', iterator_to_array(");
33+
} else {
34+
$compiler->raw("('' === \$tmp = implode('', iterator_to_array(");
5335
}
54-
5536
if ($this->getAttribute('with_blocks')) {
5637
$compiler->raw("(function () use (&\$context, \$macros, \$blocks) {\n");
5738
} else {
5839
$compiler->raw("(function () use (&\$context, \$macros) {\n");
5940
}
60-
$compiler->indent();
61-
if ($compiler->getEnvironment()->isDebug()) {
62-
$compiler->write("ob_start();\n");
63-
} else {
64-
$compiler->write("ob_start(function () { return ''; });\n");
65-
}
6641
$compiler
67-
->write("try {\n")
6842
->indent()
6943
->subcompile($this->getNode('body'))
70-
->raw("\n")
71-
;
72-
if ($this->getAttribute('raw')) {
73-
$compiler->write("return ob_get_contents();\n");
74-
} else {
75-
$compiler->write("return ('' === \$tmp = ob_get_contents()) ? '' : new Markup(\$tmp, \$this->env->getCharset());\n");
76-
}
77-
$compiler
7844
->outdent()
79-
->write("} finally {\n")
80-
->indent()
81-
->write("ob_end_clean();\n")
82-
->outdent()
83-
->write("}\n")
84-
->outdent()
85-
->write('})();')
45+
->write("})() ?? new \EmptyIterator()))")
8646
;
47+
if (!$this->getAttribute('raw')) {
48+
$compiler->raw(") ? '' : new Markup(\$tmp, \$this->env->getCharset())");
49+
}
50+
$compiler->raw(';');
8751
}
8852
}

‎src/Node/CheckSecurityCallNode.php

Copy file name to clipboardExpand all lines: src/Node/CheckSecurityCallNode.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
/**
1717
* @author Fabien Potencier <fabien@symfony.com>
1818
*/
19+
#[YieldReady]
1920
class CheckSecurityCallNode extends Node
2021
{
2122
public function compile(Compiler $compiler)

‎src/Node/CheckSecurityNode.php

Copy file name to clipboardExpand all lines: src/Node/CheckSecurityNode.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
/**
1717
* @author Fabien Potencier <fabien@symfony.com>
1818
*/
19+
#[YieldReady]
1920
class CheckSecurityNode extends Node
2021
{
2122
private $usedFilters;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.