|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\MailTemplates; |
|
4
|
|
|
|
|
5
|
|
|
use Mustache_Engine; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Spatie\MailTemplates\Exceptions\CannotRenderTemplateMailable; |
|
8
|
|
|
|
|
9
|
|
|
class TemplateMailableRenderer |
|
10
|
|
|
{ |
|
11
|
|
|
public const RENDER_HTML_LAYOUT = 0; |
|
12
|
|
|
public const RENDER_TEXT_LAYOUT = 1; |
|
13
|
|
|
|
|
14
|
|
|
/** @var \Spatie\MailTemplates\TemplateMailable */ |
|
15
|
|
|
protected $templateMailable; |
|
16
|
|
|
|
|
17
|
|
|
/** @var \Spatie\MailTemplates\Interfaces\MailTemplateInterface */ |
|
18
|
|
|
protected $mailTemplate; |
|
19
|
|
|
|
|
20
|
|
|
/** @var \Mustache_Engine */ |
|
21
|
|
|
protected $mustache; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(TemplateMailable $templateMailable, Mustache_Engine $mustache) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->templateMailable = $templateMailable; |
|
26
|
|
|
$this->mustache = $mustache; |
|
27
|
|
|
$this->mailTemplate = $templateMailable->getMailTemplate(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function renderHtmlLayout(array $data = []): string |
|
31
|
|
|
{ |
|
32
|
|
|
$body = $this->mustache->render( |
|
33
|
|
|
$this->mailTemplate->getHtmlTemplate(), |
|
34
|
|
|
$data |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
return $this->renderInLayout($body, static::RENDER_HTML_LAYOUT, $data); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function renderTextLayout(array $data = []): ?string |
|
41
|
|
|
{ |
|
42
|
|
|
if (! $this->mailTemplate->getTextTemplate()) { |
|
|
|
|
|
|
43
|
|
|
return $this->textView ?? null; |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$body = $this->mustache->render( |
|
47
|
|
|
$this->mailTemplate->getTextTemplate(), |
|
48
|
|
|
$data |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
return $this->renderInLayout($body, static::RENDER_TEXT_LAYOUT, $data); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function renderSubject(array $data = []): string |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->mustache->render( |
|
57
|
|
|
$this->mailTemplate->getSubject(), |
|
58
|
|
|
$data |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function renderInLayout(string $body, int $layoutType, array $data = []): string |
|
63
|
|
|
{ |
|
64
|
|
|
$method = $layoutType === static::RENDER_HTML_LAYOUT ? 'getHtmlLayout' : 'getTextLayout'; |
|
65
|
|
|
$layout = $this->templateMailable->$method() |
|
66
|
|
|
?? (method_exists($this->mailTemplate, $method) ? $this->mailTemplate->$method() : null) |
|
67
|
|
|
?? '{{{ body }}}'; |
|
68
|
|
|
|
|
69
|
|
|
$this->guardAgainstInvalidLayout($layout); |
|
70
|
|
|
|
|
71
|
|
|
$data = array_merge(['body' => $body], $data); |
|
72
|
|
|
|
|
73
|
|
|
return $this->mustache->render($layout, $data); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function guardAgainstInvalidLayout(string $layout): void |
|
77
|
|
|
{ |
|
78
|
|
|
if (! Str::contains($layout, [ |
|
79
|
|
|
'{{{body}}}', |
|
80
|
|
|
'{{{ body }}}', |
|
81
|
|
|
'{{body}}', |
|
82
|
|
|
'{{ body }}', |
|
83
|
|
|
'{{ $body }}', |
|
84
|
|
|
'{!! $body !!}', |
|
85
|
|
|
])) { |
|
86
|
|
|
throw CannotRenderTemplateMailable::layoutDoesNotContainABodyPlaceHolder($this->templateMailable); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: