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