Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feature #30719 [Mime] Add BodyRendererInterface (fabpot)
This PR was merged into the 4.3-dev branch.

Discussion
----------

[Mime] Add BodyRendererInterface

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

<!--
Write a short README entry for your feature/bugfix here (replace this comment block.)
This will help people understand your PR and can be used as a start of the Doc PR.
Additionally:
 - Bug fixes must be submitted against the lowest branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against the master branch.
-->

Commits
-------

0c9d684 [Mime] added BodyRendererInterface
  • Loading branch information
fabpot committed Mar 27, 2019
2 parents a35ad63 + 0c9d684 commit 93018de
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
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
@@ -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;
}

0 comments on commit 93018de

Please sign in to comment.