Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
stream.php
1<?php
2
4
5use Psr\Http\Message\StreamInterface;
7
8class Stream implements StreamInterface
9{
13 protected $resource;
14
20 public function __construct($stream, $mode = 'r')
21 {
22 if (is_resource($stream))
23 {
24 $this->resource = $stream;
25 }
26 elseif ($stream instanceof Stream)
27 {
28 $this->resource = $stream->resource;
29 }
30 elseif (is_string($stream))
31 {
32 $this->resource = fopen($stream, $mode);
33 }
34 else
35 {
36 throw new ArgumentException('Stream must be a Stream object, a string identifier, or a resource.', 'stream');
37 }
38 }
39
43 public function __toString(): string
44 {
45 if (!$this->isReadable())
46 {
47 return '';
48 }
49
50 try
51 {
52 $this->rewind();
53 return $this->getContents();
54 }
55 catch (\RuntimeException)
56 {
57 return '';
58 }
59 }
60
64 public function close(): void
65 {
66 if ($this->resource)
67 {
68 $resource = $this->detach();
69 fclose($resource);
70 }
71 }
72
76 public function detach()
77 {
79 $this->resource = null;
80 return $resource;
81 }
82
86 public function getSize(): ?int
87 {
88 if ($this->resource !== null)
89 {
90 $stats = fstat($this->resource);
91 return $stats['size'];
92 }
93 return null;
94 }
95
99 public function tell(): int
100 {
101 if (!$this->resource)
102 {
103 throw new \RuntimeException('No resource available, cannot tell position.');
104 }
105
106 $result = ftell($this->resource);
107 if ($result === false)
108 {
109 throw new \RuntimeException('Error occurred during tell operation.');
110 }
111
112 return $result;
113 }
114
118 public function eof(): bool
119 {
120 if ($this->resource)
121 {
122 return feof($this->resource);
123 }
124 return true;
125 }
126
130 public function isSeekable(): bool
131 {
132 if ($this->resource)
133 {
134 return $this->getMetadata('seekable');
135 }
136
137 return false;
138 }
139
143 public function seek(int $offset, int $whence = SEEK_SET): void
144 {
145 if (!$this->resource)
146 {
147 throw new \RuntimeException('No resource available, cannot seek position.');
148 }
149
150 if (!$this->isSeekable())
151 {
152 throw new \RuntimeException('Stream is not seekable.');
153 }
154
155 $result = fseek($this->resource, $offset, $whence);
156
157 if ($result !== 0)
158 {
159 throw new \RuntimeException('Error seeking within stream.');
160 }
161 }
162
166 public function rewind(): void
167 {
168 $this->seek(0);
169 }
170
174 public function isWritable(): bool
175 {
176 if ($this->resource)
177 {
178 return is_writable($this->getMetadata('uri'));
179 }
180
181 return false;
182 }
183
187 public function write(string $string): int
188 {
189 if (!$this->resource)
190 {
191 throw new \RuntimeException('No resource available, cannot write.');
192 }
193
194 $result = fwrite($this->resource, $string);
195
196 if ($result === false)
197 {
198 throw new \RuntimeException('Error writing to stream.');
199 }
200
201 return $result;
202 }
203
207 public function isReadable(): bool
208 {
209 if ($this->resource)
210 {
211 $mode = $this->getMetadata('mode');
212
213 return (str_contains($mode, 'r') || str_contains($mode, '+'));
214 }
215
216 return false;
217 }
218
222 public function read(int $length): string
223 {
224 if (!$this->resource)
225 {
226 throw new \RuntimeException('No resource available, cannot read.');
227 }
228
229 if (!$this->isReadable())
230 {
231 throw new \RuntimeException('Stream is not readable.');
232 }
233
234 $result = fread($this->resource, $length);
235
236 if ($result === false)
237 {
238 throw new \RuntimeException('Error reading stream.');
239 }
240
241 return $result;
242 }
243
247 public function getContents(): string
248 {
249 if (!$this->isReadable())
250 {
251 return '';
252 }
253
254 $result = stream_get_contents($this->resource);
255
256 if ($result === false)
257 {
258 throw new \RuntimeException('Error reading stream.');
259 }
260
261 return $result;
262 }
263
267 public function getMetadata(?string $key = null)
268 {
269 $meta = stream_get_meta_data($this->resource);
270
271 if ($key === null)
272 {
273 return $meta;
274 }
275
276 return $meta[$key] ?? null;
277 }
278
279 public function copyTo($stream)
280 {
281 $this->rewind();
282 return stream_copy_to_stream($this->resource, $stream);
283 }
284}
__construct($stream, $mode='r')
Definition stream.php:20
getMetadata(?string $key=null)
Definition stream.php:267
seek(int $offset, int $whence=SEEK_SET)
Definition stream.php:143
write(string $string)
Definition stream.php:187