Overview
  • Namespace
  • Class

Namespaces

  • Http
    • Message
      • Authentication
      • Decorator
      • Encoding
        • Filter
      • Formatter
      • MessageFactory
      • StreamFactory
      • UriFactory

Classes

  • Http\Message\Authentication\BasicAuth
  • Http\Message\Authentication\Bearer
  • Http\Message\Authentication\Chain
  • Http\Message\Authentication\Matching
  • Http\Message\Authentication\QueryParam
  • Http\Message\Authentication\Wsse
  • Http\Message\Encoding\ChunkStream
  • Http\Message\Encoding\CompressStream
  • Http\Message\Encoding\DechunkStream
  • Http\Message\Encoding\DecompressStream
  • Http\Message\Encoding\DeflateStream
  • Http\Message\Encoding\Filter\Chunk
  • Http\Message\Encoding\FilteredStream
  • Http\Message\Encoding\GzipDecodeStream
  • Http\Message\Encoding\GzipEncodeStream
  • Http\Message\Encoding\InflateStream
  • Http\Message\Formatter\SimpleFormatter
  • Http\Message\MessageFactory\DiactorosMessageFactory
  • Http\Message\MessageFactory\GuzzleMessageFactory
  • Http\Message\StreamFactory\DiactorosStreamFactory
  • Http\Message\StreamFactory\GuzzleStreamFactory
  • Http\Message\UriFactory\DiactorosUriFactory
  • Http\Message\UriFactory\GuzzleUriFactory

Interfaces

  • Http\Message\Authentication
  • Http\Message\Formatter

Traits

  • Http\Message\Decorator\MessageDecorator
  • Http\Message\Decorator\RequestDecorator
  • Http\Message\Decorator\ResponseDecorator
  • Http\Message\Decorator\StreamDecorator
  1 <?php
  2 
  3 namespace Http\Message\Encoding;
  4 
  5 use Clue\StreamFilter as Filter;
  6 use Http\Message\Decorator\StreamDecorator;
  7 use Psr\Http\Message\StreamInterface;
  8 
  9 /**
 10  * A filtered stream has a filter for filtering output and a filter for filtering input made to a underlying stream.
 11  *
 12  * @author Joel Wurtz <joel.wurtz@gmail.com>
 13  */
 14 abstract class FilteredStream implements StreamInterface
 15 {
 16     const BUFFER_SIZE = 65536;
 17 
 18     use StreamDecorator;
 19 
 20     /**
 21      * @var callable
 22      */
 23     protected $readFilterCallback;
 24 
 25     /**
 26      * @var resource
 27      */
 28     protected $readFilter;
 29 
 30     /**
 31      * @var callable
 32      */
 33     protected $writeFilterCallback;
 34 
 35     /**
 36      * @var resource
 37      */
 38     protected $writeFilter;
 39 
 40     /**
 41      * Internal buffer.
 42      *
 43      * @var string
 44      */
 45     protected $buffer = '';
 46 
 47     /**
 48      * @param StreamInterface $stream
 49      * @param mixed|null      $readFilterOptions
 50      * @param mixed|null      $writeFilterOptions
 51      */
 52     public function __construct(StreamInterface $stream, $readFilterOptions = null, $writeFilterOptions = null)
 53     {
 54         $this->readFilterCallback = Filter\fun($this->getReadFilter(), $readFilterOptions);
 55         $this->writeFilterCallback = Filter\fun($this->getWriteFilter(), $writeFilterOptions);
 56         $this->stream = $stream;
 57     }
 58 
 59     /**
 60      * {@inheritdoc}
 61      */
 62     public function read($length)
 63     {
 64         if (strlen($this->buffer) >= $length) {
 65             $read = substr($this->buffer, 0, $length);
 66             $this->buffer = substr($this->buffer, $length);
 67 
 68             return $read;
 69         }
 70 
 71         if ($this->stream->eof()) {
 72             $buffer = $this->buffer;
 73             $this->buffer = '';
 74 
 75             return $buffer;
 76         }
 77 
 78         $read = $this->buffer;
 79         $this->buffer = '';
 80         $this->fill();
 81 
 82         return $read.$this->read($length - strlen($read));
 83     }
 84 
 85     /**
 86      * {@inheritdoc}
 87      */
 88     public function eof()
 89     {
 90         return $this->stream->eof() && $this->buffer === '';
 91     }
 92 
 93     /**
 94      * Buffer is filled by reading underlying stream.
 95      *
 96      * Callback is reading once more even if the stream is ended.
 97      * This allow to get last data in the PHP buffer otherwise this
 98      * bug is present : https://bugs.php.net/bug.php?id=48725
 99      */
100     protected function fill()
101     {
102         $readFilterCallback = $this->readFilterCallback;
103         $this->buffer      .= $readFilterCallback($this->stream->read(self::BUFFER_SIZE));
104 
105         if ($this->stream->eof()) {
106             $this->buffer .= $readFilterCallback();
107         }
108     }
109 
110     /**
111      * {@inheritdoc}
112      */
113     public function getContents()
114     {
115         $buffer = '';
116 
117         while (!$this->eof()) {
118             $buf = $this->read(1048576);
119             // Using a loose equality here to match on '' and false.
120             if ($buf == null) {
121                 break;
122             }
123 
124             $buffer .= $buf;
125         }
126 
127         return $buffer;
128     }
129 
130     /**
131      * Returns the read filter name.
132      *
133      * @return string
134      */
135     abstract public function getReadFilter();
136 
137     /**
138      * Returns the write filter name.
139      *
140      * @return string
141      */
142     abstract public function getWriteFilter();
143 }
144 
API documentation generated by ApiGen