Skip to content

Commit 4cb75b8

Browse files
committed
fixed twig 3.9 compatibility
1 parent f7e80aa commit 4cb75b8

10 files changed

Lines changed: 56 additions & 51 deletions

composer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"doctrine/inflector": "^2.0.8",
1616
"doctrine/orm": "^2.17.2",
1717
"symfony/filesystem": "^5.4||^6.0",
18-
"twig/twig": "^3.4.3"
18+
"twig/twig": "3.9.3"
1919
},
2020
"require-dev": {
2121
"composer/composer": "^2.0.0",
@@ -24,9 +24,6 @@
2424
"phpspec/prophecy-phpunit": "^2.1",
2525
"phpunit/phpunit": "^9.6.0"
2626
},
27-
"conflict": {
28-
"twig/twig": "^3.9.0"
29-
},
3027
"autoload": {
3128
"psr-4": {
3229
"Hostnet\\Component\\AccessorGenerator\\": "src/"

src/Twig/PerLineNode.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
namespace Hostnet\Component\AccessorGenerator\Twig;
88

9+
use Twig\Attribute\YieldReady;
910
use Twig\Compiler;
11+
use Twig\Node\CaptureNode;
1012
use Twig\Node\Node;
1113

1214
/**
@@ -25,6 +27,7 @@
2527
* the perline block, otherwise a Twig_Node with sub nodes of all
2628
* the nodes between the prefix and/or postfix.
2729
*/
30+
#[YieldReady]
2831
class PerLineNode extends Node
2932
{
3033
/**
@@ -57,19 +60,19 @@ private function compileComplex(Compiler $compiler): void
5760
{
5861
$prefix = $this->getAttribute('prefix');
5962
$postfix = $this->getAttribute('postfix');
60-
$lines = $this->getNode('lines');
6163

6264
// We use normal subcompilation, which uses echo so we buffer our output.
63-
$compiler->write("ob_start();\n");
64-
$compiler->subcompile($lines);
65+
$compiler->write('$lines = ');
66+
$compiler->subcompile(new CaptureNode($this->getNode('lines'), $this->getNode('lines')->lineno));
67+
$compiler->raw("\n");
6568

6669
$ltrim_prefix = ltrim($prefix); // Trimmed version for use on first line
6770
$indent = ! trim($prefix) && ! trim($postfix); // Are we only indenting or also prefixing
6871

6972
// Fetch the content of the lines inside of this block
70-
// and itterate over them
73+
// and iterate over them
7174
$compiler
72-
->write("\$lines = explode(\"\\n\", ob_get_clean());\n")
75+
->write("\$lines = explode(\"\\n\", \$lines);\n")
7376
->write("foreach (\$lines as \$key => \$line) {\n")
7477
->indent(1);
7578

@@ -82,11 +85,11 @@ private function compileComplex(Compiler $compiler): void
8285

8386
// Write out the prefix for this line.
8487
if ($prefix) {
85-
$compiler->write("echo \$key > 0 ? '$prefix' : '$ltrim_prefix' ;\n");
88+
$compiler->write("yield \$key > 0 ? '$prefix' : '$ltrim_prefix' ;\n");
8689
}
8790

8891
// Write the line itself
89-
$compiler->write("echo \"\$line\";\n");
92+
$compiler->write("yield \"\$line\";\n");
9093

9194
// Close if statement for empty line check when indenting.
9295
if ($indent) {
@@ -97,7 +100,7 @@ private function compileComplex(Compiler $compiler): void
97100

98101
// Write postfix and new line.
99102
$compiler
100-
->write("echo \"$postfix\\n\";\n")
103+
->write("yield \"$postfix\\n\";\n")
101104
->outdent(1)
102105
->write("}\n");
103106
}

test/Twig/CodeGenerationExtensionTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public function __construct($name = null, array $data = [], $data_name = '')
3737
'singularize' => '{{ data | singularize }}',
3838
'twos_complement_min' => '{{ data | twos_complement_min }}',
3939
'twos_complement_max' => '{{ data | twos_complement_max }}',
40-
'singularize' => '{{ data | singularize }}',
4140
'perline_stars' => " {% perline %}\n * {{data}} *\n{% endperline %}",
4241
'perline_indent' => " {% perline %}\n {{data}}\n {% endperline %}",
4342
'decimal_right_shift' => '{{ data | decimal_right_shift(amount) }}',

test/Twig/fixtures/data.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// line 1
2-
echo ($context["data"] ?? null);
2+
yield ($context["data"] ?? null);
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// line 1
2-
ob_start();
3-
echo ($context["data"] ?? null);
4-
$lines = explode("\n", ob_get_clean());
2+
$lines = ('' === $tmp = \Twig\Extension\CoreExtension::captureOutput((function () use (&$context, $macros) {
3+
yield ($context["data"] ?? null);
4+
})() ?? new \EmptyIterator())) ? '' : new Markup($tmp, $this->env->getCharset());
5+
$lines = explode("\n", $lines);
56
foreach ($lines as $key => $line) {
6-
echo "$line";
7-
echo "// POSTFIX\n";
7+
yield "$line";
8+
yield "// POSTFIX\n";
89
}

test/Twig/fixtures/indent_data.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// line 1
2-
ob_start();
3-
echo ($context["data"] ?? null);
4-
$lines = explode("\n", ob_get_clean());
2+
$lines = ('' === $tmp = \Twig\Extension\CoreExtension::captureOutput((function () use (&$context, $macros) {
3+
yield ($context["data"] ?? null);
4+
})() ?? new \EmptyIterator())) ? '' : new Markup($tmp, $this->env->getCharset());
5+
$lines = explode("\n", $lines);
56
foreach ($lines as $key => $line) {
67
if (trim($line)) {
7-
echo $key > 0 ? ' ' : '' ;
8-
echo "$line";
8+
yield $key > 0 ? ' ' : '' ;
9+
yield "$line";
910
}
10-
echo "\n";
11+
yield "\n";
1112
}
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// line 1
2-
ob_start();
3-
echo ($context["data"] ?? null);
4-
$lines = explode("\n", ob_get_clean());
2+
$lines = ('' === $tmp = \Twig\Extension\CoreExtension::captureOutput((function () use (&$context, $macros) {
3+
yield ($context["data"] ?? null);
4+
})() ?? new \EmptyIterator())) ? '' : new Markup($tmp, $this->env->getCharset());
5+
$lines = explode("\n", $lines);
56
foreach ($lines as $key => $line) {
6-
echo $key > 0 ? ' ' : '' ;
7-
echo "$line";
8-
echo "// POSTFIX\n";
7+
yield $key > 0 ? ' ' : '' ;
8+
yield "$line";
9+
yield "// POSTFIX\n";
910
}

test/Twig/fixtures/prefix_data.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// line 1
2-
ob_start();
3-
echo ($context["data"] ?? null);
4-
$lines = explode("\n", ob_get_clean());
2+
$lines = ('' === $tmp = \Twig\Extension\CoreExtension::captureOutput((function () use (&$context, $macros) {
3+
yield ($context["data"] ?? null);
4+
})() ?? new \EmptyIterator())) ? '' : new Markup($tmp, $this->env->getCharset());
5+
$lines = explode("\n", $lines);
56
foreach ($lines as $key => $line) {
6-
echo $key > 0 ? ' * ' : '* ' ;
7-
echo "$line";
8-
echo "\n";
7+
yield $key > 0 ? ' * ' : '* ' ;
8+
yield "$line";
9+
yield "\n";
910
}
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// line 1
2-
ob_start();
3-
echo ($context["data"] ?? null);
4-
$lines = explode("\n", ob_get_clean());
2+
$lines = ('' === $tmp = \Twig\Extension\CoreExtension::captureOutput((function () use (&$context, $macros) {
3+
yield ($context["data"] ?? null);
4+
})() ?? new \EmptyIterator())) ? '' : new Markup($tmp, $this->env->getCharset());
5+
$lines = explode("\n", $lines);
56
foreach ($lines as $key => $line) {
6-
echo $key > 0 ? ' * ' : '* ' ;
7-
echo "$line";
8-
echo "// POSTFIX\n";
7+
yield $key > 0 ? ' * ' : '* ' ;
8+
yield "$line";
9+
yield "// POSTFIX\n";
910
}
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// line 1
2-
ob_start();
3-
echo ($context["data"] ?? null);
4-
echo "TEXT";
5-
echo ($context["data"] ?? null);
6-
$lines = explode("\n", ob_get_clean());
2+
$lines = ('' === $tmp = \Twig\Extension\CoreExtension::captureOutput((function () use (&$context, $macros) {
3+
yield ($context["data"] ?? null);
4+
yield "TEXT";
5+
yield ($context["data"] ?? null);
6+
})() ?? new \EmptyIterator())) ? '' : new Markup($tmp, $this->env->getCharset());
7+
$lines = explode("\n", $lines);
78
foreach ($lines as $key => $line) {
8-
echo $key > 0 ? ' * ' : '* ' ;
9-
echo "$line";
10-
echo "// POSTFIX\n";
9+
yield $key > 0 ? ' * ' : '* ' ;
10+
yield "$line";
11+
yield "// POSTFIX\n";
1112
}

0 commit comments

Comments
 (0)