|
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
|
|
|
|
|
8
|
|
|
namespace ZBateson\MailMimeParser\Parser; |
|
9
|
|
|
|
|
10
|
|
|
use ZBateson\MailMimeParser\Parser\Proxy\ParserPartProxy; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Manages a prioritized list of IParser objects for parsing messages and parts |
|
14
|
|
|
* and creating proxied parts. |
|
15
|
|
|
* |
|
16
|
|
|
* The default ParserManager sets up a MimeParser in priority 0, and a |
|
17
|
|
|
* NonMimeParser in priority 1. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Zaahid Bateson |
|
20
|
|
|
*/ |
|
21
|
|
|
class ParserManager |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var IParser[] List of parsers in order of priority (0 is highest |
|
25
|
|
|
* priority). |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $parsers = []; |
|
28
|
|
|
|
|
29
|
6 |
|
public function __construct(MimeParser $mimeParser, NonMimeParser $nonMimeParser) |
|
30
|
|
|
{ |
|
31
|
6 |
|
$this->setParsers([$mimeParser, $nonMimeParser]); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Overrides the internal prioritized list of parses with the passed list, |
|
36
|
|
|
* calling $parser->setParserManager($this) on each one. |
|
37
|
|
|
* |
|
38
|
|
|
*/ |
|
39
|
6 |
|
public function setParsers(array $parsers) : self |
|
40
|
|
|
{ |
|
41
|
6 |
|
foreach ($parsers as $parser) { |
|
42
|
6 |
|
$parser->setParserManager($this); |
|
43
|
|
|
} |
|
44
|
6 |
|
$this->parsers = $parsers; |
|
45
|
6 |
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Adds an IParser at the highest priority (up front), calling |
|
50
|
|
|
* $parser->setParserManager($this) on it. |
|
51
|
|
|
* |
|
52
|
|
|
* @param IParser $parser The parser to add. |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function prependParser(IParser $parser) : self |
|
55
|
|
|
{ |
|
56
|
1 |
|
$parser->setParserManager($this); |
|
57
|
1 |
|
\array_unshift($this->parsers, $parser); |
|
58
|
1 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Creates a ParserPartProxy for the passed $partBuilder using a compatible |
|
63
|
|
|
* IParser. |
|
64
|
|
|
* |
|
65
|
|
|
* Loops through registered IParsers calling 'canParse()' on each with the |
|
66
|
|
|
* passed PartBuilder, then calling either 'getParserMessageProxyFactory()' |
|
67
|
|
|
* or 'getParserPartProxyFactory()' depending on if the PartBuilder has a |
|
68
|
|
|
* parent, and finally calling 'newInstance' on the returned |
|
69
|
|
|
* ParserPartProxyFactory passing it the IParser, and returning the new |
|
70
|
|
|
* ParserPartProxy instance that was created. |
|
71
|
|
|
* |
|
72
|
|
|
* @param PartBuilder $partBuilder The PartBuilder to wrap in a proxy with |
|
73
|
|
|
* an IParser |
|
74
|
|
|
* @return ?ParserPartProxy The created ParserPartProxy tied to a new |
|
75
|
|
|
* IMessagePart and associated IParser. |
|
76
|
|
|
*/ |
|
77
|
106 |
|
public function createParserProxyFor(PartBuilder $partBuilder) |
|
78
|
|
|
{ |
|
79
|
106 |
|
foreach ($this->parsers as $parser) { |
|
80
|
106 |
|
if ($parser->canParse($partBuilder)) { |
|
81
|
106 |
|
$factory = ($partBuilder->getParent() === null) ? |
|
82
|
105 |
|
$parser->getParserMessageProxyFactory() : |
|
83
|
106 |
|
$parser->getParserPartProxyFactory(); |
|
84
|
106 |
|
return $factory->newInstance($partBuilder, $parser); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
return null; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|