8000 [TwigBridge] don't use deprecated and internal Twig functions by xabbuh · Pull Request #52990 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBridge] don't use deprecated and internal Twig functions #52990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Symfony/Bridge/Twig/Node/SearchAndRenderBlockNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bridge\Twig\Node;

use Twig\Compiler;
use Twig\Extension\CoreExtension;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FunctionExpression;
Expand Down Expand Up @@ -50,7 +51,7 @@ public function compile(Compiler $compiler): void
$labelIsExpression = false;

// Only insert the label into the array if it is not empty
if (!twig_test_empty($label->getAttribute('value'))) {
if (null !== $label->getAttribute('value') && false !== $label->getAttribute('value') && '' !== (string) $label->getAttribute('value')) {
$originalVariables = $variables;
$variables = new ArrayExpression([], $lineno);
$labelKey = new ConstantExpression('label', $lineno);
Expand Down Expand Up @@ -97,7 +98,12 @@ public function compile(Compiler $compiler): void

// Check at runtime whether the label is empty.
// If not, add it to the array at runtime.
$compiler->raw('(twig_test_empty($_label_ = ');
if (method_exists(CoreExtension::class, 'testEmpty')) {
$compiler->raw('(CoreExtension::testEmpty($_label_ = ');
} else {
$compiler->raw('(twig_test_empty($_label_ = ');
}

$compiler->subcompile($label);
$compiler->raw(') ? [] : ["label" => $_label_])');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode;
use Twig\Compiler;
use Twig\Environment;
use Twig\Extension\CoreExtension;
use Twig\Loader\LoaderInterface;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConditionalExpression;
Expand Down Expand Up @@ -226,8 +227,9 @@ public function testCompileLabelWithLabelThatEvaluatesToNull()
// https://github.com/symfony/symfony/issues/5029
$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))',
$this->getVariableGetter('form')
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', (%s($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))',
$this->getVariableGetter('form'),
method_exists(CoreExtension::class, 'testEmpty') ? 'CoreExtension::testEmpty' : 'twig_test_empty'
),
trim($compiler->compile($node)->getSource())
);
Expand Down Expand Up @@ -263,8 +265,9 @@ public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
// https://github.com/symfony/symfony/issues/5029
$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', ["foo" => "bar", "label" => "value in attributes"] + (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))',
$this->getVariableGetter('form')
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', ["foo" => "bar", "label" => "value in attributes"] + (%s($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))',
$this->getVariableGetter('form'),
method_exists(CoreExtension::class, 'testEmpty') ? 'CoreExtension::testEmpty' : 'twig_test_empty'
),
trim($compiler->compile($node)->getSource())
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use Symfony\Bundle\WebProfilerBundle\Twig\WebProfilerExtension;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Twig\Environment;
use Twig\Extension\CoreExtension;
use Twig\Extension\EscaperExtension;

class WebProfilerExtensionTest extends TestCase
{
Expand All @@ -25,9 +23,6 @@ class WebProfilerExtensionTest extends TestCase
*/
public function testDumpHeaderIsDisplayed(string $message, array $context, bool $dump1HasHeader, bool $dump2HasHeader)
{
class_exists(CoreExtension::class); // Load twig_convert_encoding()
class_exists(EscaperExtension::class); // Load twig_escape_filter()

$twigEnvironment = $this->mockTwigEnvironment();
$varCloner = new VarCloner();

Expand Down
21 changes: 13 additions & 8 deletions src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Twig\Environment;
use Twig\Extension\EscaperExtension;
use Twig\Extension\ProfilerExtension;
use Twig\Profiler\Profile;
use Twig\TwigFunction;
Expand Down Expand Up @@ -60,9 +61,6 @@ public function leave(Profile $profile): void
}
}

/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
Expand All @@ -87,12 +85,12 @@ public function dumpData(Environment $env, Data $data, int $maxDepth = 0)

public function dumpLog(Environment $env, string $message, Data $context = null)
{
$message = twig_escape_filter($env, $message);
$message = self::escape($env, $message);
$message = preg_replace('/&quot;(.*?)&quot;/', '&quot;<b>$1</b>&quot;', $message);

$replacements = [];
foreach ($context ?? [] as $k => $v) {
$k = '{'.twig_escape_filter($env, $k).'}';
$k = '{'.self::escape($env, $k).'}';
if (str_contains($message, $k)) {
< 7603 span class='blob-code-inner blob-code-marker ' data-code-marker=" "> $replacements[$k] = $v;
}
Expand All @@ -109,11 +107,18 @@ public function dumpLog(Environment $env, string $message, Data $context = null)
return '<span class="dump-inline">'.strtr($message, $replacements).'</span>';
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'profiler';
}

private static function escape(Environment $env, string $s): string
{
if (method_exists(EscaperExtension::class, 'escape')) {
return EscaperExtension::escape($env, $s);
}

// to be removed when support for Twig 3 is dropped
return twig_escape_filter($env, $s);
}
}
0