ArrayKeyCache.php 5.1 KB

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