1 | <?php |
||
24 | class PartStreamRegistry |
||
25 | { |
||
26 | /** |
||
27 | * @var array Array of handles, with message IDs as keys. |
||
28 | */ |
||
29 | private $registeredHandles; |
||
30 | |||
31 | /** |
||
32 | * @var int[] number of registered part stream handles with message IDs as |
||
33 | * keys |
||
34 | */ |
||
35 | private $numRefCountsForHandles; |
||
36 | |||
37 | /** |
||
38 | * Registers an ID for the passed resource handle. |
||
39 | * |
||
40 | * @param string $id |
||
41 | * @param resource $handle |
||
42 | */ |
||
43 | 1 | public function register($id, $handle) |
|
44 | { |
||
45 | 1 | if (!isset($this->registeredHandles[$id])) { |
|
46 | 1 | $this->registeredHandles[$id] = $handle; |
|
47 | 1 | $this->numRefCountsForHandles[$id] = 0; |
|
48 | 1 | } |
|
49 | 1 | } |
|
50 | |||
51 | /** |
||
52 | * Unregisters the given message ID and closes the associated resource |
||
53 | * handle. |
||
54 | * |
||
55 | * @param string $id |
||
56 | */ |
||
57 | 1 | protected function unregister($id) |
|
58 | { |
||
59 | 1 | fclose($this->registeredHandles[$id]); |
|
60 | 1 | unset($this->registeredHandles[$id], $this->registeredPartStreamHandles[$id]); |
|
61 | 1 | } |
|
62 | |||
63 | /** |
||
64 | * Increases the reference count for streams using the resource handle |
||
65 | * associated with the message id. |
||
66 | * |
||
67 | * @param int $messageId |
||
68 | */ |
||
69 | 1 | public function increaseHandleRefCount($messageId) |
|
70 | { |
||
71 | 1 | $this->numRefCountsForHandles[$messageId] += 1; |
|
72 | 1 | } |
|
73 | |||
74 | /** |
||
75 | * Decreases the reference count for streams using the resource handle |
||
76 | * associated with the message id. Once the reference count hits 0, |
||
77 | * unregister is called. |
||
78 | * |
||
79 | * @param int $messageId |
||
80 | */ |
||
81 | 1 | public function decreaseHandleRefCount($messageId) |
|
82 | { |
||
83 | 1 | $this->numRefCountsForHandles[$messageId] -= 1; |
|
84 | 1 | if ($this->numRefCountsForHandles[$messageId] === 0) { |
|
85 | 1 | $this->unregister($messageId); |
|
86 | 1 | } |
|
87 | 1 | } |
|
88 | |||
89 | /** |
||
90 | * Returns the resource handle with the passed $id. |
||
91 | * |
||
92 | * @param string $id |
||
93 | * @return resource |
||
94 | */ |
||
95 | 1 | public function get($id) |
|
96 | { |
||
97 | 1 | if (!isset($this->registeredHandles[$id])) { |
|
98 | 1 | return null; |
|
99 | } |
||
100 | 1 | return $this->registeredHandles[$id]; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Attaches a stream filter on the passed resource $handle for the part's |
||
105 | * encoding. |
||
106 | * |
||
107 | * @param \ZBateson\MailMimeParser\Message\MimePart $part |
||
108 | * @param resource $handle |
||
109 | */ |
||
110 | private function attachEncodingFilterToStream(MimePart $part, $handle) |
||
127 | |||
128 | /** |
||
129 | * Attaches a mailmimeparser-encode stream filter based on the part's |
||
130 | * defined charset. |
||
131 | * |
||
132 | * @param \ZBateson\MailMimeParser\Message\MimePart $part |
||
133 | * @param resource $handle |
||
134 | */ |
||
135 | private function attachCharsetFilterToStream(MimePart $part, $handle) |
||
146 | |||
147 | /** |
||
148 | * Creates a part stream handle for the start and end position of the |
||
149 | * message stream, and attaches it to the passed MimePart. |
||
150 | * |
||
151 | * @param MimePart $part |
||
152 | * @param Message $message |
||
153 | * @param int $start |
||
154 | * @param int $end |
||
155 | */ |
||
156 | public function attachContentPartStreamHandle(MimePart $part, Message $message, $start, $end) |
||
169 | |||
170 | /** |
||
171 | * Creates a part stream handle for the start and end position of the |
||
172 | * message stream, and attaches it to the passed MimePart. |
||
173 | * |
||
174 | * @param MimePart $part |
||
175 | * @param Message $message |
||
176 | * @param int $start |
||
177 | * @param int $end |
||
178 | */ |
||
179 | public function attachOriginalPartStreamHandle(MimePart $part, Message $message, $start, $end) |
||
190 | } |
||
191 |