1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\mail; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
use yii\base\Object; |
13
|
|
|
use yii\web\View; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Composer composes the mail messages via view rendering. |
17
|
|
|
* |
18
|
|
|
* @property \yii\base\View $view View instance. Note that the type of this property differs in getter and setter. See |
19
|
|
|
* [[getView()]] and [[setView()]] for details. |
20
|
|
|
* @property string $viewPath The directory that contains the view files for composing mail messages Defaults |
21
|
|
|
* to '@app/mail'. |
22
|
|
|
* |
23
|
|
|
* @author Paul Klimov <[email protected]> |
24
|
|
|
* @since 2.1 |
25
|
|
|
*/ |
26
|
|
|
class Composer extends Object |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string|bool HTML layout view name. |
30
|
|
|
* See [[Template::$htmlLayout]] for detailed documentation. |
31
|
|
|
*/ |
32
|
|
|
public $htmlLayout = 'layouts/html'; |
33
|
|
|
/** |
34
|
|
|
* @var string|bool text layout view name. |
35
|
|
|
* See [[Template::$textLayout]] for detailed documentation. |
36
|
|
|
*/ |
37
|
|
|
public $textLayout = 'layouts/text'; |
38
|
|
|
/** |
39
|
|
|
* @var array the configuration that should be applied to any newly created message template. |
40
|
|
|
*/ |
41
|
|
|
public $templateConfig = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \yii\base\View|array view instance or its array configuration. |
45
|
|
|
*/ |
46
|
|
|
private $_view = []; |
47
|
|
|
/** |
48
|
|
|
* @var string the directory containing view files for composing mail messages. |
49
|
|
|
*/ |
50
|
|
|
private $_viewPath; |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string the directory that contains the view files for composing mail messages |
55
|
|
|
* Defaults to '@app/mail'. |
56
|
|
|
*/ |
57
|
2 |
|
public function getViewPath() |
58
|
|
|
{ |
59
|
2 |
|
if ($this->_viewPath === null) { |
60
|
|
|
$this->setViewPath('@app/mail'); |
61
|
|
|
} |
62
|
2 |
|
return $this->_viewPath; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $path the directory that contains the view files for composing mail messages |
67
|
|
|
* This can be specified as an absolute path or a path alias. |
68
|
|
|
*/ |
69
|
2 |
|
public function setViewPath($path) |
70
|
|
|
{ |
71
|
2 |
|
$this->_viewPath = Yii::getAlias($path); |
|
|
|
|
72
|
2 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array|\yii\base\View $view view instance or its array configuration that will be used to |
76
|
|
|
* render message bodies. |
77
|
|
|
* @throws InvalidConfigException on invalid argument. |
78
|
|
|
*/ |
79
|
1 |
|
public function setView($view) |
80
|
|
|
{ |
81
|
1 |
|
if (!is_array($view) && !is_object($view)) { |
82
|
|
|
throw new InvalidConfigException('"' . get_class($this) . '::view" should be either object or configuration array, "' . gettype($view) . '" given.'); |
83
|
|
|
} |
84
|
1 |
|
$this->_view = $view; |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return \yii\base\View view instance. |
89
|
|
|
*/ |
90
|
4 |
|
public function getView() |
91
|
|
|
{ |
92
|
4 |
|
if (!is_object($this->_view)) { |
93
|
4 |
|
$this->_view = $this->createView($this->_view); |
94
|
|
|
} |
95
|
|
|
|
96
|
4 |
|
return $this->_view; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Creates view instance from given configuration. |
101
|
|
|
* @param array $config view configuration. |
102
|
|
|
* @return \yii\base\View view instance. |
103
|
|
|
*/ |
104
|
4 |
|
protected function createView(array $config) |
105
|
|
|
{ |
106
|
4 |
|
if (!array_key_exists('class', $config)) { |
107
|
4 |
|
$config['class'] = View::class; |
108
|
|
|
} |
109
|
|
|
|
110
|
4 |
|
return Yii::createObject($config); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Creates new message view template. |
115
|
|
|
* The newly created instance will be initialized with the configuration specified by [[templateConfig]]. |
116
|
|
|
* @param string|array $viewName view name for the template. |
117
|
|
|
* @return Template message template instance. |
118
|
|
|
* @throws InvalidConfigException if the [[templateConfig]] is invalid. |
119
|
|
|
*/ |
120
|
2 |
|
protected function createTemplate($viewName) |
121
|
|
|
{ |
122
|
2 |
|
$config = $this->templateConfig; |
123
|
2 |
|
if (!array_key_exists('class', $config)) { |
124
|
2 |
|
$config['class'] = Template::class; |
125
|
|
|
} |
126
|
2 |
|
if (!array_key_exists('view', $config)) { |
127
|
2 |
|
$config['view'] = $this->getView(); |
128
|
|
|
} |
129
|
|
|
|
130
|
2 |
|
$config['viewPath'] = $this->getViewPath(); |
131
|
2 |
|
$config['htmlLayout'] = $this->htmlLayout; |
132
|
2 |
|
$config['textLayout'] = $this->textLayout; |
133
|
2 |
|
$config['viewName'] = $viewName; |
134
|
|
|
|
135
|
2 |
|
return Yii::createObject($config); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param MessageInterface $message the message to be composed. |
140
|
|
|
* @param string|array $view the view to be used for rendering the message body. This can be: |
141
|
|
|
* |
142
|
|
|
* - a string, which represents the view name or path alias for rendering the HTML body of the email. |
143
|
|
|
* In this case, the text body will be generated by applying `strip_tags()` to the HTML body. |
144
|
|
|
* - an array with 'html' and/or 'text' elements. The 'html' element refers to the view name or path alias |
145
|
|
|
* for rendering the HTML body, while 'text' element is for rendering the text body. For example, |
146
|
|
|
* `['html' => 'contact-html', 'text' => 'contact-text']`. |
147
|
|
|
* |
148
|
|
|
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. |
149
|
|
|
*/ |
150
|
1 |
|
public function compose($message, $view, array $params = []) |
151
|
|
|
{ |
152
|
1 |
|
$this->createTemplate($view)->compose($message, $params); |
153
|
|
|
} |
154
|
|
|
} |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.