Skip to content

Commit de7d7a9

Browse files
committed
feature #30853 [Twig] Remove TemplatedEmail::template() (fabpot)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Twig] Remove TemplatedEmail::template() | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes <!-- don't forget to update src/**/CHANGELOG.md files --> | 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 I propose to remove `TemplatedEmail::template()` for several reasons: * There is no real benefit over using `textTemplate` and `htmlTemplate` (ok, you only have one template instead of two... but the text template can only be automatically created based on the HTML one, so...); * It means having more than one way to do the same thing (do I set the subject on the object directly or in the template for instance); * A major drawback that is not easy to spot: the template is HTML, so the `subject` and `text` block must be carefully crafted to avoid avoid HTML escaping. Commits ------- 5e61b75 [Twig] removed TemplatedEmail::template()
2 parents 574097f + 5e61b75 commit de7d7a9

File tree

4 files changed

+7
-151
lines changed

4 files changed

+7
-151
lines changed

src/Symfony/Bridge/Twig/Mime/BodyRenderer.php

-27
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ public function render(Message $message): void
5050
'email' => new WrappedTemplatedEmail($this->twig, $message),
5151
]);
5252

53-
if ($template = $message->getTemplate()) {
54-
$this->renderFull($message, $template, $vars);
55-
}
56-
5753
if ($template = $message->getTextTemplate()) {
5854
$message->text($this->twig->render($template, $vars));
5955
}
@@ -68,29 +64,6 @@ public function render(Message $message): void
6864
}
6965
}
7066

71-
private function renderFull(TemplatedEmail $message, string $template, array $vars): void
72-
{
73-
$template = $this->twig->load($template);
74-
75-
if ($template->hasBlock('subject', $vars)) {
76-
$message->subject($template->renderBlock('subject', $vars));
77-
}
78-
79-
if ($template->hasBlock('text', $vars)) {
80-
$message->text($template->renderBlock('text', $vars));
81-
}
82-
83-
if ($template->hasBlock('html', $vars)) {
84-
$message->html($template->renderBlock('html', $vars));
85-
}
86-
87-
if ($template->hasBlock('config', $vars)) {
88-
// we discard the output as we're only interested
89-
// in the side effect of calling email methods
90-
$template->renderBlock('config', $vars);
91-
}
92-
}
93-
9467
private function convertHtmlToText(string $html): string
9568
{
9669
if (null !== $this->converter) {

src/Symfony/Bridge/Twig/Mime/TemplatedEmail.php

+2-18
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,10 @@
2020
*/
2121
class TemplatedEmail extends Email
2222
{
23-
private $template;
2423
private $htmlTemplate;
2524
private $textTemplate;
2625
private $context = [];
2726

28-
/**
29-
* @return $this
30-
*/
31-
public function template(?string $template)
32-
{
33-
$this->template = $template;
34-
35-
return $this;
36-
}
37-
3827
/**
3928
* @return $this
4029
*/
@@ -55,11 +44,6 @@ public function htmlTemplate(?string $template)
5544
return $this;
5645
}
5746

58-
public function getTemplate(): ?string
59-
{
60-
return $this->template;
61-
}
62-
6347
public function getTextTemplate(): ?string
6448
{
6549
return $this->textTemplate;
@@ -90,15 +74,15 @@ public function getContext(): array
9074
*/
9175
public function __serialize(): array
9276
{
93-
return [$this->template, $this->htmlTemplate, $this->textTemplate, $this->context, parent::__serialize()];
77+
return [$this->htmlTemplate, $this->textTemplate, $this->context, parent::__serialize()];
9478
}
9579

9680
/**
9781
* @internal
9882
*/
9983
public function __unserialize(array $data): void
10084
{
101-
[$this->template, $this->htmlTemplate, $this->textTemplate, $this->context, $parentData] = $data;
85+
[$this->htmlTemplate, $this->textTemplate, $this->context, $parentData] = $data;
10286

10387
parent::__unserialize($parentData);
10488
}

src/Symfony/Bridge/Twig/Tests/Mime/RendererTest.php

+5-103
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,20 @@
1515
use Symfony\Bridge\Twig\Mime\BodyRenderer;
1616
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
1717
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
18-
use Symfony\Component\Mime\Part\Multipart\MixedPart;
19-
use Symfony\Component\Mime\Part\Multipart\RelatedPart;
20-
use Symfony\Component\Mime\Part\TextPart;
2118
use Twig\Environment;
2219
use Twig\Loader\ArrayLoader;
2320

2421
class RendererTest extends TestCase
2522
{
2623
public function testRenderTextOnly(): void
2724
{
28-
$email = $this->prepareEmail(null, 'Text', null);
25+
$email = $this->prepareEmail('Text', null);
2926
$this->assertEquals('Text', $email->getBody()->bodyToString());
3027
}
3128

3229
public function testRenderHtmlOnly(): void
3330
{
34-
$email = $this->prepareEmail(null, null, '<b>HTML</b>');
31+
$email = $this->prepareEmail(null, '<b>HTML</b>');
3532
$body = $email->getBody();
3633
$this->assertInstanceOf(AlternativePart::class, $body);
3734
$this->assertEquals('HTML', $body->getParts()[0]->bodyToString());
@@ -40,7 +37,7 @@ public function testRenderHtmlOnly(): void
4037

4138
public function testRenderHtmlOnlyWithTextSet(): void
4239
{
43-
$email = $this->prepareEmail(null, null, '<b>HTML</b>');
40+
$email = $this->prepareEmail(null, '<b>HTML</b>');
4441
$email->text('Text');
4542
$body = $email->getBody();
4643
$this->assertInstanceOf(AlternativePart::class, $body);
@@ -50,118 +47,23 @@ public function testRenderHtmlOnlyWithTextSet(): void
5047

5148
public function testRenderTextAndHtml(): void
5249
{
53-
$email = $this->prepareEmail(null, 'Text', '<b>HTML</b>');
50+
$email = $this->prepareEmail('Text', '<b>HTML</b>');
5451
$body = $email->getBody();
5552
$this->assertInstanceOf(AlternativePart::class, $body);
5653
$this->assertEquals('Text', $body->getParts()[0]->bodyToString());
5754
$this->assertEquals('<b>HTML</b>', $body->getParts()[1]->bodyToString());
5855
}
5956

60-
public function testRenderFullOnly(): void
61-
{
62-
$email = $this->prepareEmail(<<<EOF
63-
{% block subject %}Subject{% endblock %}
64-
{% block text %}Text{% endblock %}
65-
{% block html %}<b>HTML</b>{% endblock %}
66-
EOF
67-
, null, null);
68-
$body = $email->getBody();
69-
$this->assertInstanceOf(AlternativePart::class, $body);
70-
$this->assertEquals('Subject', $email->getSubject());
71-
$this->assertEquals('Text', $body->getParts()[0]->bodyToString());
72-
$this->assertEquals('<b>HTML</b>', $body->getParts()[1]->bodyToString());
73-
}
74-
75-
public function testRenderFullOnlyWithTextOnly(): void
76-
{
77-
$email = $this->prepareEmail(<<<EOF
78-
{% block text %}Text{% endblock %}
79-
EOF
80-
, null, null);
81-
$body = $email->getBody();
82-
$this->assertInstanceOf(TextPart::class, $body);
83-
$this->assertEquals('', $email->getSubject());
84-
$this->assertEquals('Text', $body->bodyToString());
85-
}
86-
87-
public function testRenderFullOnlyWithHtmlOnly(): void
88-
{
89-
$email = $this->prepareEmail(<<<EOF
90-
{% block html %}<b>HTML</b>{% endblock %}
91-
EOF
92-
, null, null);
93-
$body = $email->getBody();
94-
$this->assertInstanceOf(AlternativePart::class, $body);
95-
$this->assertEquals('', $email->getSubject());
96-
$this->assertEquals('HTML', $body->getParts()[0]->bodyToString());
97-
$this->assertEquals('<b>HTML</b>', $body->getParts()[1]->bodyToString());
98-
}
99-
100-
public function testRenderFullAndText(): void
101-
{
102-
$email = $this->prepareEmail(<<<EOF
103-
{% block text %}Text full{% endblock %}
104-
{% block html %}<b>HTML</b>{% endblock %}
105-
EOF
106-
, 'Text', null);
107-
$body = $email->getBody();
108-
$this->assertInstanceOf(AlternativePart::class, $body);
109-
$this->assertEquals('Text', $body->getParts()[0]->bodyToString());
110-
$this->assertEquals('<b>HTML</b>', $body->getParts()[1]->bodyToString());
111-
}
112-
113-
public function testRenderFullAndHtml(): void
114-
{
115-
$email = $this->prepareEmail(<<<EOF
116-
{% block text %}Text full{% endblock %}
117-
{% block html %}<b>HTML</b>{% endblock %}
118-
EOF
119-
, null, '<i>HTML</i>');
120-
$body = $email->getBody();
121-
$this->assertInstanceOf(AlternativePart::class, $body);
122-
$this->assertEquals('Text full', $body->getParts()[0]->bodyToString());
123-
$this->assertEquals('<i>HTML</i>', $body->getParts()[1]->bodyToString());
124-
}
125-
126-
public function testRenderHtmlWithEmbeddedImages(): void
127-
{
128-
$email = $this->prepareEmail(null, null, '<img src="{{ email.image("image.jpg") }}" />');
129-
$body = $email->getBody();
130-
$this->assertInstanceOf(RelatedPart::class, $body);
131-
$this->assertInstanceOf(AlternativePart::class, $body->getParts()[0]);
132-
$this->assertStringMatchesFormat('<img src=3D"cid:%s@symfony" />', $body->getParts()[0]->getParts()[1]->bodyToString());
133-
$this->assertEquals('Some image data', base64_decode($body->getParts()[1]->bodyToString()));
134-
}
135-
136-
public function testRenderFullWithAttachments(): void
137-
{
138-
$email = $this->prepareEmail(<<<EOF
139-
{% block text %}Text{% endblock %}
140-
{% block config %}
141-
{% do email.attach('document.txt') %}
142-
{% endblock %}
143-
EOF
144-
, null, null);
145-
$body = $email->getBody();
146-
$this->assertInstanceOf(MixedPart::class, $body);
147-
$this->assertEquals('Text', $body->getParts()[0]->bodyToString());
148-
$this->assertEquals('Some text document...', base64_decode($body->getParts()[1]->bodyToString()));
149-
}
150-
151-
private function prepareEmail(?string $full, ?string $text, ?string $html): TemplatedEmail
57+
private function prepareEmail(?string $text, ?string $html): TemplatedEmail
15258
{
15359
$twig = new Environment(new ArrayLoader([
154-
'full' => $full,
15560
'text' => $text,
15661
'html' => $html,
15762
'document.txt' => 'Some text document...',
15863
'image.jpg' => 'Some image data',
15964
]));
16065
$renderer = new BodyRenderer($twig);
16166
$email = (new TemplatedEmail())->to('fabien@symfony.com')->from('helene@symfony.com');
162-
if (null !== $full) {
163-
$email->template('full');
164-
}
16567
if (null !== $text) {
16668
$email->textTemplate('text');
16769
}

src/Symfony/Bridge/Twig/Tests/Mime/TemplatedEmailTest.php

-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ public function test()
1313
$email->context($context = ['product' => 'Symfony']);
1414
$this->assertEquals($context, $email->getContext());
1515

16-
$email->template($template = 'full');
17-
$this->assertEquals($template, $email->getTemplate());
18-
1916
$email->textTemplate($template = 'text');
2017
$this->assertEquals($template, $email->getTextTemplate());
2118

0 commit comments

Comments
 (0)