1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\MailTemplates; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use ReflectionProperty; |
7
|
|
|
use Illuminate\Bus\Queueable; |
8
|
|
|
use Illuminate\Mail\Mailable; |
9
|
|
|
use Illuminate\Support\HtmlString; |
10
|
|
|
use Spatie\MailTemplates\Models\MailTemplate; |
11
|
|
|
use Spatie\MailTemplates\Interfaces\MailTemplateInterface; |
12
|
|
|
|
13
|
|
|
abstract class TemplateMailable extends Mailable |
14
|
|
|
{ |
15
|
|
|
protected static $templateModelClass = MailTemplate::class; |
16
|
|
|
|
17
|
|
|
/** @var MailTemplateInterface */ |
18
|
|
|
protected $mailTemplate; |
19
|
|
|
|
20
|
|
|
public static function getVariables(): array |
21
|
|
|
{ |
22
|
|
|
return static::getPublicProperties(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getMailTemplate(): MailTemplateInterface |
26
|
|
|
{ |
27
|
|
|
return $this->mailTemplate ?? $this->resolveTemplateModel(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function resolveTemplateModel(): MailTemplateInterface |
31
|
|
|
{ |
32
|
|
|
return $this->mailTemplate = static::$templateModelClass::findForMailable($this); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function buildView() |
36
|
|
|
{ |
37
|
|
|
$renderer = $this->getMailTemplateRenderer(); |
38
|
|
|
|
39
|
|
|
$viewData = $this->buildViewData(); |
40
|
|
|
|
41
|
|
|
$html = $renderer->renderHtmlLayout($viewData); |
42
|
|
|
$text = $renderer->renderTextLayout($viewData); |
43
|
|
|
|
44
|
|
|
return array_filter([ |
45
|
|
|
'html' => new HtmlString($html), |
46
|
|
|
'text' => new HtmlString($text), |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function buildSubject($message) |
51
|
|
|
{ |
52
|
|
|
if ($this->subject) { |
53
|
|
|
$message->subject($this->subject); |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($this->getMailTemplate()->subject()) { |
59
|
|
|
$subject = $this |
60
|
|
|
->getMailTemplateRenderer() |
61
|
|
|
->renderSubject($this->buildViewData()); |
62
|
|
|
|
63
|
|
|
$message->subject($subject); |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return parent::buildSubject($message); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getHtmlLayout(): ?string |
72
|
|
|
{ |
73
|
|
|
return null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getTextLayout(): ?string |
77
|
|
|
{ |
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function build() |
82
|
|
|
{ |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected static function getPublicProperties(): array |
87
|
|
|
{ |
88
|
|
|
$class = new ReflectionClass(static::class); |
89
|
|
|
|
90
|
|
|
return collect($class->getProperties(ReflectionProperty::IS_PUBLIC)) |
91
|
|
|
->map->getName() |
92
|
|
|
->diff(static::getIgnoredPublicProperties()) |
93
|
|
|
->values() |
94
|
|
|
->all(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected static function getIgnoredPublicProperties(): array |
98
|
|
|
{ |
99
|
|
|
$mailableClass = new ReflectionClass(Mailable::class); |
100
|
|
|
$queueableClass = new ReflectionClass(Queueable::class); |
101
|
|
|
|
102
|
|
|
return collect() |
103
|
|
|
->merge($mailableClass->getProperties(ReflectionProperty::IS_PUBLIC)) |
104
|
|
|
->merge($queueableClass->getProperties(ReflectionProperty::IS_PUBLIC)) |
105
|
|
|
->map->getName() |
106
|
|
|
->values() |
107
|
|
|
->all(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function getMailTemplateRenderer(): TemplateMailableRenderer |
111
|
|
|
{ |
112
|
|
|
return app(TemplateMailableRenderer::class, ['templateMailable' => $this]); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|