12
12
namespace Symfony \Bridge \Twig \Mime ;
13
13
14
14
use League \HTMLToMarkdown \HtmlConverter ;
15
+ use Symfony \Component \Mime \BodyRendererInterface ;
16
+ use Symfony \Component \Mime \Message ;
15
17
use Twig \Environment ;
16
18
17
19
/**
18
20
* @author Fabien Potencier <fabien@symfony.com>
19
21
*
20
22
* @experimental in 4.3
21
23
*/
22
- final class Renderer
24
+ final class BodyRenderer implements BodyRendererInterface
23
25
{
24
26
private $ twig ;
25
27
private $ context ;
@@ -38,48 +40,48 @@ public function __construct(Environment $twig, array $context = [])
38
40
}
39
41
}
40
42
41
- public function render (TemplatedEmail $ email ): TemplatedEmail
43
+ public function render (Message $ message ): void
42
44
{
43
- $ email = clone $ email ;
45
+ if (!$ message instanceof TemplatedEmail) {
46
+ return ;
47
+ }
44
48
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 ),
47
51
]);
48
52
49
- if ($ template = $ email ->getTemplate ()) {
50
- $ this ->renderFull ($ email , $ template , $ vars );
53
+ if ($ template = $ message ->getTemplate ()) {
54
+ $ this ->renderFull ($ message , $ template , $ vars );
51
55
}
52
56
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 ));
55
59
}
56
60
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 ));
59
63
}
60
64
61
65
// 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 ));
64
68
}
65
-
66
- return $ email ;
67
69
}
68
70
69
- private function renderFull (TemplatedEmail $ email , string $ template , array $ vars ): void
71
+ private function renderFull (TemplatedEmail $ message , string $ template , array $ vars ): void
70
72
{
71
73
$ template = $ this ->twig ->load ($ template );
72
74
73
75
if ($ template ->hasBlock ('subject ' , $ vars )) {
74
- $ email ->subject ($ template ->renderBlock ('subject ' , $ vars ));
76
+ $ message ->subject ($ template ->renderBlock ('subject ' , $ vars ));
75
77
}
76
78
77
79
if ($ template ->hasBlock ('text ' , $ vars )) {
78
- $ email ->text ($ template ->renderBlock ('text ' , $ vars ));
80
+ $ message ->text ($ template ->renderBlock ('text ' , $ vars ));
79
81
}
80
82
81
83
if ($ template ->hasBlock ('html ' , $ vars )) {
82
- $ email ->html ($ template ->renderBlock ('html ' , $ vars ));
84
+ $ message ->html ($ template ->renderBlock ('html ' , $ vars ));
83
85
}
84
86
85
87
if ($ template ->hasBlock ('config ' , $ vars )) {
0 commit comments