1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\MailTemplates; |
4
|
|
|
|
5
|
|
|
use Illuminate\Mail\Markdown; |
6
|
|
|
use Illuminate\Support\Facades\Mail; |
7
|
|
|
use Mustache_Engine; |
8
|
|
|
use Spatie\MailTemplates\Exceptions\CannotRenderTemplateMailable; |
9
|
|
|
use Spatie\MailTemplates\Models\MailTemplate; |
10
|
|
|
|
11
|
|
|
class TemplateMailableRenderer |
12
|
|
|
{ |
13
|
|
|
/** @var TemplateMailable */ |
14
|
|
|
protected $templateMailable; |
15
|
|
|
|
16
|
|
|
/** @var MailTemplate */ |
17
|
|
|
protected $mailTemplate; |
18
|
|
|
|
19
|
|
|
/** @var Mustache_Engine */ |
20
|
|
|
protected $mustache; |
21
|
|
|
|
22
|
|
|
/** @var Markdown */ |
23
|
|
|
protected $markdown; |
24
|
|
|
|
25
|
|
|
public function __construct(TemplateMailable $templateMailable, Mustache_Engine $mustache, Markdown $markdown) |
26
|
|
|
{ |
27
|
|
|
$this->templateMailable = $templateMailable; |
28
|
|
|
$this->mustache = $mustache; |
29
|
|
|
$this->markdown = $markdown; |
30
|
|
|
|
31
|
|
|
$templateModel = $this->templateMailable->getTemplateModel(); |
32
|
|
|
$this->mailTemplate = $templateModel::findForMailable($templateMailable); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function render(array $data = []): string |
36
|
|
|
{ |
37
|
|
|
$renderer = ($this->mailTemplate->isMarkdown() |
38
|
|
|
? $this->markdown->theme($this->mailTemplate->markdown_theme ?? 'default') |
|
|
|
|
39
|
|
|
: $this->mustache); |
40
|
|
|
|
41
|
|
|
$html = $renderer->render( |
42
|
|
|
$this->mailTemplate->template, |
|
|
|
|
43
|
|
|
$data |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
return $this->renderInLayout($html, $data); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function renderTextView(array $data = []): ?string |
50
|
|
|
{ |
51
|
|
|
if ($this->mailTemplate->isMarkdown()) { |
52
|
|
|
return $this->templateMailable->textView |
53
|
|
|
?? $this->markdown->renderText($this->mailTemplate->template, $data); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->templateMailable->textView ?? null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function renderSubject(array $data = []): string |
60
|
|
|
{ |
61
|
|
|
return $this->mustache->render( |
62
|
|
|
$this->mailTemplate->subject, |
|
|
|
|
63
|
|
|
$data |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function renderInLayout(string $html, array $data = []): string |
68
|
|
|
{ |
69
|
|
|
$layout = $this->templateMailable->getLayout() |
70
|
|
|
?? $this->mailTemplate->getLayout() |
71
|
|
|
?? '{{{ body }}}'; |
72
|
|
|
|
73
|
|
|
// TODO: Regex for finding {{{ body }}} in layout string |
74
|
|
|
if ($layout && ! str_contains($layout, ['{{{body}}}', '{{{ body }}}', '{{body}}', '{{ body }}'])) { |
75
|
|
|
throw CannotRenderTemplateMailable::layoutDoesNotContainABodyPlaceHolder($this->templateMailable, $this->mailTemplate, $layout); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$data = array_merge(['body' => $html], $data); |
79
|
|
|
|
80
|
|
|
return $this->mustache->render($layout, $data); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.