1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16:
17: class Swift_KeyCache_DiskKeyCache implements Swift_KeyCache
18: {
19:
20: const POSITION_START = 0;
21:
22:
23: const POSITION_END = 1;
24:
25:
26: const POSITION_CURRENT = 2;
27:
28: 29: 30: 31: 32:
33: private $_stream;
34:
35: 36: 37: 38: 39:
40: private $_path;
41:
42: 43: 44: 45: 46:
47: private $_keys = array();
48:
49: 50: 51: 52: 53:
54: private $_quotes = false;
55:
56: 57: 58: 59: 60: 61:
62: public function __construct(Swift_KeyCache_KeyCacheInputStream $stream, $path)
63: {
64: $this->_stream = $stream;
65: $this->_path = $path;
66:
67: if (function_exists('get_magic_quotes_runtime') && @get_magic_quotes_runtime() == 1) {
68: $this->_quotes = true;
69: }
70: }
71:
72: 73: 74: 75: 76: 77: 78: 79: 80:
81: public function setString($nsKey, $itemKey, $string, $mode)
82: {
83: $this->_prepareCache($nsKey);
84: switch ($mode) {
85: case self::MODE_WRITE:
86: $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
87: break;
88: case self::MODE_APPEND:
89: $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_END);
90: break;
91: default:
92: throw new Swift_SwiftException(
93: 'Invalid mode [' . $mode . '] used to set nsKey='.
94: $nsKey . ', itemKey=' . $itemKey
95: );
96: break;
97: }
98: fwrite($fp, $string);
99: $this->_freeHandle($nsKey, $itemKey);
100: }
101:
102: 103: 104: 105: 106: 107: 108: 109: 110:
111: public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os, $mode)
112: {
113: $this->_prepareCache($nsKey);
114: switch ($mode) {
115: case self::MODE_WRITE:
116: $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
117: break;
118: case self::MODE_APPEND:
119: $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_END);
120: break;
121: default:
122: throw new Swift_SwiftException(
123: 'Invalid mode [' . $mode . '] used to set nsKey='.
124: $nsKey . ', itemKey=' . $itemKey
125: );
126: break;
127: }
128: while (false !== $bytes = $os->read(8192)) {
129: fwrite($fp, $bytes);
130: }
131: $this->_freeHandle($nsKey, $itemKey);
132: }
133:
134: 135: 136: 137: 138: 139: 140:
141: public function getInputByteStream($nsKey, $itemKey, Swift_InputByteStream $writeThrough = null)
142: {
143: $is = clone $this->_stream;
144: $is->setKeyCache($this);
145: $is->setNsKey($nsKey);
146: $is->setItemKey($itemKey);
147: if (isset($writeThrough)) {
148: $is->setWriteThroughStream($writeThrough);
149: }
150:
151: return $is;
152: }
153:
154: 155: 156: 157: 158: 159: 160:
161: public function getString($nsKey, $itemKey)
162: {
163: $this->_prepareCache($nsKey);
164: if ($this->hasKey($nsKey, $itemKey)) {
165: $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
166: if ($this->_quotes) {
167: ini_set('magic_quotes_runtime', 0);
168: }
169: $str = '';
170: while (!feof($fp) && false !== $bytes = fread($fp, 8192)) {
171: $str .= $bytes;
172: }
173: if ($this->_quotes) {
174: ini_set('magic_quotes_runtime', 1);
175: }
176: $this->_freeHandle($nsKey, $itemKey);
177:
178: return $str;
179: }
180: }
181:
182: 183: 184: 185: 186: 187:
188: public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is)
189: {
190: if ($this->hasKey($nsKey, $itemKey)) {
191: $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
192: if ($this->_quotes) {
193: ini_set('magic_quotes_runtime', 0);
194: }
195: while (!feof($fp) && false !== $bytes = fread($fp, 8192)) {
196: $is->write($bytes);
197: }
198: if ($this->_quotes) {
199: ini_set('magic_quotes_runtime', 1);
200: }
201: $this->_freeHandle($nsKey, $itemKey);
202: }
203: }
204:
205: 206: 207: 208: 209: 210:
211: public function hasKey($nsKey, $itemKey)
212: {
213: return is_file($this->_path . '/' . $nsKey . '/' . $itemKey);
214: }
215:
216: 217: 218: 219: 220:
221: public function clearKey($nsKey, $itemKey)
222: {
223: if ($this->hasKey($nsKey, $itemKey)) {
224: $this->_freeHandle($nsKey, $itemKey);
225: unlink($this->_path . '/' . $nsKey . '/' . $itemKey);
226: }
227: }
228:
229: 230: 231: 232:
233: public function clearAll($nsKey)
234: {
235: if (array_key_exists($nsKey, $this->_keys)) {
236: foreach ($this->_keys[$nsKey] as $itemKey=>$null) {
237: $this->clearKey($nsKey, $itemKey);
238: }
239: if (is_dir($this->_path . '/' . $nsKey)) {
240: rmdir($this->_path . '/' . $nsKey);
241: }
242: unset($this->_keys[$nsKey]);
243: }
244: }
245:
246:
247:
248: 249: 250: 251: 252:
253: private function _prepareCache($nsKey)
254: {
255: $cacheDir = $this->_path . '/' . $nsKey;
256: if (!is_dir($cacheDir)) {
257: if (!mkdir($cacheDir)) {
258: throw new Swift_IoException('Failed to create cache directory ' . $cacheDir);
259: }
260: $this->_keys[$nsKey] = array();
261: }
262: }
263:
264: 265: 266: 267: 268: 269: 270: 271:
272: private function _getHandle($nsKey, $itemKey, $position)
273: {
274: if (!isset($this->_keys[$nsKey][$itemKey])) {
275: $openMode = $this->hasKey($nsKey, $itemKey)
276: ? 'r+b'
277: : 'w+b'
278: ;
279: $fp = fopen($this->_path . '/' . $nsKey . '/' . $itemKey, $openMode);
280: $this->_keys[$nsKey][$itemKey] = $fp;
281: }
282: if (self::POSITION_START == $position) {
283: fseek($this->_keys[$nsKey][$itemKey], 0, SEEK_SET);
284: } elseif (self::POSITION_END == $position) {
285: fseek($this->_keys[$nsKey][$itemKey], 0, SEEK_END);
286: }
287:
288: return $this->_keys[$nsKey][$itemKey];
289: }
290:
291: private function _freeHandle($nsKey, $itemKey)
292: {
293: $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_CURRENT);
294: fclose($fp);
295: $this->_keys[$nsKey][$itemKey] = null;
296: }
297:
298: 299: 300:
301: public function __destruct()
302: {
303: foreach ($this->_keys as $nsKey=>$null) {
304: $this->clearAll($nsKey);
305: }
306: }
307: }
308: