|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of SwiftMailer. |
|
5 * (c) 2004-2009 Chris Corbyn |
|
6 * |
|
7 * For the full copyright and license information, please view the LICENSE |
|
8 * file that was distributed with this source code. |
|
9 */ |
|
10 |
|
11 |
|
12 /** |
|
13 * A basic KeyCache backed by an array. |
|
14 * @package Swift |
|
15 * @subpackage KeyCache |
|
16 * @author Chris Corbyn |
|
17 */ |
|
18 class Swift_KeyCache_ArrayKeyCache implements Swift_KeyCache |
|
19 { |
|
20 |
|
21 /** |
|
22 * Cache contents. |
|
23 * @var array |
|
24 * @access private |
|
25 */ |
|
26 private $_contents = array(); |
|
27 |
|
28 /** |
|
29 * An InputStream for cloning. |
|
30 * @var Swift_KeyCache_KeyCacheInputStream |
|
31 * @access private |
|
32 */ |
|
33 private $_stream; |
|
34 |
|
35 /** |
|
36 * Create a new ArrayKeyCache with the given $stream for cloning to make |
|
37 * InputByteStreams. |
|
38 * @param Swift_KeyCache_KeyCacheInputStream $stream |
|
39 */ |
|
40 public function __construct(Swift_KeyCache_KeyCacheInputStream $stream) |
|
41 { |
|
42 $this->_stream = $stream; |
|
43 } |
|
44 |
|
45 /** |
|
46 * Set a string into the cache under $itemKey for the namespace $nsKey. |
|
47 * @param string $nsKey |
|
48 * @param string $itemKey |
|
49 * @param string $string |
|
50 * @param int $mode |
|
51 * @see MODE_WRITE, MODE_APPEND |
|
52 */ |
|
53 public function setString($nsKey, $itemKey, $string, $mode) |
|
54 { |
|
55 $this->_prepareCache($nsKey); |
|
56 switch ($mode) |
|
57 { |
|
58 case self::MODE_WRITE: |
|
59 $this->_contents[$nsKey][$itemKey] = $string; |
|
60 break; |
|
61 case self::MODE_APPEND: |
|
62 if (!$this->hasKey($nsKey, $itemKey)) |
|
63 { |
|
64 $this->_contents[$nsKey][$itemKey] = ''; |
|
65 } |
|
66 $this->_contents[$nsKey][$itemKey] .= $string; |
|
67 break; |
|
68 default: |
|
69 throw new Swift_SwiftException( |
|
70 'Invalid mode [' . $mode . '] used to set nsKey='. |
|
71 $nsKey . ', itemKey=' . $itemKey |
|
72 ); |
|
73 } |
|
74 } |
|
75 |
|
76 /** |
|
77 * Set a ByteStream into the cache under $itemKey for the namespace $nsKey. |
|
78 * @param string $nsKey |
|
79 * @param string $itemKey |
|
80 * @param Swift_OutputByteStream $os |
|
81 * @param int $mode |
|
82 * @see MODE_WRITE, MODE_APPEND |
|
83 */ |
|
84 public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os, |
|
85 $mode) |
|
86 { |
|
87 $this->_prepareCache($nsKey); |
|
88 switch ($mode) |
|
89 { |
|
90 case self::MODE_WRITE: |
|
91 $this->clearKey($nsKey, $itemKey); |
|
92 case self::MODE_APPEND: |
|
93 if (!$this->hasKey($nsKey, $itemKey)) |
|
94 { |
|
95 $this->_contents[$nsKey][$itemKey] = ''; |
|
96 } |
|
97 while (false !== $bytes = $os->read(8192)) |
|
98 { |
|
99 $this->_contents[$nsKey][$itemKey] .= $bytes; |
|
100 } |
|
101 break; |
|
102 default: |
|
103 throw new Swift_SwiftException( |
|
104 'Invalid mode [' . $mode . '] used to set nsKey='. |
|
105 $nsKey . ', itemKey=' . $itemKey |
|
106 ); |
|
107 } |
|
108 } |
|
109 |
|
110 /** |
|
111 * Provides a ByteStream which when written to, writes data to $itemKey. |
|
112 * NOTE: The stream will always write in append mode. |
|
113 * @param string $nsKey |
|
114 * @param string $itemKey |
|
115 * @return Swift_InputByteStream |
|
116 */ |
|
117 public function getInputByteStream($nsKey, $itemKey, |
|
118 Swift_InputByteStream $writeThrough = null) |
|
119 { |
|
120 $is = clone $this->_stream; |
|
121 $is->setKeyCache($this); |
|
122 $is->setNsKey($nsKey); |
|
123 $is->setItemKey($itemKey); |
|
124 if (isset($writeThrough)) |
|
125 { |
|
126 $is->setWriteThroughStream($writeThrough); |
|
127 } |
|
128 return $is; |
|
129 } |
|
130 |
|
131 /** |
|
132 * Get data back out of the cache as a string. |
|
133 * @param string $nsKey |
|
134 * @param string $itemKey |
|
135 * @return string |
|
136 */ |
|
137 public function getString($nsKey, $itemKey) |
|
138 { |
|
139 $this->_prepareCache($nsKey); |
|
140 if ($this->hasKey($nsKey, $itemKey)) |
|
141 { |
|
142 return $this->_contents[$nsKey][$itemKey]; |
|
143 } |
|
144 } |
|
145 |
|
146 /** |
|
147 * Get data back out of the cache as a ByteStream. |
|
148 * @param string $nsKey |
|
149 * @param string $itemKey |
|
150 * @param Swift_InputByteStream $is to write the data to |
|
151 */ |
|
152 public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is) |
|
153 { |
|
154 $this->_prepareCache($nsKey); |
|
155 $is->write($this->getString($nsKey, $itemKey)); |
|
156 } |
|
157 |
|
158 /** |
|
159 * Check if the given $itemKey exists in the namespace $nsKey. |
|
160 * @param string $nsKey |
|
161 * @param string $itemKey |
|
162 * @return boolean |
|
163 */ |
|
164 public function hasKey($nsKey, $itemKey) |
|
165 { |
|
166 $this->_prepareCache($nsKey); |
|
167 return array_key_exists($itemKey, $this->_contents[$nsKey]); |
|
168 } |
|
169 |
|
170 /** |
|
171 * Clear data for $itemKey in the namespace $nsKey if it exists. |
|
172 * @param string $nsKey |
|
173 * @param string $itemKey |
|
174 */ |
|
175 public function clearKey($nsKey, $itemKey) |
|
176 { |
|
177 unset($this->_contents[$nsKey][$itemKey]); |
|
178 } |
|
179 |
|
180 /** |
|
181 * Clear all data in the namespace $nsKey if it exists. |
|
182 * @param string $nsKey |
|
183 */ |
|
184 public function clearAll($nsKey) |
|
185 { |
|
186 unset($this->_contents[$nsKey]); |
|
187 } |
|
188 |
|
189 // -- Private methods |
|
190 |
|
191 /** |
|
192 * Initialize the namespace of $nsKey if needed. |
|
193 * @param string $nsKey |
|
194 * @access private |
|
195 */ |
|
196 private function _prepareCache($nsKey) |
|
197 { |
|
198 if (!array_key_exists($nsKey, $this->_contents)) |
|
199 { |
|
200 $this->_contents[$nsKey] = array(); |
|
201 } |
|
202 } |
|
203 |
|
204 } |