8000 [Mime] added BodyRendererInterface · symfony/symfony@0c9d684 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c9d684

Browse files
committed
[Mime] added BodyRendererInterface
1 parent 19c6639 commit 0c9d684

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

src/Symfony/Bridge/Twig/Mime/Renderer.php renamed to src/Symfony/Bridge/Twig/Mime/BodyRenderer.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
namespace Symfony\Bridge\Twig\Mime;
1313

1414
use League\HTMLToMarkdown\HtmlConverter;
15+
use Symfony\Component\Mime\BodyRendererInterface;
16+
use Symfony\Component\Mime\Message;
1517
use Twig\Environment;
1618

1719
/**
1820
* @author Fabien Potencier <fabien@symfony.com>
1921
*
2022
* @experimental in 4.3
2123
*/
22-
final class Renderer
24+
final class BodyRenderer implements BodyRendererInterface
2325
{
2426
private $twig;
2527
private $context;
@@ -38,48 +40,48 @@ public function __construct(Environment $twig, array $context = [])
3840
}
3941
}
4042

41-
public function render(TemplatedEmail $email): TemplatedEmail
43+
public function render(Message $message): void
4244
{
43-
$email = clone $email;
45+
if (!$message instanceof TemplatedEmail) {
46+
return;
47+
}
4448

45-
$vars = array_merge($this->context, $email->getContext(), [
46-
'email' => new WrappedTemplatedEmail($this->twig, $email),
49+
$vars = array_merge($this->context, $message->getContext(), [
50+
'email' => new WrappedTemplatedEmail($this->twig, $message),
4751
]);
4852

49-
if ($template = $email->getTemplate()) {
50-
$this->renderFull($email, $template, $vars);
53+
if ($template = $message->getTemplate()) {
54+
$this->renderFull($message, $template, $vars);
5155
}
5256

53-
if ($template = $email->getTextTemplate()) {
54-
$email->text($this->twig->render($template, $vars));
57+
if ($template = $message->getTextTemplate()) {
58+
$message->text($this->twig->render($template, $vars));
5559
}
5660

57-
if ($template = $email->getHtmlTemplate()) {
58-
$email->html($this->twig->render($template, $vars));
61+
if ($template = $message->getHtmlTemplate()) {
62+
$message->html($this->twig->render($template, $vars));
5963
}
6064

6165
// if text body is empty, compute one from the HTML body
62-
if (!$email->getTextBody() && null !== $html = $email->getHtmlBody()) {
63-
$email->text($this->convertHtmlToText(\is_resource($html) ? stream_get_contents($html) : $html));
66+
if (!$message->getTextBody() && null !== $html = $message->getHtmlBody()) {
67+
$message->text($this->convertHtmlToText(\is_resource($html) ? stream_get_contents($html) : $html));
6468
}
65-
66-
return $email;
6769
}
6870

69-
private function renderFull(TemplatedEmail $email, string $template, array $vars): void
71+
private function renderFull(TemplatedEmail $message, string $template, array $vars): void
7072
{
7173
$template = $this->twig->load($template);
7274

7375
if ($template->hasBlock('subject', $vars)) {
74-
$email->subject($template->renderBlock('subject', $vars));
76+
$message->subject($template->renderBlock('subject', $vars));
7577
}
7678

7779
if ($template->hasBlock('text', $vars)) {
78-
$email->text($template->renderBlock('text', $vars));
80+
$message->text($template->renderBlock('text', $vars));
7981
}
8082

8183
if ($template->hasBlock('html', $vars)) {
82-
$email->html($template->renderBlock('html', $vars));
84+
$message->html($template->renderBlock('html', $vars));
8385
}
8486

8587
if ($template->hasBlock('config', $vars)) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime;
13+
14+
/**
15+
* @author Fabien Potencier <fabien@symfony.com>
16+
*
17+
* @experimental in 4.3
18+
*/
19+
interface BodyRendererInterface
20+
{
21+
public function render(Message $message): void;
22+
}

0 commit comments

Comments
 (0)
0