ArrayKeyCache.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * @author Chris Corbyn
  13. */
  14. class Swift_KeyCache_ArrayKeyCache implements Swift_KeyCache
  15. {
  16. /**
  17. * Cache contents.
  18. *
  19. * @var array
  20. */
  21. private $_contents = array();
  22. /**
  23. * An InputStream for cloning.
  24. *
  25. * @var Swift_KeyCache_KeyCacheInputStream
  26. */
  27. private $_stream;
  28. /**
  29. * Create a new ArrayKeyCache with the given $stream for cloning to make
  30. * InputByteStreams.
  31. *
  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. *
  41. * @see MODE_WRITE, MODE_APPEND
  42. *
  43. * @param string $nsKey
  44. * @param string $itemKey
  45. * @param string $string
  46. * @param int $mode
  47. */
  48. public function setString($nsKey, $itemKey, $string, $mode)
  49. {
  50. $this->_prepareCache($nsKey);
  51. switch ($mode) {
  52. case self::MODE_WRITE:
  53. $this->_contents[$nsKey][$itemKey] = $string;
  54. break;
  55. case self::MODE_APPEND:
  56. if (!$this->hasKey($nsKey, $itemKey)) {
  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. *
  71. * @see MODE_WRITE, MODE_APPEND
  72. *
  73. * @param string $nsKey
  74. * @param string $itemKey
  75. * @param Swift_OutputByteStream $os
  76. * @param int $mode
  77. */
  78. public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os, $mode)
  79. {
  80. $this->_prepareCache($nsKey);
  81. switch ($mode) {
  82. case self::MODE_WRITE:
  83. $this->clearKey($nsKey, $itemKey);
  84. case self::MODE_APPEND:
  85. if (!$this->hasKey($nsKey, $itemKey)) {
  86. $this->_contents[$nsKey][$itemKey] = '';
  87. }
  88. while (false !== $bytes = $os->read(8192)) {
  89. $this->_contents[$nsKey][$itemKey] .= $bytes;
  90. }
  91. break;
  92. default:
  93. throw new Swift_SwiftException(
  94. 'Invalid mode [' . $mode . '] used to set nsKey='.
  95. $nsKey . ', itemKey=' . $itemKey
  96. );
  97. }
  98. }
  99. /**
  100. * Provides a ByteStream which when written to, writes data to $itemKey.
  101. *
  102. * NOTE: The stream will always write in append mode.
  103. *
  104. * @param string $nsKey
  105. * @param string $itemKey
  106. * @param Swift_InputByteStream $writeThrough
  107. *
  108. * @return Swift_InputByteStream
  109. */
  110. public function getInputByteStream($nsKey, $itemKey, Swift_InputByteStream $writeThrough = null)
  111. {
  112. $is = clone $this->_stream;
  113. $is->setKeyCache($this);
  114. $is->setNsKey($nsKey);
  115. $is->setItemKey($itemKey);
  116. if (isset($writeThrough)) {
  117. $is->setWriteThroughStream($writeThrough);
  118. }
  119. return $is;
  120. }
  121. /**
  122. * Get data back out of the cache as a string.
  123. *
  124. * @param string $nsKey
  125. * @param string $itemKey
  126. *
  127. * @return string
  128. */
  129. public function getString($nsKey, $itemKey)
  130. {
  131. $this->_prepareCache($nsKey);
  132. if ($this->hasKey($nsKey, $itemKey)) {
  133. return $this->_contents[$nsKey][$itemKey];
  134. }
  135. }
  136. /**
  137. * Get data back out of the cache as a ByteStream.
  138. *
  139. * @param string $nsKey
  140. * @param string $itemKey
  141. * @param Swift_InputByteStream $is to write the data to
  142. */
  143. public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is)
  144. {
  145. $this->_prepareCache($nsKey);
  146. $is->write($this->getString($nsKey, $itemKey));
  147. }
  148. /**
  149. * Check if the given $itemKey exists in the namespace $nsKey.
  150. *
  151. * @param string $nsKey
  152. * @param string $itemKey
  153. *
  154. * @return bool
  155. */
  156. public function hasKey($nsKey, $itemKey)
  157. {
  158. $this->_prepareCache($nsKey);
  159. return array_key_exists($itemKey, $this->_contents[$nsKey]);
  160. }
  161. /**
  162. * Clear data for $itemKey in the namespace $nsKey if it exists.
  163. *
  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. *
  174. * @param string $nsKey
  175. */
  176. public function clearAll($nsKey)
  177. {
  178. unset($this->_contents[$nsKey]);
  179. }
  180. /**
  181. * Initialize the namespace of $nsKey if needed.
  182. *
  183. * @param string $nsKey
  184. */
  185. private function _prepareCache($nsKey)
  186. {
  187. if (!array_key_exists($nsKey, $this->_contents)) {
  188. $this->_contents[$nsKey] = array();
  189. }
  190. }
  191. }