1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProtoneMedia\BladeOnDemand; |
4
|
|
|
|
5
|
|
|
use Illuminate\Mail\Markdown; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Illuminate\View\Factory as ViewFactory; |
8
|
|
|
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles; |
9
|
|
|
|
10
|
|
|
class BladeOnDemandRenderer |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \Illuminate\View\Factory |
14
|
|
|
*/ |
15
|
|
|
private $viewFactory; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \Illuminate\Mail\Markdown |
19
|
|
|
*/ |
20
|
|
|
private $markdown; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles |
24
|
|
|
*/ |
25
|
|
|
private $cssInliner; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Wether to fill the missing variables from the template. |
29
|
|
|
* |
30
|
|
|
* @var boolean|callable |
31
|
|
|
*/ |
32
|
|
|
private $fillMissingVariables = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The current theme being used when generating emails. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $theme = 'default'; |
40
|
|
|
|
41
|
|
|
public function __construct(ViewFactory $viewFactory, Markdown $markdown, CssToInlineStyles $cssInliner) |
42
|
|
|
{ |
43
|
|
|
$this->viewFactory = $viewFactory; |
44
|
|
|
$this->markdown = $markdown; |
45
|
|
|
$this->cssInliner = $cssInliner; |
46
|
|
|
|
47
|
|
|
$this->theme = config('mail.markdown.theme', 'default'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Fills the missing variables in the template |
52
|
|
|
* |
53
|
|
|
* @param callable $callback |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function fillMissingVariables(callable $callback = null) |
57
|
|
|
{ |
58
|
|
|
$this->fillMissingVariables = $callback ?: true; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set the default theme to be used. |
65
|
|
|
* |
66
|
|
|
* @param string $theme |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
public function theme($theme) |
70
|
|
|
{ |
71
|
|
|
$this->theme = $theme; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Renders the content with the given data. |
78
|
|
|
* |
79
|
|
|
* @param string $contents |
80
|
|
|
* @param array $data |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function render(string $contents, array $data = []): string |
84
|
|
|
{ |
85
|
|
|
if ($this->fillMissingVariables) { |
86
|
|
|
$data = $this->addMissingVariables($contents, $data); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
file_put_contents( |
90
|
|
|
$path = tempnam(sys_get_temp_dir(), 'blade-on-demand') . '.blade.php', |
91
|
|
|
$contents |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$this->viewFactory->flushFinderCache(); |
95
|
|
|
|
96
|
|
|
return tap($this->viewFactory->file($path, $data)->render(), function () use ($path) { |
97
|
|
|
unlink($path); |
98
|
|
|
|
99
|
|
|
$this->fillMissingVariables = false; |
100
|
|
|
$this->theme = config('mail.markdown.theme', 'default'); |
101
|
|
|
}); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Finds all missing variables. |
106
|
|
|
* Source: https://stackoverflow.com/a/19563063 |
107
|
|
|
* |
108
|
|
|
* @param string $contents |
109
|
|
|
* @param array $data |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function getMissingVariables(string $contents, array $data = []): array |
113
|
|
|
{ |
114
|
|
|
$pattern = '/\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/'; |
115
|
|
|
|
116
|
|
|
preg_match_all($pattern, $contents, $matches); |
117
|
|
|
|
118
|
|
|
$variables = $matches[1] ?? []; |
119
|
|
|
|
120
|
|
|
return array_filter($variables, function ($variable) use ($data) { |
121
|
|
|
return !array_key_exists($variable, $data); |
122
|
|
|
}); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Makes sure each variable is present in the data array. |
127
|
|
|
* |
128
|
|
|
* @param string $contents |
129
|
|
|
* @param array $data |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
private function addMissingVariables(string $contents, array $data = []): array |
133
|
|
|
{ |
134
|
|
|
foreach (static::getMissingVariables($contents, $data) as $variable) { |
135
|
|
|
$data[$variable] = is_callable($this->fillMissingVariables) |
136
|
|
|
? call_user_func_array($this->fillMissingVariables, [$variable]) |
137
|
|
|
: $variable; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $data; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Renders the markdown content to a HTML mail. |
145
|
|
|
* |
146
|
|
|
* @param string $contents |
147
|
|
|
* @param array $data |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function renderMarkdownMailToHtml(string $contents, array $data = []): string |
151
|
|
|
{ |
152
|
|
|
$this->viewFactory->replaceNamespace('mail', $this->markdown->htmlComponentPaths()); |
153
|
|
|
|
154
|
|
|
$theme = Str::contains($this->theme, '::') |
155
|
|
|
? $this->theme |
156
|
|
|
: 'mail::themes.' . $this->theme; |
157
|
|
|
|
158
|
|
|
$rendered = $this->render($contents, $data); |
159
|
|
|
|
160
|
|
|
return $this->cssInliner->convert( |
161
|
|
|
$rendered, |
162
|
|
|
$this->viewFactory->make($theme)->render() |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Renders the markdown content to a Text mail. |
168
|
|
|
* |
169
|
|
|
* @param string $contents |
170
|
|
|
* @param array $data |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
public function renderMarkdownMailToText(string $contents, array $data = []): string |
174
|
|
|
{ |
175
|
|
|
$this->viewFactory->replaceNamespace('mail', $this->markdown->textComponentPaths()); |
176
|
|
|
|
177
|
|
|
$rendered = $this->render($contents, $data); |
178
|
|
|
|
179
|
|
|
return html_entity_decode(preg_replace("/[\r\n]{2,}/", "\n\n", $rendered), ENT_QUOTES, 'UTF-8'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Parses the markdown content. |
184
|
|
|
* |
185
|
|
|
* @param string $contents |
186
|
|
|
* @param array $data |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
|
|
public function parseMarkdownMail(string $contents, array $data = []): string |
190
|
|
|
{ |
191
|
|
|
return $this->markdown->parse($this->renderMarkdownMailToText($contents, $data)); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|