1 | <?php |
||
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 null $readFilterOptions |
||
50 | * @param 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 | * Return the read filter name. |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | abstract public function getReadFilter(); |
||
136 | |||
137 | /** |
||
138 | * Return the write filter name. |
||
139 | * |
||
140 | * @return mixed |
||
141 | */ |
||
142 | abstract public function getWriteFilter(); |
||
143 | } |
||
144 |