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