[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/Swiftmailer/classes/Swift/KeyCache/ -> ArrayKeyCache.php (source)

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


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1