8000 don't use deprecated and internal Twig functions · symfony/symfony@25b14ba · GitHub
[go: up one dir, main page]

Skip to content

Commit 25b14ba

Browse files
committed
don't use deprecated and internal Twig functions
1 parent 1ae24df commit 25b14ba

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

src/Symfony/Bridge/Twig/Node/SearchAndRenderBlockNode.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Twig\Node;
1313

1414
use Twig\Compiler;
15+
use Twig\Extension\CoreExtension;
1516
use Twig\Node\Expression\ArrayExpression;
1617
use Twig\Node\Expression\ConstantExpression;
1718
use Twig\Node\Expression\FunctionExpression;
@@ -50,7 +51,7 @@ public function compile(Compiler $compiler): void
5051
$labelIsExpression = false;
5152

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

9899
// Check at runtime whether the label is empty.
99100
// If not, add it to the array at runtime.
100-
$compiler->raw('(twig_test_empty($_label_ = ');
101+
if (method_exists(CoreExtension::class, 'testEmpty')) {
102+
$compiler->raw('(CoreExtension::testEmpty($_label_ = ');
103+
} else {
104+
$compiler->raw('(twig_test_empty($_label_ = ');
105+
}
106+
101107
$compiler->subcompile($label);
102108
$compiler->raw(') ? [] : ["label" => $_label_])');
103109
}

src/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode;
1616
use Twig\Compiler;
1717
use Twig\Environment;
18+
use Twig\Extension\CoreExtension;
1819
use Twig\Loader\LoaderInterface;
1920
use Twig\Node\Expression\ArrayExpression;
2021
use Twig\Node\Expression\ConditionalExpression;
@@ -226,8 +227,9 @@ public function testCompileLabelWithLabelThatEvaluatesToNull()
226227
// https://github.com/symfony/symfony/issues/5029
227228
$this->assertEquals(
228229
sprintf(
229-
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))',
230-
$this->getVariableGetter('form')
230+
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', (%s($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))',
231+
$this->getVariableGetter('form'),
232+
method_exists(CoreExtension::class, 'testEmpty') ? 'CoreExtension::testEmpty' : 'twig_test_empty'
231233
),
232234
trim($compiler->compile($node)->getSource())
233235
);
@@ -263,8 +265,9 @@ public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
263265
// https://github.com/symfony/symfony/issues/5029
264266
$this->assertEquals(
265267
sprintf(
266-
'$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_]))',
267-
$this->getVariableGetter('form')
268+
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', ["foo" => "bar", "label" => "value in attributes"] + (%s($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))',
269+
$this->getVariableGetter('form'),
270+
method_exists(CoreExtension::class, 'testEmpty') ? 'CoreExtension::testEmpty' : 'twig_test_empty'
268271
),
269272
trim($compiler->compile($node)->getSource())
270273
);

src/Symfony/Bundle/WebProfilerBundle/Tests/Twig/WebProfilerExtensionTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use Symfony\Bundle\WebProfilerBundle\Twig\WebProfilerExtension;
1616
use Symfony\Component\VarDumper\Cloner\VarCloner;
1717
use Twig\Environment;
18-
use Twig\Extension\CoreExtension;
19-
use Twig\Extension\EscaperExtension;
2018

2119
class WebProfilerExtensionTest extends TestCase
2220
{
@@ -25,9 +23,6 @@ class WebProfilerExtensionTest extends TestCase
2523
*/
2624
public function testDumpHeaderIsDisplayed(string $message, array $context, bool $dump1HasHeader, bool $dump2HasHeader)
2725
{
28-
class_exists(CoreExtension::class); // Load twig_convert_encoding()
29-
class_exists(EscaperExtension::class); // Load twig_escape_filter()
30-
3126
$twigEnvironment = $this->mockTwigEnvironment();
3227
$varCloner = new VarCloner();
3328

src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php

+13-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\VarDumper\Cloner\Data;
1515
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
1616
use Twig\Environment;
17+
use Twig\Extension\EscaperExtension;
1718
use Twig\Extension\ProfilerExtension;
1819
use Twig\Profiler\Profile;
1920
use Twig\TwigFunction;
@@ -60,9 +61,6 @@ public function leave(Profile $profile): void
6061
}
6162
}
6263

63-
/**
64-
* {@inheritdoc}
65-
*/
6664
public function getFunctions(): array
6765
{
6866
return [
@@ -87,12 +85,12 @@ public function dumpData(Environment $env, Data $data, int $maxDepth = 0)
8785

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

9391
$replacements = [];
9492
foreach ($context ?? [] as $k => $v) {
95-
$k = '{'.twig_escape_filter($env, $k).'}';
93+
$k = '{'.self::escape($env, $k).'}';
9694
if (str_contains($message, $k)) {
9795
$replacements[$k] = $v;
9896
}
@@ -109,11 +107,18 @@ public function dumpLog(Environment $env, string $message, Data $context = null)
109107
return '<span class="dump-inline">'.strtr($message, $replacements).'</span>';
110108
}
111109

112-
/**
113-
* {@inheritdoc}
114-
*/
115110
public function getName()
116111
{
117112
return 'profiler';
118113
}
114+
115+
private static function escape(Environment $env, string $s): string
116+
{
117+
if (method_exists(EscaperExtension::class, 'escape')) {
118+
return EscaperExtension::escape($env, $s);
119+
}
120+
121+
// to be removed when support for Twig 3 is dropped
122+
return twig_escape_filter($env, $s);
123+
}
119124
}

0 commit comments

Comments
 (0)
0