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