[ Index ]

MailPress 7.2

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

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/ByteStream/ -> AbstractFilterableInputStream.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   * Provides the base functionality for an InputStream supporting filters.
  13   *
  14   * @author Chris Corbyn
  15   */
  16  abstract class Swift_ByteStream_AbstractFilterableInputStream implements Swift_InputByteStream, Swift_Filterable
  17  {
  18      /**
  19       * Write sequence.
  20       */
  21      protected $sequence = 0;
  22  
  23      /**
  24       * StreamFilters.
  25       *
  26       * @var Swift_StreamFilter[]
  27       */
  28      private $filters = [];
  29  
  30      /**
  31       * A buffer for writing.
  32       */
  33      private $writeBuffer = '';
  34  
  35      /**
  36       * Bound streams.
  37       *
  38       * @var Swift_InputByteStream[]
  39       */
  40      private $mirrors = [];
  41  
  42      /**
  43       * Commit the given bytes to the storage medium immediately.
  44       *
  45       * @param string $bytes
  46       */
  47      abstract protected function doCommit($bytes);
  48  
  49      /**
  50       * Flush any buffers/content with immediate effect.
  51       */
  52      abstract protected function flush();
  53  
  54      /**
  55       * Add a StreamFilter to this InputByteStream.
  56       *
  57       * @param string $key
  58       */
  59      public function addFilter(Swift_StreamFilter $filter, $key)
  60      {
  61          $this->filters[$key] = $filter;
  62      }
  63  
  64      /**
  65       * Remove an already present StreamFilter based on its $key.
  66       *
  67       * @param string $key
  68       */
  69      public function removeFilter($key)
  70      {
  71          unset($this->filters[$key]);
  72      }
  73  
  74      /**
  75       * Writes $bytes to the end of the stream.
  76       *
  77       * @param string $bytes
  78       *
  79       * @throws Swift_IoException
  80       *
  81       * @return int
  82       */
  83      public function write($bytes)
  84      {
  85          $this->writeBuffer .= $bytes;
  86          foreach ($this->filters as $filter) {
  87              if ($filter->shouldBuffer($this->writeBuffer)) {
  88                  return;
  89              }
  90          }
  91          $this->doWrite($this->writeBuffer);
  92  
  93          return ++$this->sequence;
  94      }
  95  
  96      /**
  97       * For any bytes that are currently buffered inside the stream, force them
  98       * off the buffer.
  99       *
 100       * @throws Swift_IoException
 101       */
 102      public function commit()
 103      {
 104          $this->doWrite($this->writeBuffer);
 105      }
 106  
 107      /**
 108       * Attach $is to this stream.
 109       *
 110       * The stream acts as an observer, receiving all data that is written.
 111       * All {@link write()} and {@link flushBuffers()} operations will be mirrored.
 112       */
 113      public function bind(Swift_InputByteStream $is)
 114      {
 115          $this->mirrors[] = $is;
 116      }
 117  
 118      /**
 119       * Remove an already bound stream.
 120       *
 121       * If $is is not bound, no errors will be raised.
 122       * If the stream currently has any buffered data it will be written to $is
 123       * before unbinding occurs.
 124       */
 125      public function unbind(Swift_InputByteStream $is)
 126      {
 127          foreach ($this->mirrors as $k => $stream) {
 128              if ($is === $stream) {
 129                  if ('' !== $this->writeBuffer) {
 130                      $stream->write($this->writeBuffer);
 131                  }
 132                  unset($this->mirrors[$k]);
 133              }
 134          }
 135      }
 136  
 137      /**
 138       * Flush the contents of the stream (empty it) and set the internal pointer
 139       * to the beginning.
 140       *
 141       * @throws Swift_IoException
 142       */
 143      public function flushBuffers()
 144      {
 145          if ('' !== $this->writeBuffer) {
 146              $this->doWrite($this->writeBuffer);
 147          }
 148          $this->flush();
 149  
 150          foreach ($this->mirrors as $stream) {
 151              $stream->flushBuffers();
 152          }
 153      }
 154  
 155      /** Run $bytes through all filters */
 156      private function filter($bytes)
 157      {
 158          foreach ($this->filters as $filter) {
 159              $bytes = $filter->filter($bytes);
 160          }
 161  
 162          return $bytes;
 163      }
 164  
 165      /** Just write the bytes to the stream */
 166      private function doWrite($bytes)
 167      {
 168          $this->doCommit($this->filter($bytes));
 169  
 170          foreach ($this->mirrors as $stream) {
 171              $stream->write($bytes);
 172          }
 173  
 174          $this->writeBuffer = '';
 175      }
 176  }


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