‪TYPO3CMS  11.5
Stream.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
16 namespace ‪TYPO3\CMS\Core\Http;
17 
18 use Psr\Http\Message\StreamInterface;
19 
28 class ‪Stream implements StreamInterface
29 {
34  protected ‪$resource;
35 
39  protected ‪$stream;
40 
48  public function ‪__construct(‪$stream, $mode = 'r')
49  {
50  $this->stream = ‪$stream;
51  if (is_resource(‪$stream)) {
52  $this->resource = ‪$stream;
53  } elseif (is_string(‪$stream)) {
54  $this->resource = fopen(‪$stream, $mode) ?: null;
55  } else {
56  throw new \InvalidArgumentException('Invalid stream provided; must be a string stream identifier or resource', 1436717284);
57  }
58  }
59 
74  public function ‪__toString()
75  {
76  if (!$this->‪isReadable()) {
77  return '';
78  }
79  try {
80  $this->‪rewind();
81  return $this->‪getContents();
82  } catch (\RuntimeException $e) {
83  return '';
84  }
85  }
86 
90  public function ‪close()
91  {
92  if (!is_resource($this->resource)) {
93  return;
94  }
95  ‪$resource = $this->‪detach();
96  if (‪$resource === null) {
97  return;
98  }
99  fclose(‪$resource);
100  }
101 
109  public function ‪detach()
110  {
112  $this->resource = null;
113  return ‪$resource;
114  }
115 
121  public function ‪getSize()
122  {
123  if ($this->resource === null) {
124  return null;
125  }
126  $stats = fstat($this->resource);
127  return $stats['size'];
128  }
129 
136  public function ‪tell()
137  {
138  if (!is_resource($this->resource)) {
139  throw new \RuntimeException('No resource available; cannot tell position', 1436717285);
140  }
141  $result = ftell($this->resource);
142  if (!is_int($result)) {
143  throw new \RuntimeException('Error occurred during tell operation', 1436717286);
144  }
145  return $result;
146  }
147 
153  public function ‪eof()
154  {
155  if (!is_resource($this->resource)) {
156  return true;
157  }
158  return feof($this->resource);
159  }
160 
166  public function ‪isSeekable()
167  {
168  if (!is_resource($this->resource)) {
169  return false;
170  }
171  return (bool)$this->‪getMetadata('seekable');
172  }
173 
188  public function ‪seek($offset, $whence = SEEK_SET)
189  {
190  if (!is_resource($this->resource)) {
191  throw new \RuntimeException('No resource available; cannot seek position', 1436717287);
192  }
193 
194  if (!$this->‪isSeekable()) {
195  throw new \RuntimeException('Stream is not seekable', 1436717288);
196  }
197  $result = fseek($this->resource, $offset, $whence);
198  if ($result !== 0) {
199  throw new \RuntimeException('Error seeking within stream', 1436717289);
200  }
201  }
202 
213  public function ‪rewind()
214  {
215  $this->‪seek(0);
216  }
217 
223  public function ‪isWritable()
224  {
225  if (!is_resource($this->resource)) {
226  return false;
227  }
228  $uri = $this->‪getMetadata('uri');
229  return is_writable($uri);
230  }
231 
239  public function ‪write($string)
240  {
241  if (!is_resource($this->resource)) {
242  throw new \RuntimeException('No resource available; cannot write', 1436717290);
243  }
244  $result = fwrite($this->resource, $string);
245  if ($result === false) {
246  throw new \RuntimeException('Error writing to stream', 1436717291);
247  }
248  return $result;
249  }
250 
256  public function ‪isReadable()
257  {
258  if (!is_resource($this->resource)) {
259  return false;
260  }
261  $mode = $this->‪getMetadata('mode');
262  return str_contains($mode, 'r') || str_contains($mode, '+');
263  }
264 
275  public function ‪read($length)
276  {
277  if (!is_resource($this->resource)) {
278  throw new \RuntimeException('No resource available; cannot read', 1436717292);
279  }
280  if (!$this->‪isReadable()) {
281  throw new \RuntimeException('Stream is not readable', 1436717293);
282  }
283  $result = fread($this->resource, $length);
284  if ($result === false) {
285  throw new \RuntimeException('Error reading stream', 1436717294);
286  }
287  return $result;
288  }
289 
297  public function ‪getContents()
298  {
299  if (!is_resource($this->resource) || !$this->‪isReadable()) {
300  return '';
301  }
302  $result = stream_get_contents($this->resource);
303  if ($result === false) {
304  throw new \RuntimeException('Error reading from stream', 1436717295);
305  }
306  return $result;
307  }
308 
323  public function ‪getMetadata($key = null)
324  {
325  if (!is_resource($this->resource)) {
326  return null;
327  }
328  $metadata = stream_get_meta_data($this->resource);
329  if ($key === null) {
330  return $metadata;
331  }
332  if (!isset($metadata[$key])) {
333  return null;
334  }
335  return $metadata[$key];
336  }
337 
346  public function ‪attach(‪$resource, $mode = 'r')
347  {
348  $error = null;
349  if (!is_resource(‪$resource) && is_string(‪$resource)) {
350  set_error_handler(static function ($e) use (&$error): bool {
351  $error = $e;
352  return true;
353  }, E_WARNING);
354  ‪$resource = fopen(‪$resource, $mode);
355  restore_error_handler();
356  }
357  if ($error) {
358  throw new \InvalidArgumentException('Invalid stream reference provided', 1436717296);
359  }
360  if (!is_resource(‪$resource)) {
361  throw new \InvalidArgumentException('Invalid stream provided; must be a string stream identifier or resource', 1436717297);
362  }
363  $this->resource = ‪$resource;
364  }
365 }
‪TYPO3\CMS\Core\Http\Stream\rewind
‪rewind()
Definition: Stream.php:211
‪TYPO3\CMS\Core\Http\Stream\seek
‪seek($offset, $whence=SEEK_SET)
Definition: Stream.php:186
‪TYPO3\CMS\Core\Http\Stream\tell
‪int tell()
Definition: Stream.php:134
‪TYPO3\CMS\Core\Http\Stream\read
‪string read($length)
Definition: Stream.php:273
‪TYPO3\CMS\Core\Http\Stream\eof
‪bool eof()
Definition: Stream.php:151
‪TYPO3\CMS\Core\Http\Stream\getMetadata
‪array mixed null getMetadata($key=null)
Definition: Stream.php:321
‪TYPO3\CMS\Core\Http\Stream\isSeekable
‪bool isSeekable()
Definition: Stream.php:164
‪TYPO3\CMS\Core\Http\Stream\write
‪int write($string)
Definition: Stream.php:237
‪TYPO3\CMS\Core\Http\Stream\isReadable
‪bool isReadable()
Definition: Stream.php:254
‪TYPO3\CMS\Core\Http\Stream\__construct
‪__construct($stream, $mode='r')
Definition: Stream.php:46
‪TYPO3\CMS\Core\Http\Stream
Definition: Stream.php:29
‪TYPO3\CMS\Core\Http\Stream\$stream
‪string resource $stream
Definition: Stream.php:37
‪TYPO3\CMS\Core\Http\Stream\__toString
‪string __toString()
Definition: Stream.php:72
‪TYPO3\CMS\Core\Http\Stream\getContents
‪string getContents()
Definition: Stream.php:295
‪TYPO3\CMS\Core\Http\Stream\getSize
‪int null getSize()
Definition: Stream.php:119
‪TYPO3\CMS\Core\Http\Stream\close
‪close()
Definition: Stream.php:88
‪TYPO3\CMS\Core\Http\Stream\detach
‪resource null detach()
Definition: Stream.php:107
‪TYPO3\CMS\Core\Http\Stream\$resource
‪resource null $resource
Definition: Stream.php:33
‪TYPO3\CMS\Core\Http\Stream\isWritable
‪bool isWritable()
Definition: Stream.php:221
‪TYPO3\CMS\Core\Http\Stream\attach
‪attach($resource, $mode='r')
Definition: Stream.php:344
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18