Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 54b96d3

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

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

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

Lines changed: 14 additions & 2 deletions
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;
@@ -49,8 +50,14 @@ public function compile(Compiler $compiler): void
4950
// of variables at compile time.
5051
$labelIsExpression = false;
5152

53+
if (method_exists(CoreExtension::class, 'testEmpty')) {
54+
$isEmpty = CoreExtension::testEmpty($label->getAttribute('value'));
55+
} else {
56+
$isEmpty = twig_test_empty($label->getAttribute('value'));
57+
}
58+
5259
// Only insert the label into the array if it is not empty
53-
if (!twig_test_empty($label->getAttribute('value'))) {
60+
if (!$isEmpty) {
5461
$originalVariables = $variables;
5562
$variables = new ArrayExpression([], $lineno);
5663
$labelKey = new ConstantExpression('label', $lineno);
@@ -97,7 +104,12 @@ public function compile(Compiler $compiler): void
97104

98105
// Check at runtime whether the label is empty.
99106
// If not, add it to the array at runtime.
100-
$compiler->raw('(twig_test_empty($_label_ = ');
107+
if (method_exists(CoreExtension::class, 'testEmpty')) {
108+
$compiler->raw('(CoreExtension::testEmpty($_label_ = ');
109+
} else {
110+
$compiler->raw('(twig_test_empty($_label_ = ');
111+
}
112+
101113
$compiler->subcompile($label);
102114
$compiler->raw(') ? [] : ["label" => $_label_])');
103115
}

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

Lines changed: 11 additions & 14 deletions
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;
@@ -224,13 +225,11 @@ public function testCompileLabelWithLabelThatEvaluatesToNull()
224225
// "label" => null must not be included in the output!
225226
// Otherwise the default label is overwritten with null.
226227
// https://github.com/symfony/symfony/issues/5029
227-
$this->assertEquals(
228-
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')
231-
),
232-
trim($compiler->compile($node)->getSource())
233-
);
228+
if (method_exists(CoreExtension::class, 'testEmpty')) {
229+
$this->assertEquals(sprintf('$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', (Twig\Extension\CoreExtension::testEmpty($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))', $this->getVariableGetter('form')), trim($compiler->compile($node)->getSource()));
230+
} else {
231+
$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')), trim($compiler->compile($node)->getSource()));
232+
}
234233
}
235234

236235
public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
@@ -261,13 +260,11 @@ public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
261260
// "label" => null must not be included in the output!
262261
// Otherwise the default label is overwritten with null.
263262
// https://github.com/symfony/symfony/issues/5029
264-
$this->assertEquals(
265-
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-
),
269-
trim($compiler->compile($node)->getSource())
270-
);
263+
if (method_exists(CoreExtension::class, 'testEmpty')) {
264+
$this->assertEquals(sprintf('$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', ["foo" => "bar", "label" => "value in attributes"] + (Twig\Extension\CoreExtension::testEmpty($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))', $this->getVariableGetter('form')), trim($compiler->compile($node)->getSource()));
265+
} else {
266+
$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')), trim($compiler->compile($node)->getSource()));
267+
}
271268
}
272269

273270
protected function getVariableGetter($name)

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

Lines changed: 0 additions & 5 deletions
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

Lines changed: 12 additions & 8 deletions
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,17 @@ 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+
return twig_escape_filter($env, $s);
122+
}
119123
}

0 commit comments

Comments
 (0)