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) |
|
52 | { |
||
53 | 71 | $this->socket = $socket; |
|
54 | 71 | $this->size = $size; |
|
55 | 71 | } |
|
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | 1 | public function __toString() |
|
61 | { |
||
62 | try { |
||
63 | 1 | return $this->getContents(); |
|
64 | } catch (\Exception $e) { |
||
65 | return ''; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | 5 | public function close() |
|
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | 1 | public function detach() |
|
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | 58 | public function getSize() |
|
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | 1 | public function tell() |
|
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 1 | public function eof() |
|
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | 1 | public function isSeekable() |
|
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | 1 | public function seek($offset, $whence = SEEK_SET) |
|
125 | { |
||
126 | 1 | throw new StreamException('This stream is not seekable'); |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | 1 | public function rewind() |
|
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | 1 | public function isWritable() |
|
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | 1 | public function write($string) |
|
149 | { |
||
150 | 1 | throw new StreamException('This stream is not writable'); |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | 1 | public function isReadable() |
|
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | 4 | public function read($length) |
|
165 | { |
||
166 | 4 | if (null === $this->getSize()) { |
|
167 | return fread($this->socket, $length); |
||
168 | } |
||
169 | |||
170 | 4 | if ($this->getSize() === $this->readed) { |
|
171 | 1 | return ''; |
|
172 | } |
||
173 | |||
174 | // Even if we request a length a non blocking stream can return less data than asked |
||
175 | 4 | $read = fread($this->socket, $length); |
|
176 | |||
177 | 4 | if ($this->getMetadata('timed_out')) { |
|
178 | throw new TimeoutException('Stream timed out while reading data'); |
||
179 | } |
||
180 | |||
181 | 4 | $this->readed += strlen($read); |
|
182 | |||
183 | 4 | return $read; |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | 57 | public function getContents() |
|
203 | |||
204 | /** |
||
205 | * {@inheritdoc} |
||
206 | */ |
||
207 | 5 | public function getMetadata($key = null) |
|
217 | } |
||
218 |