|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) 2015 ublaboo <[email protected]> |
|
5
|
|
|
* @author Pavel Janda <[email protected]> |
|
6
|
|
|
* @package Ublaboo |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Ublaboo\Mailing; |
|
10
|
|
|
|
|
11
|
|
|
use Nette; |
|
12
|
|
|
use Ublaboo; |
|
13
|
|
|
use Ublaboo\Mailing\Exception\MailingException; |
|
14
|
|
|
|
|
15
|
|
|
abstract class Mail extends Nette\Object |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
const CONFIG_LOG = 'log'; |
|
19
|
|
|
const CONFIG_SEND = 'send'; |
|
20
|
|
|
const CONFIG_BOTH = 'both'; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $config; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $mails; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var Nette\Mail\IMailer |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $mailer; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Nette\Mail\Message |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $message; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $args; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var Nette\Application\LinkGenerator |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $linkGenerator; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var ILogger |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $logger; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var Nette\Application\UI\ITemplate |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $template; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var string |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $underscore_name; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var string |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $log_type; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var string |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $mail_images_base_path; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var string |
|
80
|
|
|
*/ |
|
81
|
|
|
protected $template_file; |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
public function __construct( |
|
85
|
|
|
$config, |
|
86
|
|
|
$mails, |
|
87
|
|
|
Nette\Mail\IMailer $mailer, |
|
88
|
|
|
Nette\Mail\Message $message, |
|
89
|
|
|
Nette\Application\LinkGenerator $linkGenerator, |
|
90
|
|
|
Nette\Application\UI\ITemplateFactory $templateFactory, |
|
91
|
|
|
ILogger $logger, |
|
92
|
|
|
$args |
|
93
|
|
|
) { |
|
94
|
|
|
$this->config = $config; |
|
95
|
|
|
$this->mails = $mails; |
|
96
|
|
|
$this->mailer = $mailer; |
|
97
|
|
|
$this->message = $message; |
|
98
|
|
|
$this->linkGenerator = $linkGenerator; |
|
99
|
|
|
$this->logger = $logger; |
|
100
|
|
|
$this->args = $args; |
|
101
|
|
|
|
|
102
|
|
|
$this->template = $templateFactory->createTemplate(); |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Initiate mail composing |
|
106
|
|
|
*/ |
|
107
|
|
|
if ($this instanceof IComposableMail) { |
|
108
|
|
|
$this->compose($this->message, $this->args); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Set template file |
|
115
|
|
|
* @return void |
|
116
|
|
|
*/ |
|
117
|
|
|
public function setTemplateFile($template_file) |
|
118
|
|
|
{ |
|
119
|
|
|
$this->template_file = (string) $template_file; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Set template variables |
|
125
|
|
|
* @return void |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function setTemplateVariables() |
|
128
|
|
|
{ |
|
129
|
|
|
foreach ($this->args as $key => $value) { |
|
130
|
|
|
$this->template->$key = $value; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Set absolute base path for images |
|
137
|
|
|
* @param string $mail_images_base_path |
|
138
|
|
|
*/ |
|
139
|
|
|
public function setBasePath($mail_images_base_path) |
|
140
|
|
|
{ |
|
141
|
|
|
$this->mail_images_base_path = (string) $mail_images_base_path; |
|
142
|
|
|
|
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Stick to convention that Email: |
|
149
|
|
|
* ?/mailing/Mails/FooMail.php |
|
150
|
|
|
* |
|
151
|
|
|
* will have template with path of: |
|
152
|
|
|
* ?/mailing/Mails/templates/foo_mail.latte |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getTemplateFile() |
|
157
|
|
|
{ |
|
158
|
|
|
if ($this->template_file) { |
|
159
|
|
|
return $this->template_file; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get child class file path |
|
164
|
|
|
* @var \ReflectionClass |
|
165
|
|
|
*/ |
|
166
|
|
|
$reflection = new \ReflectionClass(get_class($this)); |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Split path to directory and file |
|
170
|
|
|
*/ |
|
171
|
|
|
$class_path = $reflection->getFileName(); |
|
172
|
|
|
$class_dir = dirname($class_path); |
|
173
|
|
|
$class_name = pathinfo($class_path, PATHINFO_FILENAME); |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Convert class name to underscore and set latte file extension |
|
177
|
|
|
*/ |
|
178
|
|
|
$this->underscore_name = lcfirst(preg_replace_callback('/(?<=.)([A-Z])/', function ($m) { |
|
179
|
|
|
return '_' . strtolower($m[1]); |
|
180
|
|
|
}, $class_name)); |
|
181
|
|
|
|
|
182
|
|
|
$template_name = $this->underscore_name . '.latte'; |
|
183
|
|
|
$this->log_type = $this->underscore_name; |
|
184
|
|
|
|
|
185
|
|
|
$template_file = "$class_dir/templates/$template_name"; |
|
186
|
|
|
|
|
187
|
|
|
if (!file_exists($template_file)) { |
|
188
|
|
|
throw new MailingException("Error creating template from file [$template_file]", 1); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return $template_file; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Render latte template to string and send (and/or log) mail |
|
197
|
|
|
* @return void |
|
198
|
|
|
*/ |
|
199
|
|
|
public function send() |
|
200
|
|
|
{ |
|
201
|
|
|
/** |
|
202
|
|
|
* Set template variables |
|
203
|
|
|
*/ |
|
204
|
|
|
$this->setTemplateVariables(); |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Set body/html body |
|
208
|
|
|
*/ |
|
209
|
|
|
try { |
|
210
|
|
|
$this->template->setFile($this->getTemplateFile()); |
|
211
|
|
|
$this->message->setHtmlBody((string) $this->template, $this->mail_images_base_path); |
|
212
|
|
|
} catch (MailingException $e) { |
|
213
|
|
|
/** |
|
214
|
|
|
* If mail template was set and not found, bubble exception up |
|
215
|
|
|
*/ |
|
216
|
|
|
if ($this->template_file) { |
|
217
|
|
|
throw $e; |
|
218
|
|
|
} |
|
219
|
|
|
/** |
|
220
|
|
|
* Otherwise just suppose that user has set message body via ::setBody |
|
221
|
|
|
*/ |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* In case mail sending in on, send message |
|
226
|
|
|
*/ |
|
227
|
|
|
if ($this->config === self::CONFIG_BOTH || $this->config === self::CONFIG_SEND) { |
|
228
|
|
|
$this->mailer->send($this->message); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* In case mail logging is turned on, log message |
|
233
|
|
|
*/ |
|
234
|
|
|
if ($this->config === self::CONFIG_LOG || $this->config === self::CONFIG_BOTH) { |
|
235
|
|
|
$this->logger->log($this->log_type, $this->message); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
} |
|
240
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: