1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the ZBateson\MailMimeParser project. |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace ZBateson\MailMimeParser\Parser\Proxy; |
10
|
|
|
|
11
|
|
|
use ZBateson\MailMimeParser\Message; |
12
|
|
|
use ZBateson\MailMimeParser\Message\MessageService; |
13
|
|
|
use ZBateson\MailMimeParser\Message\PartHeaderContainer; |
14
|
|
|
use ZBateson\MailMimeParser\Message\Factory\PartHeaderContainerFactory; |
15
|
|
|
use ZBateson\MailMimeParser\Parser\MimeParserFactory; |
16
|
|
|
use ZBateson\MailMimeParser\Parser\NonMimeParserFactory; |
17
|
|
|
use ZBateson\MailMimeParser\Parser\Part\ParsedPartChildrenContainerFactory; |
18
|
|
|
use ZBateson\MailMimeParser\Parser\Part\ParsedPartStreamContainerFactory; |
19
|
|
|
use ZBateson\MailMimeParser\Parser\PartBuilder; |
20
|
|
|
use ZBateson\MailMimeParser\Stream\StreamFactory; |
21
|
|
|
use Psr\Http\Message\StreamInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Responsible for creating ParsedMessage instances. |
25
|
|
|
* |
26
|
|
|
* @author Zaahid Bateson |
27
|
|
|
*/ |
28
|
|
|
class ParserMessageFactory |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var StreamFactory the StreamFactory instance |
32
|
|
|
*/ |
33
|
|
|
protected $streamFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var PartHeaderContainerFactory |
37
|
|
|
*/ |
38
|
|
|
protected $partHeaderContainerFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ParsedPartStreamContainerFactory |
42
|
|
|
*/ |
43
|
|
|
protected $parsedPartStreamContainerFactory; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var ParsedPartChildrenContainerFactory |
47
|
|
|
*/ |
48
|
|
|
protected $parsedPartChildrenContainerFactory; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var MessageService helper class for message manipulation routines. |
52
|
|
|
*/ |
53
|
|
|
protected $messageService; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var ZBateson\MailMimeParser\Parser\IParserFactory[] |
57
|
|
|
*/ |
58
|
|
|
protected $parserFactories; |
59
|
|
|
|
60
|
|
|
public function __construct( |
61
|
|
|
StreamFactory $sdf, |
62
|
|
|
PartHeaderContainerFactory $phcf, |
63
|
|
|
ParsedPartStreamContainerFactory $pscf, |
64
|
|
|
ParsedPartChildrenContainerFactory $ppccf, |
65
|
|
|
MimeParserFactory $mpf, |
66
|
|
|
NonMimeParserFactory $nmpf, |
67
|
|
|
MessageService $mhs |
68
|
|
|
) { |
69
|
|
|
$this->streamFactory = $sdf; |
70
|
|
|
$this->partHeaderContainerFactory = $phcf; |
71
|
|
|
$this->parsedPartStreamContainerFactory = $pscf; |
72
|
|
|
$this->parsedPartChildrenContainerFactory = $ppccf; |
73
|
|
|
$this->parserFactories = [ $mpf, $nmpf ]; |
|
|
|
|
74
|
|
|
$this->messageService = $mhs; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function prependMessageParser(IParser $parser) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
array_unshift($this->parsers, $parser); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function getMessageParser(PartHeaderContainer $container) |
83
|
|
|
{ |
84
|
|
|
foreach ($this->parserFactories as $pf) { |
85
|
|
|
if ($pf->canParse($container)) { |
86
|
|
|
return $pf->newInstance(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Constructs a new IMessage object and returns it |
94
|
|
|
* |
95
|
|
|
* @param PartBuilder $partBuilder |
96
|
|
|
* @param StreamInterface $stream |
97
|
|
|
* @return \ZBateson\MailMimeParser\Message\IMimePart |
98
|
|
|
*/ |
99
|
|
|
public function newInstance(PartBuilder $partBuilder, PartHeaderContainer $headerContainer) |
100
|
|
|
{ |
101
|
|
|
// changes to headers by the user can't affect parsing which could come |
102
|
|
|
// after a change to headers is made by the user on the Part |
103
|
|
|
$copied = $this->partHeaderContainerFactory->newInstance($headerContainer); |
104
|
|
|
$parserProxy = new ParserMessageProxy($copied, $partBuilder, $this->getMessageParser($headerContainer)); |
|
|
|
|
105
|
|
|
$streamContainer = $this->parsedPartStreamContainerFactory->newInstance($parserProxy); |
106
|
|
|
$childrenContainer = $this->parsedPartChildrenContainerFactory->newInstance($parserProxy); |
107
|
|
|
|
108
|
|
|
$message = new Message( |
109
|
|
|
$streamContainer, |
110
|
|
|
$headerContainer, |
111
|
|
|
$childrenContainer, |
112
|
|
|
$this->messageService |
113
|
|
|
); |
114
|
|
|
$parserProxy->setPart($message); |
115
|
|
|
$parserProxy->setParsedPartStreamContainer($streamContainer); |
116
|
|
|
$parserProxy->setParsedPartChildrenContainer($childrenContainer); |
117
|
|
|
|
118
|
|
|
$streamContainer->setStream($this->streamFactory->newMessagePartStream($message)); |
119
|
|
|
$message->attach($streamContainer); |
120
|
|
|
return $message; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..