|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\MailTemplates; |
|
4
|
|
|
|
|
5
|
|
|
use Mustache_Engine; |
|
6
|
|
|
use Spatie\MailTemplates\Models\MailTemplate; |
|
7
|
|
|
use Spatie\MailTemplates\Exceptions\CannotRenderTemplateMailable; |
|
8
|
|
|
|
|
9
|
|
|
class TemplateMailableRenderer |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var TemplateMailable */ |
|
12
|
|
|
protected $templateMailable; |
|
13
|
|
|
|
|
14
|
|
|
/** @var MailTemplate */ |
|
15
|
|
|
protected $mailTemplate; |
|
16
|
|
|
|
|
17
|
|
|
/** @var Mustache_Engine */ |
|
18
|
|
|
protected $mustache; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(TemplateMailable $templateMailable, Mustache_Engine $mustache) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->templateMailable = $templateMailable; |
|
23
|
|
|
$this->mustache = $mustache; |
|
24
|
|
|
|
|
25
|
|
|
$templateModel = $this->templateMailable->getTemplateModel(); |
|
26
|
|
|
$this->mailTemplate = $templateModel::findForMailable($templateMailable); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function render(array $data = []): string |
|
30
|
|
|
{ |
|
31
|
|
|
$html = $this->mustache->render( |
|
32
|
|
|
$this->mailTemplate->template, |
|
|
|
|
|
|
33
|
|
|
$data |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
return $this->renderInLayout($html, $data); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function renderSubject(array $data = []): string |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->mustache->render( |
|
42
|
|
|
$this->mailTemplate->subject, |
|
|
|
|
|
|
43
|
|
|
$data |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function renderInLayout(string $html, array $data = []): string |
|
48
|
|
|
{ |
|
49
|
|
|
$layout = $this->templateMailable->getLayout() |
|
50
|
|
|
?? $this->mailTemplate->getLayout() |
|
51
|
|
|
?? '{{{ body }}}'; |
|
52
|
|
|
|
|
53
|
|
|
if ($layout && ! str_contains($layout, ['{{{body}}}', '{{{ body }}}', '{{body}}', '{{ body }}'])) { |
|
54
|
|
|
throw CannotRenderTemplateMailable::layoutDoesNotContainABodyPlaceHolder($this->templateMailable, $this->mailTemplate, $layout); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$data = array_merge(['body' => $html], $data); |
|
58
|
|
|
|
|
59
|
|
|
return $this->mustache->render($layout, $data); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
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@propertyannotation 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.