|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\MailerBundle\Renderer\Adapter; |
|
13
|
|
|
|
|
14
|
|
|
use Sylius\Component\Mailer\Event\EmailRenderEvent; |
|
15
|
|
|
use Sylius\Component\Mailer\Model\EmailInterface; |
|
16
|
|
|
use Sylius\Component\Mailer\Renderer\Adapter\AbstractAdapter; |
|
17
|
|
|
use Sylius\Component\Mailer\Renderer\RenderedEmail; |
|
18
|
|
|
use Sylius\Component\Mailer\SyliusMailerEvents; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Daniel Richter <[email protected]> |
|
22
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
|
23
|
|
|
* @author Jérémy Leherpeur <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class EmailTwigAdapter extends AbstractAdapter |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var \Twig_Environment |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $twig; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param \Twig_Environment $twig |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(\Twig_Environment $twig) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->twig = $twig; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function render(EmailInterface $email, array $data = []) |
|
44
|
|
|
{ |
|
45
|
|
|
$renderedEmail = $this->getRenderedEmail($email, $data); |
|
46
|
|
|
|
|
47
|
|
|
/** @var EmailRenderEvent $event */ |
|
48
|
|
|
$event = $this->dispatcher->dispatch( |
|
49
|
|
|
SyliusMailerEvents::EMAIL_PRE_RENDER, |
|
50
|
|
|
new EmailRenderEvent($renderedEmail) |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
return $event->getRenderedEmail(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param EmailInterface $email |
|
58
|
|
|
* @param array $data |
|
59
|
|
|
* |
|
60
|
|
|
* @return RenderedEmail |
|
61
|
|
|
*/ |
|
62
|
|
|
private function getRenderedEmail(EmailInterface $email, array $data) |
|
63
|
|
|
{ |
|
64
|
|
|
if (null !== $email->getTemplate()) { |
|
65
|
|
|
return $this->provideEmailWithTemplate($email, $data); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this->provideEmailWithoutTemplate($email, $data); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param EmailInterface $email |
|
73
|
|
|
* @param array $data |
|
74
|
|
|
* |
|
75
|
|
|
* @return RenderedEmail |
|
76
|
|
|
*/ |
|
77
|
|
|
private function provideEmailWithTemplate(EmailInterface $email, array $data) |
|
78
|
|
|
{ |
|
79
|
|
|
$data = $this->twig->mergeGlobals($data); |
|
80
|
|
|
|
|
81
|
|
|
/** @var \Twig_Template $template */ |
|
82
|
|
|
$template = $this->twig->loadTemplate($email->getTemplate()); |
|
83
|
|
|
|
|
84
|
|
|
$subject = $template->renderBlock('subject', $data); |
|
85
|
|
|
$body = $template->renderBlock('body', $data); |
|
86
|
|
|
|
|
87
|
|
|
return new RenderedEmail($subject, $body); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param EmailInterface $email |
|
92
|
|
|
* @param array $data |
|
93
|
|
|
* |
|
94
|
|
|
* @return RenderedEmail |
|
95
|
|
|
*/ |
|
96
|
|
|
private function provideEmailWithoutTemplate(EmailInterface $email, array $data) |
|
97
|
|
|
{ |
|
98
|
|
|
$twig = new \Twig_Environment(new \Twig_Loader_Array([])); |
|
99
|
|
|
|
|
100
|
|
|
$subjectTemplate = $twig->createTemplate($email->getSubject()); |
|
101
|
|
|
$bodyTemplate = $twig->createTemplate($email->getContent()); |
|
102
|
|
|
|
|
103
|
|
|
$subject = $subjectTemplate->render($data); |
|
104
|
|
|
$body = $bodyTemplate->render($data); |
|
105
|
|
|
|
|
106
|
|
|
return new RenderedEmail($subject, $body); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|