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() |
|
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | 5 | public function close() |
|
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | 1 | public function detach() |
|
77 | { |
||
78 | 1 | $this->isDetached = true; |
|
79 | 1 | $socket = $this->socket; |
|
80 | 1 | $this->socket = null; |
|
81 | |||
82 | 1 | return $socket; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | 58 | public function getSize() |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | 1 | public function tell() |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 1 | public function eof() |
|
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | 1 | public function isSeekable() |
|
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 1 | public function seek($offset, $whence = SEEK_SET) |
|
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | 1 | public function rewind() |
|
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | 1 | public function isWritable() |
|
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | 1 | public function write($string) |
|
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | 1 | public function isReadable() |
|
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | 4 | public function read($length) |
|
161 | { |
||
162 | 4 | if (null === $this->getSize()) { |
|
163 | return fread($this->socket, $length); |
||
164 | } |
||
165 | |||
166 | 4 | if ($this->getSize() === $this->readed) { |
|
167 | 1 | return ''; |
|
168 | } |
||
169 | |||
170 | // Even if we request a length a non blocking stream can return less data than asked |
||
171 | 4 | $read = fread($this->socket, $length); |
|
172 | |||
173 | 4 | if ($this->getMetadata('timed_out')) { |
|
174 | throw new TimeoutException('Stream timed out while reading data'); |
||
175 | } |
||
176 | |||
177 | 4 | $this->readed += strlen($read); |
|
178 | |||
179 | 4 | return $read; |
|
180 | } |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | 57 | public function getContents() |
|
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | */ |
||
203 | 5 | public function getMetadata($key = null) |
|
213 | } |
||
214 |