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
-------
0c9d684d0a
[Mime] added BodyRendererInterface
This commit is contained in:
commit
93018de198
@ -12,6 +12,8 @@
|
||||
namespace Symfony\Bridge\Twig\Mime;
|
||||
|
||||
use League\HTMLToMarkdown\HtmlConverter;
|
||||
use Symfony\Component\Mime\BodyRendererInterface;
|
||||
use Symfony\Component\Mime\Message;
|
||||
use Twig\Environment;
|
||||
|
||||
/**
|
||||
@ -19,7 +21,7 @@ use Twig\Environment;
|
||||
*
|
||||
* @experimental in 4.3
|
||||
*/
|
||||
final class Renderer
|
||||
final class BodyRenderer implements BodyRendererInterface
|
||||
{
|
||||
private $twig;
|
||||
private $context;
|
||||
@ -38,48 +40,48 @@ final class Renderer
|
||||
}
|
||||
}
|
||||
|
||||
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)) {
|
22
src/Symfony/Component/Mime/BodyRendererInterface.php
Normal file
22
src/Symfony/Component/Mime/BodyRendererInterface.php
Normal file
@ -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;
|
||||
}
|
Reference in New Issue
Block a user