Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Mail;
4
 
5
use Illuminate\Contracts\View\Factory as ViewFactory;
6
use Illuminate\Support\HtmlString;
7
use Illuminate\Support\Str;
8
use League\CommonMark\Environment\Environment;
9
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
10
use League\CommonMark\Extension\Table\TableExtension;
11
use League\CommonMark\MarkdownConverter;
12
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
13
 
14
class Markdown
15
{
16
    /**
17
     * The view factory implementation.
18
     *
19
     * @var \Illuminate\Contracts\View\Factory
20
     */
21
    protected $view;
22
 
23
    /**
24
     * The current theme being used when generating emails.
25
     *
26
     * @var string
27
     */
28
    protected $theme = 'default';
29
 
30
    /**
31
     * The registered component paths.
32
     *
33
     * @var array
34
     */
35
    protected $componentPaths = [];
36
 
37
    /**
38
     * Create a new Markdown renderer instance.
39
     *
40
     * @param  \Illuminate\Contracts\View\Factory  $view
41
     * @param  array  $options
42
     * @return void
43
     */
44
    public function __construct(ViewFactory $view, array $options = [])
45
    {
46
        $this->view = $view;
47
        $this->theme = $options['theme'] ?? 'default';
48
        $this->loadComponentsFrom($options['paths'] ?? []);
49
    }
50
 
51
    /**
52
     * Render the Markdown template into HTML.
53
     *
54
     * @param  string  $view
55
     * @param  array  $data
56
     * @param  \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles|null  $inliner
57
     * @return \Illuminate\Support\HtmlString
58
     */
59
    public function render($view, array $data = [], $inliner = null)
60
    {
61
        $this->view->flushFinderCache();
62
 
63
        $contents = $this->view->replaceNamespace(
64
            'mail', $this->htmlComponentPaths()
65
        )->make($view, $data)->render();
66
 
67
        if ($this->view->exists($customTheme = Str::start($this->theme, 'mail.'))) {
68
            $theme = $customTheme;
69
        } else {
70
            $theme = str_contains($this->theme, '::')
71
                ? $this->theme
72
                : 'mail::themes.'.$this->theme;
73
        }
74
 
75
        return new HtmlString(($inliner ?: new CssToInlineStyles)->convert(
76
            $contents, $this->view->make($theme, $data)->render()
77
        ));
78
    }
79
 
80
    /**
81
     * Render the Markdown template into text.
82
     *
83
     * @param  string  $view
84
     * @param  array  $data
85
     * @return \Illuminate\Support\HtmlString
86
     */
87
    public function renderText($view, array $data = [])
88
    {
89
        $this->view->flushFinderCache();
90
 
91
        $contents = $this->view->replaceNamespace(
92
            'mail', $this->textComponentPaths()
93
        )->make($view, $data)->render();
94
 
95
        return new HtmlString(
96
            html_entity_decode(preg_replace("/[\r\n]{2,}/", "\n\n", $contents), ENT_QUOTES, 'UTF-8')
97
        );
98
    }
99
 
100
    /**
101
     * Parse the given Markdown text into HTML.
102
     *
103
     * @param  string  $text
104
     * @return \Illuminate\Support\HtmlString
105
     */
106
    public static function parse($text)
107
    {
108
        $environment = new Environment([
109
            'allow_unsafe_links' => false,
110
        ]);
111
 
112
        $environment->addExtension(new CommonMarkCoreExtension);
113
        $environment->addExtension(new TableExtension);
114
 
115
        $converter = new MarkdownConverter($environment);
116
 
117
        return new HtmlString($converter->convert($text)->getContent());
118
    }
119
 
120
    /**
121
     * Get the HTML component paths.
122
     *
123
     * @return array
124
     */
125
    public function htmlComponentPaths()
126
    {
127
        return array_map(function ($path) {
128
            return $path.'/html';
129
        }, $this->componentPaths());
130
    }
131
 
132
    /**
133
     * Get the text component paths.
134
     *
135
     * @return array
136
     */
137
    public function textComponentPaths()
138
    {
139
        return array_map(function ($path) {
140
            return $path.'/text';
141
        }, $this->componentPaths());
142
    }
143
 
144
    /**
145
     * Get the component paths.
146
     *
147
     * @return array
148
     */
149
    protected function componentPaths()
150
    {
151
        return array_unique(array_merge($this->componentPaths, [
152
            __DIR__.'/resources/views',
153
        ]));
154
    }
155
 
156
    /**
157
     * Register new mail component paths.
158
     *
159
     * @param  array  $paths
160
     * @return void
161
     */
162
    public function loadComponentsFrom(array $paths = [])
163
    {
164
        $this->componentPaths = $paths;
165
    }
166
 
167
    /**
168
     * Set the default theme to be used.
169
     *
170
     * @param  string  $theme
171
     * @return $this
172
     */
173
    public function theme($theme)
174
    {
175
        $this->theme = $theme;
176
 
177
        return $this;
178
    }
179
 
180
    /**
181
     * Get the theme currently being used by the renderer.
182
     *
183
     * @return string
184
     */
185
    public function getTheme()
186
    {
187
        return $this->theme;
188
    }
189
}