Skip to content

[Mime] Add BodyRendererInterface #30719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
namespace Symfony\Bridge\Twig\Mime;

use League\HTMLToMarkdown\HtmlConverter;
use Symfony\Component\Mime\BodyRendererInterface;
use Symfony\Component\Mime\Message;
use Twig\Environment;

/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @experimental in 4.3
*/
final class Renderer
final class BodyRenderer implements BodyRendererInterface
{
private $twig;
private $context;
Expand All @@ -38,48 +40,48 @@ public function __construct(Environment $twig, array $context = [])
}
}

public function render(TemplatedEmail $email): TemplatedEmail
public function render(Message $message): void
{
$email = clone $email;
if (!$message instanceof TemplatedEmail) {
return;
}

$vars = array_merge($this->context, $email->getContext(), [
'email' => new WrappedTemplatedEmail($this->twig, $email),
$vars = array_merge($this->context, $message->getContext(), [
'email' => new WrappedTemplatedEmail($this->twig, $message),
]);

if ($template = $email->getTemplate()) {
$this->renderFull($email, $template, $vars);
if ($template = $message->getTemplate()) {
$this->renderFull($message, $template, $vars);
}

if ($template = $email->getTextTemplate()) {
$email->text($this->twig->render($template, $vars));
if ($template = $message->getTextTemplate()) {
$message->text($this->twig->render($template, $vars));
}

if ($template = $email->getHtmlTemplate()) {
$email->html($this->twig->render($template, $vars));
if ($template = $message->getHtmlTemplate()) {
$message->html($this->twig->render($template, $vars));
}

// if text body is empty, compute one from the HTML body
if (!$email->getTextBody() && null !== $html = $email->getHtmlBody()) {
$email->text($this->convertHtmlToText(\is_resource($html) ? stream_get_contents($html) : $html));
if (!$message->getTextBody() && null !== $html = $message->getHtmlBody()) {
$message->text($this->convertHtmlToText(\is_resource($html) ? stream_get_contents($html) : $html));
}

return $email;
}

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

if ($template->hasBlock('subject', $vars)) {
$email->subject($template->renderBlock('subject', $vars));
$message->subject($template->renderBlock('subject', $vars));
}

if ($template->hasBlock('text', $vars)) {
$email->text($template->renderBlock('text', $vars));
$message->text($template->renderBlock('text', $vars));
}

if ($template->hasBlock('html', $vars)) {
$email->html($template->renderBlock('html', $vars));
$message->html($template->renderBlock('html', $vars));
}

if ($template->hasBlock('config', $vars)) {
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/Mime/BodyRendererInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mime;

/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @experimental in 4.3
*/
interface BodyRendererInterface
{
public function render(Message $message): void;
}