Skip to content

Instantly share code, notes, and snippets.

@ajgarlag
Last active February 8, 2021 08:24
Show Gist options
  • Save ajgarlag/77c793a394aaafaa1bee5d78aa6ad06c to your computer and use it in GitHub Desktop.
Save ajgarlag/77c793a394aaafaa1bee5d78aa6ad06c to your computer and use it in GitHub Desktop.
Backport symfony/symfony:#39733
diff --git a/Mime/BodyRenderer.php b/Mime/BodyRenderer.php
index 5e75a69..ed105cf 100644
--- a/Mime/BodyRenderer.php
+++ b/Mime/BodyRenderer.php
@@ -46,6 +46,14 @@ final class BodyRenderer implements BodyRendererInterface
}
$messageContext = $message->getContext();
+
+ $previousRenderingKey = $messageContext[__CLASS__] ?? null;
+ unset($messageContext[__CLASS__]);
+ $currentRenderingKey = md5(serialize([$messageContext, $message->getTextTemplate(), $message->getHtmlTemplate()]));
+ if ($previousRenderingKey === $currentRenderingKey) {
+ return;
+ }
+
if (isset($messageContext['email'])) {
throw new InvalidArgumentException(sprintf('A "%s" context cannot have an "email" entry as this is a reserved variable.', \get_class($message)));
}
@@ -66,6 +74,8 @@ final class BodyRenderer implements BodyRendererInterface
if (!$message->getTextBody() && null !== $html = $message->getHtmlBody()) {
$message->text($this->convertHtmlToText(\is_resource($html) ? stream_get_contents($html) : $html));
}
+
+ $message->context($message->getContext() + [__CLASS__ => $currentRenderingKey]);
}
private function convertHtmlToText(string $html): string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment