8000 Merge branch '7.1' into 7.2 · symfony/symfony@2fe419e · GitHub
[go: up one dir, main page]

Skip to content

Commit 2fe419e

Browse files
committed
Merge branch '7.1' into 7.2
* 7.1: [TwigBridge] Remove `VersionAwareTest` from `AbstractLayoutTestCase` [DependencyInjection] Add coverage for error cases of `LazyClosure` and `AutowireLocator` [TwigBridge] Fixed a parameterized choice label translation Fix extracting of message from ->trans() method with named params [TwigBridge] Remove usage of Node() instantiations Update security.bg.xlf [Dotenv] Default value can be empty [Emoji] Update data to support emoji 16 Add missing Albanian translations for Security and Validator components [HttpClient] Add `crypto_method` to scoped client options suppress proc_open errors [DependencyInjection] Fix `XmlFileLoader` not respecting when env for services
2 parents 8ad277a + 643f8d1 commit 2fe419e

File tree

170 files changed

+42554
-5495
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+42554
-5495
lines changed

src/Symfony/Bridge/Twig/Extension/FormExtension.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Form\FormError;
2020
use Symfony\Component\Form\FormRenderer;
2121
use Symfony\Component\Form\FormView;
22+
use Symfony\Contracts\Translation\TranslatableInterface;
2223
use Symfony\Contracts\Translation\TranslatorInterface;
2324
use Twig\Extension\AbstractExtension;
2425
use Twig\TwigFilter;
@@ -147,23 +148,26 @@ public function getFieldChoices(FormView $view): iterable
147148
private function createFieldChoicesList(iterable $choices, string|false|null $translationDomain): iterable
148149
{
149150
foreach ($choices as $choice) {
150-
$translatableLabel = $this->createFieldTranslation($choice->label, [], $translationDomain);
151-
152151
if ($choice instanceof ChoiceGroupView) {
152+
$translatableLabel = $this->createFieldTranslation($choice->label, [], $translationDomain);
153153
yield $translatableLabel => $this->createFieldChoicesList($choice, $translationDomain);
154154

155155
continue;
156156
}
157157

158158
/* @var ChoiceView $choice */
159+
$translatableLabel = $this->createFieldTranslation($choice->label, $choice->labelTranslationParameters, $translationDomain);
159160
yield $translatableLabel => $choice->value;
160161
}
161162
}
162163

163-
private function createFieldTranslation(?string $value, array $parameters, string|false|null $domain): ?string
164+
private function createFieldTranslation(TranslatableInterface|string|null $value, array $parameters, string|false|null $domain): ?string
164165
{
165166
if (!$this->translator || !$value || false === $domain) {
166-
return $value;
167+
return null !== $value ? (string) $value : null;
168+
}
169+
if ($value instanceof TranslatableInterface) {
170+
return $value->trans($this->translator);
167171
}
168172

169173
return $this->translator->trans($value, $parameters, $domain);

src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Twig\Node\Expression\NameExpression;
2323
use Twig\Node\ModuleNode;
2424
use Twig\Node\Node;
25+
use Twig\Node\Nodes;
2526
use Twig\Node\SetNode;
2627
use Twig\NodeVisitor\NodeVisitorInterface;
2728

@@ -55,7 +56,12 @@ public function enterNode(Node $node, Environment $env): Node
5556
$name = new AssignNameExpression(self::INTERNAL_VAR_NAME, $node->getTemplateLine());
5657
$this->scope->set('domain', new NameExpression(self::INTERNAL_VAR_NAME, $node->getTemplateLine()));
5758

58-
return new SetNode(false, new Node([$name]), new Node([$node->getNode('expr')]), $node->getTemplateLine());
59+
60+
if (class_exists(Nodes::class)) {
61+
return new SetNode(false, new Nodes([$name]), new Nodes([$node->getNode('expr')]), $node->getTemplateLine());
62+
} else {
63+
return new SetNode(false, new Node([$name]), new Node([$node->getNode('expr')]), $node->getTemplateLine());
64+
}
5965
}
6066

6167
if (!$this->scope->has('domain')) {

src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionFieldHelpersTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Form\FormError;
2020
use Symfony\Component\Form\FormView;
2121
use Symfony\Component\Form\Test\FormIntegrationTestCase;
22+
use Symfony\Component\Translation\TranslatableMessage;
2223

2324
class FormExtensionFieldHelpersTest extends FormIntegrationTestCase
2425
{
@@ -81,6 +82,28 @@ protected function setUp(): void
8182
'expanded' => true,
8283
'label' => false,
8384
])
85+
->add('parametrized_choice_label', ChoiceType::class, [
86+
'choices' => [
87+
(object) ['value' => 'yes', 'label' => 'parametrized.%yes%'],
88+
(object) ['value' => 'no', 'label' => 'parametrized.%no%'],
89+
],
90+
'choice_value' => 'value',
91+
'choice_label' => 'label',
92+
'choice_translation_domain' => 'forms',
93+
'choice_translation_parameters' => [
94+
['%yes%' => 'YES'],
95+
['%no%' => 'NO'],
96+
],
97+
])
98+
->add('translatable_choice_label', ChoiceType::class, [
99+
'choices' => [
100+
'yes',
101+
'no',
102+
],
103+
'choice_label' => static function (string $choice) {
104+
return new TranslatableMessage('parametrized.%value%', ['%value%' => $choice], 'forms');
105+
},
106+
])
84107
->getForm()
85108
;
86109

@@ -290,4 +313,40 @@ public function testFieldTranslatedChoicesMultiple()
290313
$this->assertSame('salt', $choicesArray[1]['value']);
291314
$this->assertSame('[trans]base.salt[/trans]', $choicesArray[1]['label']);
292315
}
316+
317+
public function testChoiceParametrizedLabel()
318+
{
319+
$choices = $this->translatorExtension->getFieldChoices($this->view->children['parametrized_choice_label']);
320+
321+
$choicesArray = [];
322+
foreach ($choices as $label => $value) {
323+
$choicesArray[] = ['label' => $label, 'value' => $value];
324+
}
325+
326+
$this->assertCount(2, $choicesArray);
327+
328+
$this->assertSame('yes', $choicesArray[0]['value']);
329+
$this->assertSame('[trans]parametrized.YES[/trans]', $choicesArray[0]['label']);
330+
331+
$this->assertSame('no', $choicesArray[1]['value']);
332+
$this->assertSame('[trans]parametrized.NO[/trans]', $choicesArray[1]['label']);
333+
}
334+
335+
public function testChoiceTranslatableLabel()
336+
{
337+
$choices = $this->translatorExtension->getFieldChoices($this->view->children['translatable_choice_label']);
338+
339+
$choicesArray = [];
340+
foreach ($choices as $label => 10000 $value) {
341+
$choicesArray[] = ['label' => $label, 'value' => $value];
342+
}
343+
344+
$this->assertCount(2, $choicesArray);
345+
346+
$this->assertSame('yes', $choicesArray[0]['value']);
347+
$this->assertSame('[trans]parametrized.yes[/trans]', $choicesArray[0]['label']);
348+
349+
$this->assertSame('no', $choicesArray[1]['value']);
350+
$this->assertSame('[trans]parametrized.no[/trans]', $choicesArray[1]['label']);
351+
}
293352
}

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Twig\Loader\LoaderInterface;
1919
use Twig\Node\Expression\NameExpression;
2020
use Twig\Node\Node;
21+
use Twig\Node\Nodes;
2122

2223
class DumpNodeTest extends TestCase
2324
{
@@ -71,9 +72,16 @@ public function testIndented()
7172

7273
public function testOneVar()
7374
{
74-
$vars = new Node([
75-
new NameExpression('foo', 7),
76-
]);
75+
if (class_exists(Nodes::class)) {
76+
$vars = new Nodes([
77+
new NameExpression('foo', 7),
78+
]);
79+
} else {
80+
$vars = new Node([
81+
new NameExpression('foo', 7),
82+
]);
83+
}
84+
7785
$node = new DumpNode('bar', $vars, 7);
7886

7987
$env = new Environment($this->createMock(LoaderInterface::class));
@@ -94,10 +102,18 @@ public function testOneVar()
94102

95103
public function testMultiVars()
96104
{
97-
$vars = new Node([
98-
new NameExpression('foo', 7),
99-
new NameExpression('bar', 7),
100-
]);
105+
if (class_exists(Nodes::class)) {
106+
$vars = new Nodes([
107+
new NameExpression('foo', 7),
108+
new NameExpression('bar', 7),
109+
]);
110+
} else {
111+
$vars = new Node([
112+
new NameExpression('foo', 7),
113+
new NameExpression('bar', 7),
114+
]);
115+
}
116+
101117
$node = new DumpNode('bar', $vars, 7);
102118

103119
$env = new Environment($this->createMock(LoaderInterface::class));

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Twig\Node\Expression\ConstantExpression;
2424
use Twig\Node\Expression\NameExpression;
2525
use Twig\Node\Node;
26+
use Twig\Node\Nodes;
2627

2728
class FormThemeTest extends TestCase
2829
{
@@ -31,10 +32,17 @@ class FormThemeTest extends TestCase
3132
public function testConstructor()
3233
{
3334
$form = new NameExpression('form', 0);
34-
$resources = new Node([
35-
new ConstantExpression('tpl1', 0),
36-
new ConstantExpression('tpl2', 0),
37-
]);
35+
if (class_exists(Nodes::class)) {
36+
$resources = new Nodes([
37+
new ConstantExpression('tpl1', 0),
38+
new ConstantExpression('tpl2', 0),
39+
]);
40+
} else {
41+
$resources = new Node([
42+
new ConstantExpression('tpl1', 0),
43+
new ConstantExpression('tpl2', 0),
44+
]);
45+
}
3846

3947
$node = new FormThemeNode($form, $resources, 0);
4048

0 commit comments

Comments
 (0)
0