ArrayKeyCache.php 4.9KB

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