|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the ZBateson\MailMimeParser project. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace ZBateson\MailMimeParser; |
|
8
|
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\Header\Consumer\ConsumerService; |
|
10
|
|
|
use ZBateson\MailMimeParser\Header\HeaderFactory; |
|
11
|
|
|
use ZBateson\MailMimeParser\Stream\PartStream; |
|
12
|
|
|
use ZBateson\MailMimeParser\Stream\UUEncodeStreamFilter; |
|
13
|
|
|
use ZBateson\MailMimeParser\Stream\CharsetStreamFilter; |
|
14
|
|
|
use ZBateson\MailMimeParser\Stream\QuotedPrintableDecodeStreamFilter; |
|
15
|
|
|
use ZBateson\MailMimeParser\Stream\Base64DecodeStreamFilter; |
|
16
|
|
|
use ZBateson\MailMimeParser\Stream\Helper\CharsetConverter; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Dependency injection container for use by ZBateson\MailMimeParser - because a |
|
20
|
|
|
* more complex one seems like overkill. |
|
21
|
|
|
* |
|
22
|
|
|
* Constructs objects and whatever dependencies they require. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Zaahid Bateson |
|
25
|
|
|
*/ |
|
26
|
|
|
class SimpleDi |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var \ZBateson\MailMimeParser\MimePartFactory singleton 'service' instance |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $partFactory; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var \ZBateson\MailMimeParser\Stream\PartStreamRegistry singleton |
|
35
|
|
|
* 'service' instance |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $partStreamRegistry; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var \ZBateson\MailMimeParser\Header\HeaderFactory singleton 'service' |
|
41
|
|
|
* instance |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $headerFactory; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var \ZBateson\MailMimeParser\Header\Part\HeaderPartFactory singleton |
|
47
|
|
|
* 'service' instance |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $headerPartFactory; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var \ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory |
|
53
|
|
|
* singleton 'service' instance |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $mimeLiteralPartFactory; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var \ZBateson\MailMimeParser\Header\Consumer\ConsumerService singleton |
|
59
|
|
|
* 'service' instance |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $consumerService; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Constructs a SimpleDi - call singleton() to invoke |
|
65
|
|
|
*/ |
|
66
|
1 |
|
private function __construct() |
|
67
|
|
|
{ |
|
68
|
1 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Returns the singleton instance. |
|
72
|
|
|
* |
|
73
|
|
|
* @return \ZBateson\MailMimeParser\SimpleDi |
|
74
|
|
|
*/ |
|
75
|
10 |
|
public static function singleton() |
|
76
|
|
|
{ |
|
77
|
10 |
|
static $singleton = null; |
|
78
|
10 |
|
if (empty($singleton)) { |
|
79
|
1 |
|
$singleton = new SimpleDi(); |
|
80
|
1 |
|
} |
|
81
|
10 |
|
return $singleton; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Returns a singleton 'service' instance for the given service named $var |
|
86
|
|
|
* with a class type of $class. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $var the name of the service |
|
89
|
|
|
* @param string $class the name of the class |
|
90
|
|
|
* @return mixed the service object |
|
91
|
|
|
*/ |
|
92
|
4 |
|
protected function getInstance($var, $class) |
|
93
|
|
|
{ |
|
94
|
4 |
|
if ($this->$var === null) { |
|
95
|
1 |
|
$this->$var = new $class(); |
|
96
|
1 |
|
} |
|
97
|
4 |
|
return $this->$var; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Constructs and returns a new MessageParser object. |
|
102
|
|
|
* |
|
103
|
|
|
* @return \ZBateson\MailMimeParser\MessageParser |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function newMessageParser() |
|
106
|
|
|
{ |
|
107
|
1 |
|
return new MessageParser( |
|
108
|
1 |
|
$this->newMessage(), |
|
109
|
1 |
|
$this->getPartFactory(), |
|
110
|
1 |
|
$this->getPartStreamRegistry() |
|
111
|
1 |
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Constructs and returns a new Message object. |
|
116
|
|
|
* |
|
117
|
|
|
* @return \ZBateson\MailMimeParser\Message |
|
118
|
|
|
*/ |
|
119
|
2 |
|
public function newMessage() |
|
120
|
|
|
{ |
|
121
|
2 |
|
return new Message( |
|
122
|
2 |
|
$this->getHeaderFactory() |
|
123
|
2 |
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Constructs and returns a new CharsetConverter object. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $fromCharset source charset |
|
130
|
|
|
* @param string $toCharset destination charset |
|
131
|
|
|
* @return \ZBateson\MailMimeParser\Stream\Helper\CharsetConverter |
|
132
|
|
|
*/ |
|
133
|
1 |
|
public function newCharsetConverter($fromCharset, $toCharset) |
|
134
|
|
|
{ |
|
135
|
1 |
|
return new CharsetConverter( |
|
136
|
1 |
|
$fromCharset, |
|
137
|
|
|
$toCharset |
|
138
|
1 |
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Returns the part factory service instance. |
|
143
|
|
|
* |
|
144
|
|
|
* @return \ZBateson\MailMimeParser\MimePartFactory |
|
145
|
|
|
*/ |
|
146
|
2 |
|
public function getPartFactory() |
|
147
|
|
|
{ |
|
148
|
2 |
|
if ($this->partFactory === null) { |
|
149
|
1 |
|
$this->partFactory = new MimePartFactory( |
|
150
|
1 |
|
$this->getHeaderFactory() |
|
151
|
1 |
|
); |
|
152
|
1 |
|
} |
|
153
|
2 |
|
return $this->partFactory; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Returns the header factory service instance. |
|
158
|
|
|
* |
|
159
|
|
|
* @return \ZBateson\MailMimeParser\Header\HeaderFactory |
|
160
|
|
|
*/ |
|
161
|
3 |
|
public function getHeaderFactory() |
|
162
|
|
|
{ |
|
163
|
3 |
|
if ($this->headerFactory === null) { |
|
164
|
1 |
|
$this->headerFactory = new HeaderFactory($this->getConsumerService()); |
|
165
|
1 |
|
} |
|
166
|
3 |
|
return $this->headerFactory; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Returns the part stream registry service instance. The method also |
|
171
|
|
|
* registers the stream extension by calling registerStreamExtensions. |
|
172
|
|
|
* |
|
173
|
|
|
* @return \ZBateson\MailMimeParser\Stream\PartStreamRegistry |
|
174
|
|
|
*/ |
|
175
|
2 |
|
public function getPartStreamRegistry() |
|
176
|
|
|
{ |
|
177
|
2 |
|
if ($this->partStreamRegistry === null) { |
|
178
|
1 |
|
$this->registerStreamExtensions(); |
|
179
|
1 |
|
} |
|
180
|
2 |
|
return $this->getInstance('partStreamRegistry', __NAMESPACE__ . '\Stream\PartStreamRegistry'); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Returns the part factory service |
|
185
|
|
|
* |
|
186
|
|
|
* @return \ZBateson\MailMimeParser\Header\Part\HeaderPartFactory |
|
187
|
|
|
*/ |
|
188
|
2 |
|
public function getHeaderPartFactory() |
|
189
|
|
|
{ |
|
190
|
2 |
|
return $this->getInstance('headerPartFactory', __NAMESPACE__ . '\Header\Part\HeaderPartFactory'); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Returns the MimeLiteralPartFactory service |
|
195
|
|
|
* |
|
196
|
|
|
* @return \ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory |
|
197
|
|
|
*/ |
|
198
|
2 |
|
public function getMimeLiteralPartFactory() |
|
199
|
|
|
{ |
|
200
|
2 |
|
return $this->getInstance('mimeLiteralPartFactory', __NAMESPACE__ . '\Header\Part\MimeLiteralPartFactory'); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Returns the header consumer service |
|
205
|
|
|
* |
|
206
|
|
|
* @return \ZBateson\MailMimeParser\Header\Consumer\ConsumerService |
|
207
|
|
|
*/ |
|
208
|
2 |
|
public function getConsumerService() |
|
209
|
|
|
{ |
|
210
|
2 |
|
if ($this->consumerService === null) { |
|
211
|
1 |
|
$this->consumerService = new ConsumerService( |
|
212
|
1 |
|
$this->getHeaderPartFactory(), |
|
213
|
1 |
|
$this->getMimeLiteralPartFactory() |
|
214
|
1 |
|
); |
|
215
|
1 |
|
} |
|
216
|
2 |
|
return $this->consumerService; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Registers stream extensions for PartStream and CharsetStreamFilter |
|
221
|
|
|
* |
|
222
|
|
|
* @see stream_filter_register |
|
223
|
|
|
* @see stream_wrapper_register |
|
224
|
|
|
*/ |
|
225
|
1 |
|
protected function registerStreamExtensions() |
|
226
|
|
|
{ |
|
227
|
1 |
|
stream_filter_register(UUEncodeStreamFilter::STREAM_FILTER_NAME, __NAMESPACE__ . '\Stream\UUEncodeStreamFilter'); |
|
228
|
1 |
|
stream_filter_register(CharsetStreamFilter::STREAM_FILTER_NAME, __NAMESPACE__ . '\Stream\CharsetStreamFilter'); |
|
229
|
1 |
|
stream_wrapper_register(PartStream::STREAM_WRAPPER_PROTOCOL, __NAMESPACE__ . '\Stream\PartStream'); |
|
230
|
|
|
|
|
231
|
|
|
// hhvm compatibility -- at time of writing, no convert.* filters |
|
232
|
|
|
// should return false if already registered |
|
233
|
1 |
|
$filters = stream_get_filters(); |
|
234
|
|
|
// @codeCoverageIgnoreStart |
|
235
|
|
|
if (!in_array('convert.*', $filters)) { |
|
236
|
|
|
stream_filter_register( |
|
237
|
|
|
QuotedPrintableDecodeStreamFilter::STREAM_FILTER_NAME, |
|
238
|
|
|
__NAMESPACE__ . '\Stream\QuotedPrintableDecodeStreamFilter' |
|
239
|
|
|
); |
|
240
|
|
|
stream_filter_register( |
|
241
|
|
|
Base64DecodeStreamFilter::STREAM_FILTER_NAME, |
|
242
|
|
|
__NAMESPACE__ . '\Stream\Base64DecodeStreamFilter' |
|
243
|
|
|
); |
|
244
|
|
|
} |
|
245
|
|
|
// @codeCoverageIgnoreEnd |
|
246
|
1 |
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|