Skip to content

Commit 7f65a27

Browse files
committed
Merge branch '5.2' into 5.x
* 5.2: bug #40427 [Console] Stop accepting ints as InputOption defaults Fix fingerprint when context is not serializable Fix `ConstraintViolation#getMessageTemplate()` to always return `string`
2 parents 906b609 + 950e144 commit 7f65a27

File tree

6 files changed

+65
-12
lines changed

6 files changed

+65
-12
lines changed

src/Symfony/Bridge/Twig/Mime/BodyRenderer.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function render(Message $message): void
4949

5050
$previousRenderingKey = $messageContext[__CLASS__] ?? null;
5151
unset($messageContext[__CLASS__]);
52-
$currentRenderingKey = md5(serialize([$messageContext, $message->getTextTemplate(), $message->getHtmlTemplate()]));
52+
$currentRenderingKey = $this->getFingerPrint($message);
5353
if ($previousRenderingKey === $currentRenderingKey) {
5454
return;
5555
}
@@ -77,6 +77,23 @@ public function render(Message $message): void
7777
$message->context($message->getContext() + [__CLASS__ => $currentRenderingKey]);
7878
}
7979

80+
private function getFingerPrint(TemplatedEmail $message): string
81+
{
82+
$messageContext = $message->getContext();
83+
unset($messageContext[__CLASS__]);
84+
85+
$payload = [$messageContext, $message->getTextTemplate(), $message->getHtmlTemplate()];
86+
try {
87+
$serialized = serialize($payload);
88+
} catch (\Exception $e) {
89+
// Serialization of 'Closure' is not allowed
90+
// Happens when context contain a closure, in that case, we assume that context always change.
91+
$serialized = random_bytes(8);
92+
}
93+
94+
return md5($serialized);
95+
}
96+
8097
private function convertHtmlToText(string $html): string
8198
{
8299
if (null !== $this->converter) {

src/Symfony/Bridge/Twig/Tests/Mime/BodyRendererTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,27 @@ public function testRenderedOnce()
100100
$this->assertEquals('reset', $email->getTextBody());
101101
}
102102

103+
public function testRenderedOnceUnserializableContext()
104+
{
105+
$twig = new Environment(new ArrayLoader([
106+
'text' => 'Text',
107+
]));
108+
$renderer = new BodyRenderer($twig);
109+
$email = (new TemplatedEmail())
110+
->to('fabien@symfony.com')
111+
->from('helene@symfony.com')
112+
;
113+
$email->textTemplate('text');
114+
$email->context([
115+
'foo' => static function () {
116+
return 'bar';
117+
},
118+
]);
119+
120+
$renderer->render($email);
121+
$this->assertEquals('Text', $email->getTextBody());
122+
}
123+
103124
private function prepareEmail(?string $text, ?string $html, array $context = []): TemplatedEmail
104125
{
105126
$twig = new Environment(new ArrayLoader([

src/Symfony/Component/Console/Command/Command.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,9 @@ public function addArgument(string $name, int $mode = null, string $description
429429
/**
430430
* Adds an option.
431431
*
432-
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
433-
* @param int|null $mode The option mode: One of the InputOption::VALUE_* constants
434-
* @param string|string[]|int|bool|null $default The default value (must be null for InputOption::VALUE_NONE)
432+
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
433+
* @param int|null $mode The option mode: One of the InputOption::VALUE_* constants
434+
* @param string|string[]|bool|null $default The default value (must be null for InputOption::VALUE_NONE)
435435
*
436436
* @throws InvalidArgumentException If option mode is invalid or incompatible
437437
*

src/Symfony/Component/Console/Input/InputOption.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ class InputOption
3434
private $description;
3535

3636
/**
37-
* @param string $name The option name
38-
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
39-
* @param int|null $mode The option mode: One of the VALUE_* constants
40-
* @param string $description A description text
41-
* @param string|string[]|int|bool|null $default The default value (must be null for self::VALUE_NONE)
37+
* @param string $name The option name
38+
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
39+
* @param int|null $mode The option mode: One of the VALUE_* constants
40+
* @param string $description A description text
41+
* @param string|string[]|bool|null $default The default value (must be null for self::VALUE_NONE)
4242
*
4343
* @throws InvalidArgumentException If option mode is invalid or incompatible
4444
*/
@@ -158,7 +158,7 @@ public function isNegatable(): bool
158158
/**
159159
* Sets the default value.
160160
*
161-
* @param string|string[]|int|bool|null $default The default value
161+
* @param string|string[]|bool|null $default The default value
162162
*
163163
* @throws LogicException When incorrect default value is given
164164
*/
@@ -185,7 +185,7 @@ public function setDefault($default = null)
185185
/**
186186
* Returns the default value.
187187
*
188-
* @return string|string[]|int|bool|null The default value
188+
* @return string|string[]|bool|null The default value
189189
*/
190190
public function getDefault()
191191
{

src/Symfony/Component/Validator/ConstraintViolation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function __toString()
100100
*/
101101
public function getMessageTemplate()
102102
{
103-
return $this->messageTemplate;
103+
return (string) $this->messageTemplate;
104104
}
105105

106106
/**

src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,19 @@ public function testRetrievedPropertyPathIsAStringEvenIfNotSet()
171171
))->getPropertyPath()
172172
);
173173
}
174+
175+
public function testRetrievedMessageTemplateIsAStringEvenIfNotSet()
176+
{
177+
self::assertSame(
178+
'',
179+
(new ConstraintViolation(
180+
'irrelevant',
181+
null,
182+
[],
183+
'irrelevant',
184+
'irrelevant',
185+
null
186+
))->getMessageTemplate()
187+
);
188+
}
174189
}

0 commit comments

Comments
 (0)