|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ProtoneMedia\BladeOnDemand; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Mail\Markdown; |
|
6
|
|
|
use Illuminate\View\Factory as ViewFactory; |
|
7
|
|
|
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles; |
|
8
|
|
|
|
|
9
|
|
|
class BladeOnDemandRenderer |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var \Illuminate\View\Factory |
|
13
|
|
|
*/ |
|
14
|
|
|
private $viewFactory; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var \Illuminate\Mail\Markdown |
|
18
|
|
|
*/ |
|
19
|
|
|
private $markdown; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles |
|
23
|
|
|
*/ |
|
24
|
|
|
private $cssInliner; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(ViewFactory $viewFactory, Markdown $markdown, CssToInlineStyles $cssInliner) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->viewFactory = $viewFactory; |
|
29
|
|
|
$this->markdown = $markdown; |
|
30
|
|
|
$this->cssInliner = $cssInliner; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Renders the content with the given data. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $contents |
|
37
|
|
|
* @param array $data |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
|
|
public function render(string $contents, array $data = []): string |
|
41
|
|
|
{ |
|
42
|
|
|
file_put_contents( |
|
43
|
|
|
$path = tempnam(sys_get_temp_dir(), 'blade-on-demand') . '.blade.php', |
|
44
|
|
|
$contents |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
$this->viewFactory->flushFinderCache(); |
|
48
|
|
|
|
|
49
|
|
|
return tap($this->viewFactory->file($path, $data)->render(), function () use ($path) { |
|
50
|
|
|
unlink($path); |
|
51
|
|
|
}); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Renders the markdown content to a HTML mail. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $contents |
|
58
|
|
|
* @param array $data |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public function renderMarkdownMailToHtml(string $contents, array $data = []): string |
|
62
|
|
|
{ |
|
63
|
|
|
$this->viewFactory->replaceNamespace('mail', $this->markdown->htmlComponentPaths()); |
|
64
|
|
|
|
|
65
|
|
|
$rendered = $this->render($contents, $data); |
|
66
|
|
|
|
|
67
|
|
|
return $this->cssInliner->convert( |
|
68
|
|
|
$rendered, |
|
69
|
|
|
$this->viewFactory->make('mail::themes.' . config('mail.markdown.theme', 'default'))->render() |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Renders the markdown content to a Text mail. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $contents |
|
77
|
|
|
* @param array $data |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function renderMarkdownMailToText(string $contents, array $data = []): string |
|
81
|
|
|
{ |
|
82
|
|
|
$this->viewFactory->replaceNamespace('mail', $this->markdown->textComponentPaths()); |
|
83
|
|
|
|
|
84
|
|
|
$rendered = $this->render($contents, $data); |
|
85
|
|
|
|
|
86
|
|
|
return html_entity_decode(preg_replace("/[\r\n]{2,}/", "\n\n", $rendered), ENT_QUOTES, 'UTF-8'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Parses the markdown content. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $contents |
|
93
|
|
|
* @param array $data |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function parseMarkdownMail(string $contents, array $data = []): string |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->markdown->parse($this->renderMarkdownMailToText($contents, $data)); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|