vendor/swiftmailer/swiftmailer/lib/classes/Swift/ByteStream/AbstractFilterableInputStream.php line 91

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