1 | <?php |
||
25 | class Stream implements StreamInterface |
||
26 | { |
||
27 | /** @var resource Underlying socket */ |
||
28 | private $socket; |
||
29 | |||
30 | /** |
||
31 | * @var bool Is stream detached |
||
32 | */ |
||
33 | private $isDetached = false; |
||
34 | |||
35 | /** |
||
36 | * @var int|null Size of the stream, so we know what we must read, null if not available (i.e. a chunked stream) |
||
37 | */ |
||
38 | private $size; |
||
39 | |||
40 | /** |
||
41 | * @var int Size of the stream readed, to avoid reading more than available and have the user blocked |
||
42 | */ |
||
43 | private $readed = 0; |
||
44 | |||
45 | /** |
||
46 | * Create the stream. |
||
47 | * |
||
48 | * @param resource $socket |
||
49 | * @param int $size |
||
50 | */ |
||
51 | 71 | public function __construct($socket, $size = null) |
|
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | 1 | public function __toString() |
|
68 | 5 | ||
69 | /** |
||
70 | 5 | * {@inheritdoc} |
|
71 | 5 | */ |
|
72 | public function close() |
||
76 | 1 | ||
77 | /** |
||
78 | 1 | * {@inheritdoc} |
|
79 | 1 | */ |
|
80 | 1 | public function detach() |
|
88 | 58 | ||
89 | /** |
||
90 | 58 | * {@inheritdoc} |
|
91 | */ |
||
92 | public function getSize() |
||
96 | 1 | ||
97 | /** |
||
98 | 1 | * {@inheritdoc} |
|
99 | */ |
||
100 | public function tell() |
||
104 | 1 | ||
105 | /** |
||
106 | 1 | * {@inheritdoc} |
|
107 | */ |
||
108 | public function eof() |
||
112 | 1 | ||
113 | /** |
||
114 | 1 | * {@inheritdoc} |
|
115 | */ |
||
116 | public function isSeekable() |
||
120 | 1 | ||
121 | /** |
||
122 | 1 | * {@inheritdoc} |
|
123 | */ |
||
124 | public function seek($offset, $whence = SEEK_SET) |
||
128 | 1 | ||
129 | /** |
||
130 | 1 | * {@inheritdoc} |
|
131 | */ |
||
132 | public function rewind() |
||
136 | 1 | ||
137 | /** |
||
138 | 1 | * {@inheritdoc} |
|
139 | */ |
||
140 | public function isWritable() |
||
144 | 1 | ||
145 | /** |
||
146 | 1 | * {@inheritdoc} |
|
147 | */ |
||
148 | public function write($string) |
||
152 | 1 | ||
153 | /** |
||
154 | 1 | * {@inheritdoc} |
|
155 | */ |
||
156 | public function isReadable() |
||
160 | 5 | ||
161 | /** |
||
162 | 5 | * {@inheritdoc} |
|
163 | */ |
||
164 | public function read($length) |
||
165 | { |
||
166 | 5 | if (null === $this->getSize()) { |
|
167 | 1 | return fread($this->socket, $length); |
|
168 | } |
||
169 | |||
170 | if ($this->getSize() === $this->readed) { |
||
171 | 5 | return ''; |
|
172 | } |
||
173 | 5 | ||
174 | 1 | // Even if we request a length a non blocking stream can return less data than asked |
|
175 | $read = fread($this->socket, $length); |
||
176 | |||
177 | 4 | if ($this->getMetadata('timed_out')) { |
|
178 | throw new TimeoutException('Stream timed out while reading data'); |
||
179 | 4 | } |
|
180 | |||
181 | $this->readed += strlen($read); |
||
182 | |||
183 | return $read; |
||
184 | } |
||
185 | 57 | ||
186 | /** |
||
187 | 57 | * {@inheritdoc} |
|
188 | 53 | */ |
|
189 | public function getContents() |
||
203 | 6 | ||
204 | /** |
||
205 | 6 | * {@inheritdoc} |
|
206 | */ |
||
207 | 6 | public function getMetadata($key = null) |
|
217 | } |
||
218 |