[ Index ] |
MailPress 7.2 |
[ Index ] [ Classes ] [ Functions ] [ Variables ] [ Constants ] [ Statistics ] |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of SwiftMailer. 5 * (c) 2004-2009 Chris Corbyn 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11 /** 12 * A basic KeyCache backed by an array. 13 * 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 = []; 24 25 /** 26 * An InputStream for cloning. 27 * 28 * @var Swift_KeyCache_KeyCacheInputStream 29 */ 30 private $stream; 31 32 /** 33 * Create a new ArrayKeyCache with the given $stream for cloning to make 34 * InputByteStreams. 35 */ 36 public function __construct(Swift_KeyCache_KeyCacheInputStream $stream) 37 { 38 $this->stream = $stream; 39 } 40 41 /** 42 * Set a string into the cache under $itemKey for the namespace $nsKey. 43 * 44 * @see MODE_WRITE, MODE_APPEND 45 * 46 * @param string $nsKey 47 * @param string $itemKey 48 * @param string $string 49 * @param int $mode 50 */ 51 public function setString($nsKey, $itemKey, $string, $mode) 52 { 53 $this->prepareCache($nsKey); 54 switch ($mode) { 55 case self::MODE_WRITE: 56 $this->contents[$nsKey][$itemKey] = $string; 57 break; 58 case self::MODE_APPEND: 59 if (!$this->hasKey($nsKey, $itemKey)) { 60 $this->contents[$nsKey][$itemKey] = ''; 61 } 62 $this->contents[$nsKey][$itemKey] .= $string; 63 break; 64 default: 65 throw new Swift_SwiftException('Invalid mode ['.$mode.'] used to set nsKey='.$nsKey.', itemKey='.$itemKey); 66 } 67 } 68 69 /** 70 * Set a ByteStream into the cache under $itemKey for the namespace $nsKey. 71 * 72 * @see MODE_WRITE, MODE_APPEND 73 * 74 * @param string $nsKey 75 * @param string $itemKey 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 // no break 85 case self::MODE_APPEND: 86 if (!$this->hasKey($nsKey, $itemKey)) { 87 $this->contents[$nsKey][$itemKey] = ''; 88 } 89 while (false !== $bytes = $os->read(8192)) { 90 $this->contents[$nsKey][$itemKey] .= $bytes; 91 } 92 break; 93 default: 94 throw new Swift_SwiftException('Invalid mode ['.$mode.'] used to set nsKey='.$nsKey.', itemKey='.$itemKey); 95 } 96 } 97 98 /** 99 * Provides a ByteStream which when written to, writes data to $itemKey. 100 * 101 * NOTE: The stream will always write in append mode. 102 * 103 * @param string $nsKey 104 * @param string $itemKey 105 * 106 * @return Swift_InputByteStream 107 */ 108 public function getInputByteStream($nsKey, $itemKey, Swift_InputByteStream $writeThrough = null) 109 { 110 $is = clone $this->stream; 111 $is->setKeyCache($this); 112 $is->setNsKey($nsKey); 113 $is->setItemKey($itemKey); 114 if (isset($writeThrough)) { 115 $is->setWriteThroughStream($writeThrough); 116 } 117 118 return $is; 119 } 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 /** 138 * Get data back out of the cache as a ByteStream. 139 * 140 * @param string $nsKey 141 * @param string $itemKey 142 * @param Swift_InputByteStream $is to write the data to 143 */ 144 public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is) 145 { 146 $this->prepareCache($nsKey); 147 $is->write($this->getString($nsKey, $itemKey)); 148 } 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 bool 157 */ 158 public function hasKey($nsKey, $itemKey) 159 { 160 $this->prepareCache($nsKey); 161 162 return array_key_exists($itemKey, $this->contents[$nsKey]); 163 } 164 165 /** 166 * Clear data for $itemKey in the namespace $nsKey if it exists. 167 * 168 * @param string $nsKey 169 * @param string $itemKey 170 */ 171 public function clearKey($nsKey, $itemKey) 172 { 173 unset($this->contents[$nsKey][$itemKey]); 174 } 175 176 /** 177 * Clear all data in the namespace $nsKey if it exists. 178 * 179 * @param string $nsKey 180 */ 181 public function clearAll($nsKey) 182 { 183 unset($this->contents[$nsKey]); 184 } 185 186 /** 187 * Initialize the namespace of $nsKey if needed. 188 * 189 * @param string $nsKey 190 */ 191 private function prepareCache($nsKey) 192 { 193 if (!array_key_exists($nsKey, $this->contents)) { 194 $this->contents[$nsKey] = []; 195 } 196 } 197 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue May 19 15:55:14 2020 | Cross-referenced by PHPXref 0.7.1 |