ArrayKeyCache.php 5.6KB

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