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\Message\Part\Factory; |
8
|
|
|
|
9
|
|
|
use ReflectionClass; |
10
|
|
|
use Psr\Http\Message\StreamInterface; |
11
|
|
|
use ZBateson\MailMimeParser\Header\HeaderFactory; |
12
|
|
|
use ZBateson\MailMimeParser\Message\Helper\MessageHelperService; |
13
|
|
|
use ZBateson\MailMimeParser\Message\PartFilterFactory; |
14
|
|
|
use ZBateson\MailMimeParser\Message\Part\PartBuilder; |
15
|
|
|
use ZBateson\MailMimeParser\Stream\StreamFactory; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Abstract factory for subclasses of MessagePart. |
19
|
|
|
* |
20
|
|
|
* @author Zaahid Bateson |
21
|
|
|
*/ |
22
|
|
|
abstract class MessagePartFactory |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var PartStreamFilterManagerFactory responsible for creating |
26
|
|
|
* PartStreamFilterManager instances |
27
|
|
|
*/ |
28
|
|
|
protected $partStreamFilterManagerFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var StreamFactory the StreamFactory instance |
32
|
|
|
*/ |
33
|
|
|
protected $streamFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @static MessagePartFactory[] cached instances of MessagePartFactory |
37
|
|
|
* sub-classes |
38
|
|
|
*/ |
39
|
|
|
private static $instances = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Initializes class dependencies. |
43
|
|
|
* |
44
|
|
|
* @param StreamFactory $streamFactory |
45
|
|
|
* @param PartStreamFilterManagerFactory $psf |
46
|
|
|
*/ |
47
|
3 |
|
public function __construct( |
48
|
|
|
StreamFactory $streamFactory, |
49
|
|
|
PartStreamFilterManagerFactory $psf |
50
|
|
|
) { |
51
|
3 |
|
$this->streamFactory = $streamFactory; |
52
|
3 |
|
$this->partStreamFilterManagerFactory = $psf; |
53
|
3 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Sets a cached singleton instance. |
57
|
|
|
* |
58
|
|
|
* @param MessagePartFactory $instance |
59
|
|
|
*/ |
60
|
|
|
protected static function setCachedInstance(MessagePartFactory $instance) |
61
|
|
|
{ |
62
|
|
|
if (self::$instances === null) { |
63
|
|
|
self::$instances = []; |
64
|
|
|
} |
65
|
|
|
$class = get_called_class(); |
66
|
|
|
self::$instances[$class] = $instance; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns a cached singleton instance if one exists, or null if one hasn't |
71
|
|
|
* been created yet. |
72
|
|
|
* |
73
|
|
|
* @return MessagePartFactory |
74
|
|
|
*/ |
75
|
|
|
protected static function getCachedInstance() |
76
|
|
|
{ |
77
|
|
|
$class = get_called_class(); |
78
|
|
|
if (self::$instances === null || !isset(self::$instances[$class])) { |
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
return self::$instances[$class]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns the singleton instance for the class. |
86
|
|
|
* |
87
|
|
|
* @param StreamFactory $sdf |
88
|
|
|
* @param PartStreamFilterManagerFactory $psf |
89
|
|
|
* @param HeaderFactory $hf |
90
|
|
|
* @param PartFilterFactory $pf |
91
|
|
|
* @param MessageHelperService $mhs |
92
|
|
|
* @return MessagePartFactory |
93
|
|
|
*/ |
94
|
|
|
public static function getInstance( |
95
|
|
|
StreamFactory $sdf, |
|
|
|
|
96
|
|
|
PartStreamFilterManagerFactory $psf, |
|
|
|
|
97
|
|
|
HeaderFactory $hf = null, |
|
|
|
|
98
|
|
|
PartFilterFactory $pf = null, |
|
|
|
|
99
|
|
|
MessageHelperService $mhs = null |
|
|
|
|
100
|
|
|
) { |
101
|
|
|
$instance = static::getCachedInstance(); |
102
|
|
|
if ($instance === null) { |
103
|
|
|
$ref = new ReflectionClass(get_called_class()); |
104
|
|
|
$n = $ref->getConstructor()->getNumberOfParameters(); |
105
|
|
|
$args = []; |
106
|
|
|
for ($i = 0; $i < $n; ++$i) { |
107
|
|
|
$args[] = func_get_arg($i); |
108
|
|
|
} |
109
|
|
|
$instance = $ref->newInstanceArgs($args); |
110
|
|
|
static::setCachedInstance($instance); |
111
|
|
|
} |
112
|
|
|
return $instance; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Constructs a new MessagePart object and returns it |
117
|
|
|
* |
118
|
|
|
* @param PartBuilder $partBuilder |
119
|
|
|
* @param StreamInterface $messageStream |
120
|
|
|
* @return \ZBateson\MailMimeParser\Message\Part\MessagePart |
121
|
|
|
*/ |
122
|
|
|
public abstract function newInstance(PartBuilder $partBuilder, StreamInterface $messageStream = null); |
123
|
|
|
} |
124
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.