PageRenderTime 39ms CodeModel.GetById 26ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/swift/lib/classes/Swift/KeyCache/DiskKeyCache.php

http://zoop.googlecode.com/
PHP | 316 lines | 176 code | 23 blank | 117 comment | 22 complexity | a68e5e10fdb4d46b53f1e9ebd9ab8d71 MD5 | raw file
  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//@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';
 17
 18/**
 19 * A KeyCache which streams to and from disk.
 20 * @package Swift
 21 * @subpackage KeyCache
 22 * @author Chris Corbyn
 23 */
 24class Swift_KeyCache_DiskKeyCache implements Swift_KeyCache
 25{
 26
 27  /** Signal to place pointer at start of file */
 28  const POSITION_START = 0;
 29
 30  /** Signal to place pointer at end of file */
 31  const POSITION_END = 1;
 32
 33  /**
 34   * An InputStream for cloning.
 35   * @var Swift_KeyCache_KeyCacheInputStream
 36   * @access private
 37   */
 38  private $_stream;
 39
 40  /**
 41   * A path to write to.
 42   * @var string
 43   * @access private
 44   */
 45  private $_path;
 46
 47  /**
 48   * Stored keys.
 49   * @var array
 50   * @access private
 51   */
 52  private $_keys = array();
 53
 54  /**
 55   * Will be true if magic_quotes_runtime is turned on.
 56   * @var boolean
 57   * @access private
 58   */
 59  private $_quotes = false;
 60
 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
 66   */
 67  public function __construct(Swift_KeyCache_KeyCacheInputStream $stream, $path)
 68  {
 69    $this->_stream = $stream;
 70    $this->_path = $path;
 71    $this->_quotes = get_magic_quotes_runtime();
 72  }
 73
 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
 82   */
 83  public function setString($nsKey, $itemKey, $string, $mode)
 84  {
 85    $this->_prepareCache($nsKey);
 86    switch ($mode)
 87    {
 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
 98          );
 99        break;
100    }
101    fwrite($fp, $string);
102  }
103
104  /**
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
112   */
113  public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os,
114    $mode)
115  {
116    $this->_prepareCache($nsKey);
117    switch ($mode)
118    {
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
129          );
130        break;
131    }
132    while (false !== $bytes = $os->read(8192))
133    {
134      fwrite($fp, $bytes);
135    }
136  }
137
138  /**
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
144   */
145  public function getInputByteStream($nsKey, $itemKey,
146    Swift_InputByteStream $writeThrough = null)
147  {
148    $is = clone $this->_stream;
149    $is->setKeyCache($this);
150    $is->setNsKey($nsKey);
151    $is->setItemKey($itemKey);
152    if (isset($writeThrough))
153    {
154      $is->setWriteThroughStream($writeThrough);
155    }
156    return $is;
157  }
158
159  /**
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
165   */
166  public function getString($nsKey, $itemKey)
167  {
168    $this->_prepareCache($nsKey);
169    if ($this->hasKey($nsKey, $itemKey))
170    {
171      $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
172      if ($this->_quotes)
173      {
174        set_magic_quotes_runtime(0);
175      }
176      $str = '';
177      while (!feof($fp) && false !== $bytes = fread($fp, 8192))
178      {
179        $str .= $bytes;
180      }
181      if ($this->_quotes)
182      {
183        set_magic_quotes_runtime(1);
184      }
185      return $str;
186    }
187  }
188
189  /**
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
194   */
195  public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is)
196  {
197    if ($this->hasKey($nsKey, $itemKey))
198    {
199      $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
200      if ($this->_quotes)
201      {
202        set_magic_quotes_runtime(0);
203      }
204      while (!feof($fp) && false !== $bytes = fread($fp, 8192))
205      {
206        $is->write($bytes);
207      }
208      if ($this->_quotes)
209      {
210        set_magic_quotes_runtime(1);
211      }
212    }
213  }
214
215  /**
216   * Check if the given $itemKey exists in the namespace $nsKey.
217   * @param string $nsKey
218   * @param string $itemKey
219   * @return boolean
220   */
221  public function hasKey($nsKey, $itemKey)
222  {
223    return is_file($this->_path . '/' . $nsKey . '/' . $itemKey);
224  }
225
226  /**
227   * Clear data for $itemKey in the namespace $nsKey if it exists.
228   * @param string $nsKey
229   * @param string $itemKey
230   */
231  public function clearKey($nsKey, $itemKey)
232  {
233    if ($this->hasKey($nsKey, $itemKey))
234    {
235      $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_END);
236      fclose($fp);
237      unlink($this->_path . '/' . $nsKey . '/' . $itemKey);
238    }
239    unset($this->_keys[$nsKey][$itemKey]);
240  }
241
242  /**
243   * Clear all data in the namespace $nsKey if it exists.
244   * @param string $nsKey
245   */
246  public function clearAll($nsKey)
247  {
248    if (array_key_exists($nsKey, $this->_keys))
249    {
250      foreach ($this->_keys[$nsKey] as $itemKey=>$null)
251      {
252        $this->clearKey($nsKey, $itemKey);
253      }
254      rmdir($this->_path . '/' . $nsKey);
255      unset($this->_keys[$nsKey]);
256    }
257  }
258
259  // -- Private methods
260
261  /**
262   * Initialize the namespace of $nsKey if needed.
263   * @param string $nsKey
264   * @access private
265   */
266  private function _prepareCache($nsKey)
267  {
268    $cacheDir = $this->_path . '/' . $nsKey;
269    if (!is_dir($cacheDir))
270    {
271      if (!mkdir($cacheDir))
272      {
273        throw new Swift_IoException('Failed to create cache directory ' . $cacheDir);
274      }
275      $this->_keys[$nsKey] = array();
276    }
277  }
278
279  /**
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
286   */
287  private function _getHandle($nsKey, $itemKey, $position)
288  {
289    if (!isset($this->_keys[$nsKey]) || !array_key_exists($itemKey, $this->_keys[$nsKey]))
290    {
291      $fp = fopen($this->_path . '/' . $nsKey . '/' . $itemKey, 'w+b');
292      $this->_keys[$nsKey][$itemKey] = $fp;
293    }
294    if (self::POSITION_START == $position)
295    {
296      fseek($this->_keys[$nsKey][$itemKey], 0, SEEK_SET);
297    }
298    else
299    {
300      fseek($this->_keys[$nsKey][$itemKey], 0, SEEK_END);
301    }
302    return $this->_keys[$nsKey][$itemKey];
303  }
304
305  /**
306   * Destructor.
307   */
308  public function __destruct()
309  {
310    foreach ($this->_keys as $nsKey=>$null)
311    {
312      $this->clearAll($nsKey);
313    }
314  }
315
316}