1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Mailer; |
6
|
|
|
|
7
|
|
|
use Yiisoft\View\View; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Composer composes the mail messages via view rendering. |
11
|
|
|
*/ |
12
|
|
|
class Composer |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var View view instance. |
16
|
|
|
*/ |
17
|
|
|
private View $view; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string the directory containing view files for composing mail messages. |
21
|
|
|
*/ |
22
|
|
|
private string $viewPath; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string HTML layout view name. |
26
|
|
|
* See [[Template::$htmlLayout]] for detailed documentation. |
27
|
|
|
*/ |
28
|
|
|
private string $htmlLayout = 'layouts/html'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param View $view |
32
|
|
|
* @param string $viewPath |
33
|
|
|
*/ |
34
|
16 |
|
public function __construct(View $view, string $viewPath) |
35
|
|
|
{ |
36
|
16 |
|
$this->view = $view; |
37
|
16 |
|
$this->viewPath = $viewPath; |
38
|
16 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Sets html layout. |
42
|
|
|
* @param string $layout |
43
|
|
|
*/ |
44
|
3 |
|
public function setHtmlLayout(string $layout): void |
45
|
|
|
{ |
46
|
3 |
|
$this->htmlLayout = $layout; |
47
|
3 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string text layout view name. |
51
|
|
|
* See [[Template::$textLayout]] for detailed documentation. |
52
|
|
|
*/ |
53
|
|
|
private string $textLayout = 'layouts/text'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Sets text layout. |
57
|
|
|
* @param string $layout |
58
|
|
|
*/ |
59
|
3 |
|
public function setTextLayout(string $layout): void |
60
|
|
|
{ |
61
|
3 |
|
$this->textLayout = $layout; |
62
|
3 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string the directory that contains the view files for composing mail messages. |
66
|
|
|
*/ |
67
|
3 |
|
public function getViewPath(): string |
68
|
|
|
{ |
69
|
3 |
|
return $this->viewPath; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $path the directory that contains the view files for composing mail messages. |
74
|
|
|
*/ |
75
|
2 |
|
public function setViewPath(string $path): void |
76
|
|
|
{ |
77
|
2 |
|
$this->viewPath = $path; |
78
|
2 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param View $view view instance or its array configuration that will be used to |
82
|
|
|
* render message bodies. |
83
|
|
|
*/ |
84
|
1 |
|
public function setView(View $view): void |
85
|
|
|
{ |
86
|
1 |
|
$this->view = $view; |
87
|
1 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return View view instance. |
91
|
|
|
*/ |
92
|
4 |
|
public function getView(): View |
93
|
|
|
{ |
94
|
4 |
|
return $this->view; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Creates new message view template. |
99
|
|
|
* The newly created instance will be initialized with the configuration specified by [[templateConfig]]. |
100
|
|
|
* @param string|array $viewName view name for the template. |
101
|
|
|
* @return Template message template instance. |
102
|
|
|
*/ |
103
|
2 |
|
protected function createTemplate($viewName): Template |
104
|
|
|
{ |
105
|
2 |
|
$template = new Template($this->view, $this->viewPath, $viewName); |
106
|
2 |
|
$template->setHtmlLayout($this->htmlLayout); |
107
|
2 |
|
$template->setTextLayout($this->textLayout); |
108
|
|
|
|
109
|
2 |
|
return $template; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param MessageInterface $message the message to be composed. |
114
|
|
|
* @param string|array $view the view to be used for rendering the message body. This can be: |
115
|
|
|
* |
116
|
|
|
* - a string, which represents the view name for rendering the HTML body of the email. |
117
|
|
|
* In this case, the text body will be generated by applying `strip_tags()` to the HTML body. |
118
|
|
|
* - an array with 'html' and/or 'text' elements. The 'html' element refers to the view name |
119
|
|
|
* for rendering the HTML body, while 'text' element is for rendering the text body. For example, |
120
|
|
|
* `['html' => 'contact-html', 'text' => 'contact-text']`. |
121
|
|
|
* |
122
|
|
|
* @param array $parameters the parameters (name-value pairs) that will be extracted and made available in the view file. |
123
|
|
|
*/ |
124
|
1 |
|
public function compose(MessageInterface $message, $view, array $parameters = []): void |
125
|
|
|
{ |
126
|
1 |
|
$this->createTemplate($view)->compose($message, $parameters); |
127
|
1 |
|
} |
128
|
|
|
} |
129
|
|
|
|