Skip to content

Commit 2a2ab7c

Browse files
WebMambakbond
authored andcommitted
Fix twig_to_array is deprecated
1 parent 8fe391d commit 2a2ab7c

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/TwigComponent/src/Twig/ComponentNode.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\UX\TwigComponent\BlockStack;
1515
use Twig\Compiler;
16+
use Twig\Extension\CoreExtension;
1617
use Twig\Node\Expression\AbstractExpression;
1718
use Twig\Node\Node;
1819

@@ -43,6 +44,17 @@ public function compile(Compiler $compiler): void
4344
{
4445
$compiler->addDebugInfo($this);
4546

47+
// since twig/twig 3.9.0: Using the internal "twig_to_array" function is deprecated.
48+
$compiler
49+
->write('$toArray = function ($data) {')
50+
;
51+
52+
if (method_exists(CoreExtension::class, 'toArray')) {
53+
$compiler->write('return Twig\Extension\CoreExtension::toArray($data);};');
54+
} else {
55+
$compiler->write('return twig_to_array($data);};');
56+
}
57+
4658
/*
4759
* Block 1) PreCreateForRender handling
4860
*
@@ -55,7 +67,7 @@ public function compile(Compiler $compiler): void
5567
->raw(']->extensionPreCreateForRender(')
5668
->string($this->getAttribute('component'))
5769
->raw(', ')
58-
->raw('twig_to_array(')
70+
->raw('$toArray(')
5971
;
6072
$this->writeProps($compiler)
6173
->raw(')')
@@ -86,7 +98,7 @@ public function compile(Compiler $compiler): void
8698
->string(ComponentExtension::class)
8799
->raw(']->startEmbeddedComponentRender(')
88100
->string($this->getAttribute('component'))
89-
->raw(', twig_to_array(')
101+
->raw(', $toArray(')
90102
;
91103
$this->writeProps($compiler)
92104
->raw('), ')

0 commit comments

Comments
 (0)