Skip to content

Commit fa6729e

Browse files
committed
Merge branch '5.2' into 5.x
* 5.2: Fix #36973: Command description consistency Render email once
2 parents 38b0bbd + 9936be6 commit fa6729e

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

Command/DebugCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
class DebugCommand extends Command
3434
{
3535
protected static $defaultName = 'debug:twig';
36-
protected static $defaultDescription = 'Shows a list of twig functions, filters, globals and tests';
36+
protected static $defaultDescription = 'Show a list of twig functions, filters, globals and tests';
3737

3838
private $twig;
3939
private $projectDir;

Command/LintCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
class LintCommand extends Command
3636
{
3737
protected static $defaultName = 'lint:twig';
38-
protected static $defaultDescription = 'Lints a Twig template and outputs encountered errors';
38+
protected static $defaultDescription = 'Lint a Twig template and outputs encountered errors';
3939

4040
private $twig;
4141

Mime/BodyRenderer.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public function render(Message $message): void
4646
}
4747

4848
$messageContext = $message->getContext();
49+
50+
$previousRenderingKey = $messageContext[__CLASS__] ?? null;
51+
unset($messageContext[__CLASS__]);
52+
$currentRenderingKey = md5(serialize([$messageContext, $message->getTextTemplate(), $message->getHtmlTemplate()]));
53+
if ($previousRenderingKey === $currentRenderingKey) {
54+
return;
55+
}
56+
4957
if (isset($messageContext['email'])) {
5058
throw new InvalidArgumentException(sprintf('A "%s" context cannot have an "email" entry as this is a reserved variable.', get_debug_type($message)));
5159
}
@@ -66,6 +74,7 @@ public function render(Message $message): void
6674
if (!$message->getTextBody() && null !== $html = $message->getHtmlBody()) {
6775
$message->text($this->convertHtmlToText(\is_resource($html) ? stream_get_contents($html) : $html));
6876
}
77+
$message->context($message->getContext() + [__CLASS__ => $currentRenderingKey]);
6978
}
7079

7180
private function convertHtmlToText(string $html): string

Tests/Mime/BodyRendererTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,27 @@ public function testRenderWithContextReservedEmailEntry()
7979
$this->prepareEmail('Text', '', ['email' => 'reserved!']);
8080
}
8181

82+
public function testRenderedOnce()
83+
{
84+
$twig = new Environment(new ArrayLoader([
85+
'text' => 'Text',
86+
]));
87+
$renderer = new BodyRenderer($twig);
88+
$email = (new TemplatedEmail())
89+
90+
91+
;
92+
$email->textTemplate('text');
93+
94+
$renderer->render($email);
95+
$this->assertEquals('Text', $email->getTextBody());
96+
97+
$email->text('reset');
98+
99+
$renderer->render($email);
100+
$this->assertEquals('reset', $email->getTextBody());
101+
}
102+
82103
private function prepareEmail(?string $text, ?string $html, array $context = []): TemplatedEmail
83104
{
84105
$twig = new Environment(new ArrayLoader([

0 commit comments

Comments
 (0)