Skip to content

Commit

Permalink
bug #1337 Fix twig_to_array is deprecated (WebMamba)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 2.x branch.

Discussion
----------

Fix twig_to_array is deprecated

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| Issues        |
| License       | MIT

Commits
-------

2a2ab7c Fix twig_to_array is deprecated
  • Loading branch information
kbond committed Dec 26, 2023
2 parents 524ac5a + 2a2ab7c commit 4cee6ac
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/TwigComponent/src/Twig/ComponentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\UX\TwigComponent\BlockStack;
use Twig\Compiler;
use Twig\Extension\CoreExtension;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Node;

Expand Down Expand Up @@ -43,6 +44,17 @@ public function compile(Compiler $compiler): void
{
$compiler->addDebugInfo($this);

// since twig/twig 3.9.0: Using the internal "twig_to_array" function is deprecated.
$compiler
->write('$toArray = function ($data) {')
;

if (method_exists(CoreExtension::class, 'toArray')) {
$compiler->write('return Twig\Extension\CoreExtension::toArray($data);};');
} else {
$compiler->write('return twig_to_array($data);};');
}

/*
* Block 1) PreCreateForRender handling
*
Expand All @@ -55,7 +67,7 @@ public function compile(Compiler $compiler): void
->raw(']->extensionPreCreateForRender(')
->string($this->getAttribute('component'))
->raw(', ')
->raw('twig_to_array(')
->raw('$toArray(')
;
$this->writeProps($compiler)
->raw(')')
Expand Down Expand Up @@ -86,7 +98,7 @@ public function compile(Compiler $compiler): void
->string(ComponentExtension::class)
->raw(']->startEmbeddedComponentRender(')
->string($this->getAttribute('component'))
->raw(', twig_to_array(')
->raw(', $toArray(')
;
$this->writeProps($compiler)
->raw('), ')
Expand Down

0 comments on commit 4cee6ac

Please sign in to comment.