ArrayKeyCache.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. case self::MODE_WRITE:
  51. $this->_contents[$nsKey][$itemKey] = $string;
  52. break;
  53. case self::MODE_APPEND:
  54. if (!$this->hasKey($nsKey, $itemKey)) {
  55. $this->_contents[$nsKey][$itemKey] = '';
  56. }
  57. $this->_contents[$nsKey][$itemKey] .= $string;
  58. break;
  59. default:
  60. throw new Swift_SwiftException(
  61. 'Invalid mode [' . $mode . '] used to set nsKey='.
  62. $nsKey . ', itemKey=' . $itemKey
  63. );
  64. }
  65. }
  66. /**
  67. * Set a ByteStream into the cache under $itemKey for the namespace $nsKey.
  68. * @param string $nsKey
  69. * @param string $itemKey
  70. * @param Swift_OutputByteStream $os
  71. * @param int $mode
  72. * @see MODE_WRITE, MODE_APPEND
  73. */
  74. public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os, $mode)
  75. {
  76. $this->_prepareCache($nsKey);
  77. switch ($mode) {
  78. case self::MODE_WRITE:
  79. $this->clearKey($nsKey, $itemKey);
  80. case self::MODE_APPEND:
  81. if (!$this->hasKey($nsKey, $itemKey)) {
  82. $this->_contents[$nsKey][$itemKey] = '';
  83. }
  84. while (false !== $bytes = $os->read(8192)) {
  85. $this->_contents[$nsKey][$itemKey] .= $bytes;
  86. }
  87. break;
  88. default:
  89. throw new Swift_SwiftException(
  90. 'Invalid mode [' . $mode . '] used to set nsKey='.
  91. $nsKey . ', itemKey=' . $itemKey
  92. );
  93. }
  94. }
  95. /**
  96. * Provides a ByteStream which when written to, writes data to $itemKey.
  97. * NOTE: The stream will always write in append mode.
  98. * @param string $nsKey
  99. * @param string $itemKey
  100. * @return Swift_InputByteStream
  101. */
  102. public function getInputByteStream($nsKey, $itemKey, Swift_InputByteStream $writeThrough = null)
  103. {
  104. $is = clone $this->_stream;
  105. $is->setKeyCache($this);
  106. $is->setNsKey($nsKey);
  107. $is->setItemKey($itemKey);
  108. if (isset($writeThrough)) {
  109. $is->setWriteThroughStream($writeThrough);
  110. }
  111. return $is;
  112. }
  113. /**
  114. * Get data back out of the cache as a string.
  115. * @param string $nsKey
  116. * @param string $itemKey
  117. * @return string
  118. */
  119. public function getString($nsKey, $itemKey)
  120. {
  121. $this->_prepareCache($nsKey);
  122. if ($this->hasKey($nsKey, $itemKey)) {
  123. return $this->_contents[$nsKey][$itemKey];
  124. }
  125. }
  126. /**
  127. * Get data back out of the cache as a ByteStream.
  128. * @param string $nsKey
  129. * @param string $itemKey
  130. * @param Swift_InputByteStream $is to write the data to
  131. */
  132. public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is)
  133. {
  134. $this->_prepareCache($nsKey);
  135. $is->write($this->getString($nsKey, $itemKey));
  136. }
  137. /**
  138. * Check if the given $itemKey exists in the namespace $nsKey.
  139. * @param string $nsKey
  140. * @param string $itemKey
  141. * @return boolean
  142. */
  143. public function hasKey($nsKey, $itemKey)
  144. {
  145. $this->_prepareCache($nsKey);
  146. return array_key_exists($itemKey, $this->_contents[$nsKey]);
  147. }
  148. /**
  149. * Clear data for $itemKey in the namespace $nsKey if it exists.
  150. * @param string $nsKey
  151. * @param string $itemKey
  152. */
  153. public function clearKey($nsKey, $itemKey)
  154. {
  155. unset($this->_contents[$nsKey][$itemKey]);
  156. }
  157. /**
  158. * Clear all data in the namespace $nsKey if it exists.
  159. * @param string $nsKey
  160. */
  161. public function clearAll($nsKey)
  162. {
  163. unset($this->_contents[$nsKey]);
  164. }
  165. // -- Private methods
  166. /**
  167. * Initialize the namespace of $nsKey if needed.
  168. * @param string $nsKey
  169. * @access private
  170. */
  171. private function _prepareCache($nsKey)
  172. {
  173. if (!array_key_exists($nsKey, $this->_contents)) {
  174. $this->_contents[$nsKey] = array();
  175. }
  176. }
  177. }