1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
7
|
|
|
* @license MIT |
8
|
|
|
* @author [email protected] |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Core\Mail; |
12
|
|
|
|
13
|
|
|
use Interop\Container\ContainerInterface; |
14
|
|
|
use Laminas\Mail\Header; |
15
|
|
|
use Laminas\ServiceManager\ServiceLocatorInterface; |
16
|
|
|
use Laminas\View\Model\ViewModel; |
17
|
|
|
use Laminas\Stdlib\Response; |
18
|
|
|
use Laminas\View\Variables as ViewVariables; |
19
|
|
|
use Laminas\Stdlib\ArrayUtils; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class HTMLTemplateMessage. |
23
|
|
|
* uses methods alike ViewModel |
24
|
|
|
* @package Core\Mail |
25
|
|
|
*/ |
26
|
|
|
class HTMLTemplateMessage extends TranslatorAwareMessage |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var ServiceLocatorInterface |
30
|
|
|
*/ |
31
|
|
|
protected $serviceManager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* View variables |
35
|
|
|
* @var array|\ArrayAccess|\Traversable |
36
|
|
|
*/ |
37
|
|
|
protected $variables = array(); |
38
|
|
|
|
39
|
|
|
protected $renderedBody; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* HTMLTemplateMessage constructor. |
43
|
|
|
* |
44
|
|
|
* @param ContainerInterface $serviceManager |
45
|
|
|
* @param array $options |
46
|
|
|
*/ |
47
|
8 |
|
public function __construct( |
48
|
|
|
ContainerInterface $serviceManager, |
49
|
|
|
array $options = array() |
50
|
|
|
) { |
51
|
|
|
// @TODO make this multipart |
52
|
8 |
|
parent::__construct($options); |
53
|
8 |
|
$this->serviceManager = $serviceManager; |
|
|
|
|
54
|
8 |
|
$this->getHeaders()->addHeader(Header\ContentType::fromString('Content-Type: text/html; charset=UTF-8')); |
55
|
8 |
|
$this->setEncoding('UTF-8'); |
56
|
8 |
|
$this->variables = new ViewVariables(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Property overloading: set variable value |
61
|
|
|
* |
62
|
|
|
* @param string $name |
63
|
|
|
* @param mixed $value |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
2 |
|
public function __set($name, $value) |
67
|
|
|
{ |
68
|
2 |
|
$this->setVariable($name, $value); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Property overloading: get variable value |
73
|
|
|
* |
74
|
|
|
* @param string $name |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
3 |
|
public function __get($name) |
78
|
|
|
{ |
79
|
3 |
|
if (!$this->__isset($name)) { |
80
|
2 |
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
$variables = $this->getVariables(); |
84
|
3 |
|
return $variables[$name]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Property overloading: do we have the requested variable value? |
89
|
|
|
* |
90
|
|
|
* @param string $name |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
3 |
|
public function __isset($name) |
94
|
|
|
{ |
95
|
3 |
|
$variables = $this->getVariables(); |
96
|
3 |
|
return isset($variables[$name]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Property overloading: unset the requested variable |
101
|
|
|
* |
102
|
|
|
* @param string $name |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
1 |
|
public function __unset($name) |
106
|
|
|
{ |
107
|
1 |
|
if ($this->__isset($name)) { |
108
|
1 |
|
unset($this->variables[$name]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get a single view variable |
114
|
|
|
* |
115
|
|
|
* @param string $name |
116
|
|
|
* @param mixed|null $default (optional) default value if the variable is not present. |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
4 |
|
public function getVariable($name, $default = null) |
120
|
|
|
{ |
121
|
4 |
|
$name = (string) $name; |
122
|
4 |
|
if (is_array($this->variables) && array_key_exists($name, $this->variables)) { |
123
|
|
|
return $this->variables[$name]; |
124
|
4 |
|
} elseif (is_object($this->variables) && property_exists($this->variables, $name)) { |
125
|
2 |
|
return $this->variables[$name]; |
126
|
|
|
} |
127
|
|
|
|
128
|
3 |
|
return $default; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set view variable |
133
|
|
|
* |
134
|
|
|
* @param string $name |
135
|
|
|
* @param mixed $value |
136
|
|
|
* @return ViewModel |
137
|
|
|
*/ |
138
|
5 |
|
public function setVariable($name, $value) |
139
|
|
|
{ |
140
|
5 |
|
$this->variables[(string) $name] = $value; |
141
|
5 |
|
return $this; |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set view variables en masse |
146
|
|
|
* |
147
|
|
|
* Can be an array or a Traversable + ArrayAccess object. |
148
|
|
|
* |
149
|
|
|
* @param array|\ArrayAccess|\Traversable $variables |
150
|
|
|
* @param bool $overwrite Whether or not to overwrite the internal container with $variables |
151
|
|
|
* @throws \InvalidArgumentException |
152
|
|
|
* @return self |
153
|
|
|
*/ |
154
|
2 |
|
public function setVariables($variables, $overwrite = false) |
155
|
|
|
{ |
156
|
2 |
|
if (!is_array($variables) && !$variables instanceof \Traversable) { |
157
|
1 |
|
throw new \InvalidArgumentException( |
158
|
1 |
|
sprintf( |
159
|
1 |
|
'%s: expects an array, or Traversable argument; received "%s"', |
160
|
1 |
|
__METHOD__, |
161
|
1 |
|
(is_object($variables) ? get_class($variables) : gettype($variables)) |
162
|
|
|
) |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
2 |
|
if ($overwrite) { |
167
|
1 |
|
if (is_object($variables) && !$variables instanceof \ArrayAccess) { |
168
|
1 |
|
$variables = ArrayUtils::iteratorToArray($variables); |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
$this->variables = $variables; |
172
|
1 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
2 |
|
foreach ($variables as $key => $value) { |
176
|
2 |
|
$this->setVariable($key, $value); |
177
|
|
|
} |
178
|
|
|
|
179
|
2 |
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get view variables |
184
|
|
|
* |
185
|
|
|
* @return array|\ArrayAccess|\Traversable |
186
|
|
|
*/ |
187
|
4 |
|
public function getVariables() |
188
|
|
|
{ |
189
|
4 |
|
return $this->variables; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Clear all variables |
194
|
|
|
* |
195
|
|
|
* Resets the internal variable container to an empty container. |
196
|
|
|
* |
197
|
|
|
* @return ViewModel |
198
|
|
|
*/ |
199
|
1 |
|
public function clearVariables() |
200
|
|
|
{ |
201
|
1 |
|
$this->variables = new ViewVariables(); |
202
|
1 |
|
return $this; |
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* |
207
|
|
|
* @param $template |
208
|
|
|
* |
209
|
|
|
* @return self |
210
|
|
|
*/ |
211
|
1 |
|
public function setTemplate($template) |
212
|
|
|
{ |
213
|
1 |
|
$this->template = $template; |
|
|
|
|
214
|
1 |
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
1 |
|
public function getTemplate() |
218
|
|
|
{ |
219
|
1 |
|
return $this->template; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @example /module/Jobs/src/Jobs/Listener/PortalListener.php |
224
|
|
|
* @return string |
225
|
|
|
* @throws \InvalidArgumentException the mail body must completely be provided by the template, any other attempt is a misconception that may leave the coder in an quagmire |
226
|
|
|
*/ |
227
|
1 |
|
public function renderBodyText($force = true, $forceLanguage = null) |
228
|
|
|
{ |
229
|
1 |
|
if (!$this->renderedBody || $force) { |
230
|
1 |
|
$viewModel = new ViewModel(); |
231
|
1 |
|
$response = new Response(); |
232
|
1 |
|
$body = parent::getBodyText(); |
233
|
1 |
|
if (!empty($body)) { |
234
|
1 |
|
throw new \InvalidArgumentException('mail body shall come from Template.'); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/* @var \Laminas\Mvc\View\Http\ViewManager $viewManager */ |
238
|
|
|
$viewManager = $this->serviceManager->get('ViewManager'); |
239
|
|
|
$resolver = $this->serviceManager->get('ViewResolver'); |
240
|
|
|
|
241
|
|
|
/* @var \Laminas\Mvc\MvcEvent $event */ |
242
|
|
|
$event = $this->serviceManager->get('Application')->getMvcEvent(); |
243
|
|
|
$lang = $forceLanguage ?: $event->getRouteMatch()->getParam('lang'); |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
if ($resolver->resolve($this->getTemplate() . '.' . $lang)) { |
247
|
|
|
$viewModel->setTemplate($this->getTemplate() . '.' . $lang); |
248
|
|
|
} else { |
249
|
|
|
$viewModel->setTemplate($this->getTemplate()); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$view = $viewManager->getView(); |
253
|
|
|
|
254
|
|
|
$viewModel->setVariables($this->getVariables()); |
255
|
|
|
$viewModel->setVariable('mail', $this); |
256
|
|
|
$view->setResponse($response); |
257
|
|
|
$view->render($viewModel); |
258
|
|
|
$body = $response->getContent(); |
259
|
|
|
|
260
|
|
|
$this->renderedBody = $body; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
1 |
|
public function getBodyText() |
265
|
|
|
{ |
266
|
1 |
|
$this->renderBodyText(); |
267
|
|
|
return $this->renderedBody; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param ContainerInterface $container |
272
|
|
|
* |
273
|
|
|
* @return static |
274
|
|
|
*/ |
275
|
1 |
|
public static function factory(ContainerInterface $container) |
276
|
|
|
{ |
277
|
1 |
|
return new static($container); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.