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

http://zoop.googlecode.com/ · PHP · 209 lines · 103 code · 17 blank · 89 comment · 8 complexity · f69462e76a1712e53cb9f7a38b889647 MD5 · raw file

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