Merge branch 'maint/7.0'
[ninja.git] / application / vendor / swiftmailer / classes / Swift / KeyCache / DiskKeyCache.php
blob599fd6c7c86f6773093a04a50f1c24c3d92f420b
1 <?php
3 /*
4 * This file is part of SwiftMailer.
5 * (c) 2004-2009 Chris Corbyn
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
11 //@require 'Swift/KeyCache.php';
12 //@require 'Swift/KeyCacheInputStream.php';
13 //@require 'Swift/InputByteStream.php';
14 //@require 'Swift/OutputByteStrean.php';
15 //@require 'Swift/SwiftException.php';
16 //@require 'Swift/IoException.php';
18 /**
19 * A KeyCache which streams to and from disk.
20 * @package Swift
21 * @subpackage KeyCache
22 * @author Chris Corbyn
24 class Swift_KeyCache_DiskKeyCache implements Swift_KeyCache
27 /** Signal to place pointer at start of file */
28 const POSITION_START = 0;
30 /** Signal to place pointer at end of file */
31 const POSITION_END = 1;
33 /**
34 * An InputStream for cloning.
35 * @var Swift_KeyCache_KeyCacheInputStream
36 * @access private
38 private $_stream;
40 /**
41 * A path to write to.
42 * @var string
43 * @access private
45 private $_path;
47 /**
48 * Stored keys.
49 * @var array
50 * @access private
52 private $_keys = array();
54 /**
55 * Will be true if magic_quotes_runtime is turned on.
56 * @var boolean
57 * @access private
59 private $_quotes = false;
61 /**
62 * Create a new DiskKeyCache with the given $stream for cloning to make
63 * InputByteStreams, and the given $path to save to.
64 * @param Swift_KeyCache_KeyCacheInputStream $stream
65 * @param string $path to save to
67 public function __construct(Swift_KeyCache_KeyCacheInputStream $stream, $path)
69 $this->_stream = $stream;
70 $this->_path = $path;
71 $this->_quotes = get_magic_quotes_runtime();
74 /**
75 * Set a string into the cache under $itemKey for the namespace $nsKey.
76 * @param string $nsKey
77 * @param string $itemKey
78 * @param string $string
79 * @param int $mode
80 * @throws Swift_IoException
81 * @see MODE_WRITE, MODE_APPEND
83 public function setString($nsKey, $itemKey, $string, $mode)
85 $this->_prepareCache($nsKey);
86 switch ($mode)
88 case self::MODE_WRITE:
89 $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
90 break;
91 case self::MODE_APPEND:
92 $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_END);
93 break;
94 default:
95 throw new Swift_SwiftException(
96 'Invalid mode [' . $mode . '] used to set nsKey='.
97 $nsKey . ', itemKey=' . $itemKey
99 break;
101 fwrite($fp, $string);
105 * Set a ByteStream into the cache under $itemKey for the namespace $nsKey.
106 * @param string $nsKey
107 * @param string $itemKey
108 * @param Swift_OutputByteStream $os
109 * @param int $mode
110 * @see MODE_WRITE, MODE_APPEND
111 * @throws Swift_IoException
113 public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os,
114 $mode)
116 $this->_prepareCache($nsKey);
117 switch ($mode)
119 case self::MODE_WRITE:
120 $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
121 break;
122 case self::MODE_APPEND:
123 $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_END);
124 break;
125 default:
126 throw new Swift_SwiftException(
127 'Invalid mode [' . $mode . '] used to set nsKey='.
128 $nsKey . ', itemKey=' . $itemKey
130 break;
132 while (false !== $bytes = $os->read(8192))
134 fwrite($fp, $bytes);
139 * Provides a ByteStream which when written to, writes data to $itemKey.
140 * NOTE: The stream will always write in append mode.
141 * @param string $nsKey
142 * @param string $itemKey
143 * @return Swift_InputByteStream
145 public function getInputByteStream($nsKey, $itemKey,
146 Swift_InputByteStream $writeThrough = null)
148 $is = clone $this->_stream;
149 $is->setKeyCache($this);
150 $is->setNsKey($nsKey);
151 $is->setItemKey($itemKey);
152 if (isset($writeThrough))
154 $is->setWriteThroughStream($writeThrough);
156 return $is;
160 * Get data back out of the cache as a string.
161 * @param string $nsKey
162 * @param string $itemKey
163 * @return string
164 * @throws Swift_IoException
166 public function getString($nsKey, $itemKey)
168 $this->_prepareCache($nsKey);
169 if ($this->hasKey($nsKey, $itemKey))
171 $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
172 if ($this->_quotes)
174 set_magic_quotes_runtime(0);
176 $str = '';
177 while (!feof($fp) && false !== $bytes = fread($fp, 8192))
179 $str .= $bytes;
181 if ($this->_quotes)
183 set_magic_quotes_runtime(1);
185 return $str;
190 * Get data back out of the cache as a ByteStream.
191 * @param string $nsKey
192 * @param string $itemKey
193 * @param Swift_InputByteStream $is to write the data to
195 public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is)
197 if ($this->hasKey($nsKey, $itemKey))
199 $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
200 if ($this->_quotes)
202 set_magic_quotes_runtime(0);
204 while (!feof($fp) && false !== $bytes = fread($fp, 8192))
206 $is->write($bytes);
208 if ($this->_quotes)
210 set_magic_quotes_runtime(1);
216 * Check if the given $itemKey exists in the namespace $nsKey.
217 * @param string $nsKey
218 * @param string $itemKey
219 * @return boolean
221 public function hasKey($nsKey, $itemKey)
223 return is_file($this->_path . '/' . $nsKey . '/' . $itemKey);
227 * Clear data for $itemKey in the namespace $nsKey if it exists.
228 * @param string $nsKey
229 * @param string $itemKey
231 public function clearKey($nsKey, $itemKey)
233 if ($this->hasKey($nsKey, $itemKey))
235 $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_END);
236 fclose($fp);
237 unlink($this->_path . '/' . $nsKey . '/' . $itemKey);
239 unset($this->_keys[$nsKey][$itemKey]);
243 * Clear all data in the namespace $nsKey if it exists.
244 * @param string $nsKey
246 public function clearAll($nsKey)
248 if (array_key_exists($nsKey, $this->_keys))
250 foreach ($this->_keys[$nsKey] as $itemKey=>$null)
252 $this->clearKey($nsKey, $itemKey);
254 rmdir($this->_path . '/' . $nsKey);
255 unset($this->_keys[$nsKey]);
259 // -- Private methods
262 * Initialize the namespace of $nsKey if needed.
263 * @param string $nsKey
264 * @access private
266 private function _prepareCache($nsKey)
268 $cacheDir = $this->_path . '/' . $nsKey;
269 if (!is_dir($cacheDir))
271 if (!mkdir($cacheDir))
273 throw new Swift_IoException('Failed to create cache directory ' . $cacheDir);
275 $this->_keys[$nsKey] = array();
280 * Get a file handle on the cache item.
281 * @param string $nsKey
282 * @param string $itemKey
283 * @param int $position
284 * @return resource
285 * @access private
287 private function _getHandle($nsKey, $itemKey, $position)
289 if (!isset($this->_keys[$nsKey]) || !array_key_exists($itemKey, $this->_keys[$nsKey]))
291 $fp = fopen($this->_path . '/' . $nsKey . '/' . $itemKey, 'w+b');
292 $this->_keys[$nsKey][$itemKey] = $fp;
294 if (self::POSITION_START == $position)
296 fseek($this->_keys[$nsKey][$itemKey], 0, SEEK_SET);
298 else
300 fseek($this->_keys[$nsKey][$itemKey], 0, SEEK_END);
302 return $this->_keys[$nsKey][$itemKey];
306 * Destructor.
308 public function __destruct()
310 foreach ($this->_keys as $nsKey=>$null)
312 $this->clearAll($nsKey);