[ Index ]

MailPress 7.2

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

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/KeyCache/ -> DiskKeyCache.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 KeyCache which streams to and from disk.
  13   *
  14   * @author Chris Corbyn
  15   */
  16  class Swift_KeyCache_DiskKeyCache implements Swift_KeyCache
  17  {
  18      /** Signal to place pointer at start of file */
  19      const POSITION_START = 0;
  20  
  21      /** Signal to place pointer at end of file */
  22      const POSITION_END = 1;
  23  
  24      /** Signal to leave pointer in whatever position it currently is */
  25      const POSITION_CURRENT = 2;
  26  
  27      /**
  28       * An InputStream for cloning.
  29       *
  30       * @var Swift_KeyCache_KeyCacheInputStream
  31       */
  32      private $stream;
  33  
  34      /**
  35       * A path to write to.
  36       *
  37       * @var string
  38       */
  39      private $path;
  40  
  41      /**
  42       * Stored keys.
  43       *
  44       * @var array
  45       */
  46      private $keys = [];
  47  
  48      /**
  49       * Create a new DiskKeyCache with the given $stream for cloning to make
  50       * InputByteStreams, and the given $path to save to.
  51       *
  52       * @param string $path to save to
  53       */
  54      public function __construct(Swift_KeyCache_KeyCacheInputStream $stream, $path)
  55      {
  56          $this->stream = $stream;
  57          $this->path = $path;
  58      }
  59  
  60      /**
  61       * Set a string into the cache under $itemKey for the namespace $nsKey.
  62       *
  63       * @see MODE_WRITE, MODE_APPEND
  64       *
  65       * @param string $nsKey
  66       * @param string $itemKey
  67       * @param string $string
  68       * @param int    $mode
  69       *
  70       * @throws Swift_IoException
  71       */
  72      public function setString($nsKey, $itemKey, $string, $mode)
  73      {
  74          $this->prepareCache($nsKey);
  75          switch ($mode) {
  76              case self::MODE_WRITE:
  77                  $fp = $this->getHandle($nsKey, $itemKey, self::POSITION_START);
  78                  break;
  79              case self::MODE_APPEND:
  80                  $fp = $this->getHandle($nsKey, $itemKey, self::POSITION_END);
  81                  break;
  82              default:
  83                  throw new Swift_SwiftException('Invalid mode ['.$mode.'] used to set nsKey='.$nsKey.', itemKey='.$itemKey);
  84                  break;
  85          }
  86          fwrite($fp, $string);
  87          $this->freeHandle($nsKey, $itemKey);
  88      }
  89  
  90      /**
  91       * Set a ByteStream into the cache under $itemKey for the namespace $nsKey.
  92       *
  93       * @see MODE_WRITE, MODE_APPEND
  94       *
  95       * @param string $nsKey
  96       * @param string $itemKey
  97       * @param int    $mode
  98       *
  99       * @throws Swift_IoException
 100       */
 101      public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os, $mode)
 102      {
 103          $this->prepareCache($nsKey);
 104          switch ($mode) {
 105              case self::MODE_WRITE:
 106                  $fp = $this->getHandle($nsKey, $itemKey, self::POSITION_START);
 107                  break;
 108              case self::MODE_APPEND:
 109                  $fp = $this->getHandle($nsKey, $itemKey, self::POSITION_END);
 110                  break;
 111              default:
 112                  throw new Swift_SwiftException('Invalid mode ['.$mode.'] used to set nsKey='.$nsKey.', itemKey='.$itemKey);
 113                  break;
 114          }
 115          while (false !== $bytes = $os->read(8192)) {
 116              fwrite($fp, $bytes);
 117          }
 118          $this->freeHandle($nsKey, $itemKey);
 119      }
 120  
 121      /**
 122       * Provides a ByteStream which when written to, writes data to $itemKey.
 123       *
 124       * NOTE: The stream will always write in append mode.
 125       *
 126       * @param string $nsKey
 127       * @param string $itemKey
 128       *
 129       * @return Swift_InputByteStream
 130       */
 131      public function getInputByteStream($nsKey, $itemKey, Swift_InputByteStream $writeThrough = null)
 132      {
 133          $is = clone $this->stream;
 134          $is->setKeyCache($this);
 135          $is->setNsKey($nsKey);
 136          $is->setItemKey($itemKey);
 137          if (isset($writeThrough)) {
 138              $is->setWriteThroughStream($writeThrough);
 139          }
 140  
 141          return $is;
 142      }
 143  
 144      /**
 145       * Get data back out of the cache as a string.
 146       *
 147       * @param string $nsKey
 148       * @param string $itemKey
 149       *
 150       * @throws Swift_IoException
 151       *
 152       * @return string
 153       */
 154      public function getString($nsKey, $itemKey)
 155      {
 156          $this->prepareCache($nsKey);
 157          if ($this->hasKey($nsKey, $itemKey)) {
 158              $fp = $this->getHandle($nsKey, $itemKey, self::POSITION_START);
 159              $str = '';
 160              while (!feof($fp) && false !== $bytes = fread($fp, 8192)) {
 161                  $str .= $bytes;
 162              }
 163              $this->freeHandle($nsKey, $itemKey);
 164  
 165              return $str;
 166          }
 167      }
 168  
 169      /**
 170       * Get data back out of the cache as a ByteStream.
 171       *
 172       * @param string                $nsKey
 173       * @param string                $itemKey
 174       * @param Swift_InputByteStream $is      to write the data to
 175       */
 176      public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is)
 177      {
 178          if ($this->hasKey($nsKey, $itemKey)) {
 179              $fp = $this->getHandle($nsKey, $itemKey, self::POSITION_START);
 180              while (!feof($fp) && false !== $bytes = fread($fp, 8192)) {
 181                  $is->write($bytes);
 182              }
 183              $this->freeHandle($nsKey, $itemKey);
 184          }
 185      }
 186  
 187      /**
 188       * Check if the given $itemKey exists in the namespace $nsKey.
 189       *
 190       * @param string $nsKey
 191       * @param string $itemKey
 192       *
 193       * @return bool
 194       */
 195      public function hasKey($nsKey, $itemKey)
 196      {
 197          return is_file($this->path.'/'.$nsKey.'/'.$itemKey);
 198      }
 199  
 200      /**
 201       * Clear data for $itemKey in the namespace $nsKey if it exists.
 202       *
 203       * @param string $nsKey
 204       * @param string $itemKey
 205       */
 206      public function clearKey($nsKey, $itemKey)
 207      {
 208          if ($this->hasKey($nsKey, $itemKey)) {
 209              $this->freeHandle($nsKey, $itemKey);
 210              unlink($this->path.'/'.$nsKey.'/'.$itemKey);
 211          }
 212      }
 213  
 214      /**
 215       * Clear all data in the namespace $nsKey if it exists.
 216       *
 217       * @param string $nsKey
 218       */
 219      public function clearAll($nsKey)
 220      {
 221          if (array_key_exists($nsKey, $this->keys)) {
 222              foreach ($this->keys[$nsKey] as $itemKey => $null) {
 223                  $this->clearKey($nsKey, $itemKey);
 224              }
 225              if (is_dir($this->path.'/'.$nsKey)) {
 226                  rmdir($this->path.'/'.$nsKey);
 227              }
 228              unset($this->keys[$nsKey]);
 229          }
 230      }
 231  
 232      /**
 233       * Initialize the namespace of $nsKey if needed.
 234       *
 235       * @param string $nsKey
 236       */
 237      private function prepareCache($nsKey)
 238      {
 239          $cacheDir = $this->path.'/'.$nsKey;
 240          if (!is_dir($cacheDir)) {
 241              if (!mkdir($cacheDir)) {
 242                  throw new Swift_IoException('Failed to create cache directory '.$cacheDir);
 243              }
 244              $this->keys[$nsKey] = [];
 245          }
 246      }
 247  
 248      /**
 249       * Get a file handle on the cache item.
 250       *
 251       * @param string $nsKey
 252       * @param string $itemKey
 253       * @param int    $position
 254       *
 255       * @return resource
 256       */
 257      private function getHandle($nsKey, $itemKey, $position)
 258      {
 259          if (!isset($this->keys[$nsKey][$itemKey])) {
 260              $openMode = $this->hasKey($nsKey, $itemKey) ? 'r+b' : 'w+b';
 261              $fp = fopen($this->path.'/'.$nsKey.'/'.$itemKey, $openMode);
 262              $this->keys[$nsKey][$itemKey] = $fp;
 263          }
 264          if (self::POSITION_START == $position) {
 265              fseek($this->keys[$nsKey][$itemKey], 0, SEEK_SET);
 266          } elseif (self::POSITION_END == $position) {
 267              fseek($this->keys[$nsKey][$itemKey], 0, SEEK_END);
 268          }
 269  
 270          return $this->keys[$nsKey][$itemKey];
 271      }
 272  
 273      private function freeHandle($nsKey, $itemKey)
 274      {
 275          $fp = $this->getHandle($nsKey, $itemKey, self::POSITION_CURRENT);
 276          fclose($fp);
 277          $this->keys[$nsKey][$itemKey] = null;
 278      }
 279  
 280      /**
 281       * Destructor.
 282       */
 283      public function __destruct()
 284      {
 285          foreach ($this->keys as $nsKey => $null) {
 286              $this->clearAll($nsKey);
 287          }
 288      }
 289  }


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1