‪TYPO3CMS  10.4
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);
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 (!$this->resource) {
93  return;
94  }
95  ‪$resource = $this->‪detach();
96  fclose(‪$resource);
97  }
98 
106  public function ‪detach()
107  {
109  $this->resource = null;
110  return ‪$resource;
111  }
112 
118  public function ‪getSize()
119  {
120  if ($this->resource === null) {
121  return null;
122  }
123  $stats = fstat($this->resource);
124  return $stats['size'];
125  }
126 
133  public function ‪tell()
134  {
135  if (!$this->resource) {
136  throw new \RuntimeException('No resource available; cannot tell position', 1436717285);
137  }
138  $result = ftell($this->resource);
139  if (!is_int($result)) {
140  throw new \RuntimeException('Error occurred during tell operation', 1436717286);
141  }
142  return $result;
143  }
144 
150  public function ‪eof()
151  {
152  if (!$this->resource) {
153  return true;
154  }
155  return feof($this->resource);
156  }
157 
163  public function ‪isSeekable()
164  {
165  if (!$this->resource) {
166  return false;
167  }
168  return (bool)$this->‪getMetadata('seekable');
169  }
170 
185  public function ‪seek($offset, $whence = SEEK_SET)
186  {
187  if (!$this->resource) {
188  throw new \RuntimeException('No resource available; cannot seek position', 1436717287);
189  }
190 
191  if (!$this->‪isSeekable()) {
192  throw new \RuntimeException('Stream is not seekable', 1436717288);
193  }
194  $result = fseek($this->resource, $offset, $whence);
195  if ($result !== 0) {
196  throw new \RuntimeException('Error seeking within stream', 1436717289);
197  }
198  }
199 
210  public function ‪rewind()
211  {
212  $this->‪seek(0);
213  }
214 
220  public function ‪isWritable()
221  {
222  if (!$this->resource) {
223  return false;
224  }
225  $uri = $this->‪getMetadata('uri');
226  return is_writable($uri);
227  }
228 
236  public function ‪write($string)
237  {
238  if (!$this->resource) {
239  throw new \RuntimeException('No resource available; cannot write', 1436717290);
240  }
241  $result = fwrite($this->resource, $string);
242  if ($result === false) {
243  throw new \RuntimeException('Error writing to stream', 1436717291);
244  }
245  return $result;
246  }
247 
253  public function ‪isReadable()
254  {
255  if (!$this->resource) {
256  return false;
257  }
258  $mode = $this->‪getMetadata('mode');
259  return strpos($mode, 'r') !== false || strpos($mode, '+') !== false;
260  }
261 
272  public function ‪read($length)
273  {
274  if (!$this->resource) {
275  throw new \RuntimeException('No resource available; cannot read', 1436717292);
276  }
277  if (!$this->‪isReadable()) {
278  throw new \RuntimeException('Stream is not readable', 1436717293);
279  }
280  $result = fread($this->resource, $length);
281  if ($result === false) {
282  throw new \RuntimeException('Error reading stream', 1436717294);
283  }
284  return $result;
285  }
286 
294  public function ‪getContents()
295  {
296  if (!$this->‪isReadable()) {
297  return '';
298  }
299  $result = stream_get_contents($this->resource);
300  if ($result === false) {
301  throw new \RuntimeException('Error reading from stream', 1436717295);
302  }
303  return $result;
304  }
305 
320  public function ‪getMetadata($key = null)
321  {
322  $metadata = stream_get_meta_data($this->resource);
323  if ($key === null) {
324  return $metadata;
325  }
326  if (!isset($metadata[$key])) {
327  return null;
328  }
329  return $metadata[$key];
330  }
331 
340  public function ‪attach(‪$resource, $mode = 'r')
341  {
342  $error = null;
343  if (!is_resource(‪$resource) && is_string(‪$resource)) {
344  set_error_handler(function ($e) use (&$error) {
345  $error = $e;
346  }, E_WARNING);
347  ‪$resource = fopen(‪$resource, $mode);
348  restore_error_handler();
349  }
350  if ($error) {
351  throw new \InvalidArgumentException('Invalid stream reference provided', 1436717296);
352  }
353  if (!is_resource(‪$resource)) {
354  throw new \InvalidArgumentException('Invalid stream provided; must be a string stream identifier or resource', 1436717297);
355  }
356  $this->resource = ‪$resource;
357  }
358 }
‪TYPO3\CMS\Core\Http\Stream\rewind
‪rewind()
Definition: Stream.php:208
‪TYPO3\CMS\Core\Http\Stream\seek
‪seek($offset, $whence=SEEK_SET)
Definition: Stream.php:183
‪TYPO3\CMS\Core\Http\Stream\tell
‪int tell()
Definition: Stream.php:131
‪TYPO3\CMS\Core\Http\Stream\read
‪string read($length)
Definition: Stream.php:270
‪TYPO3\CMS\Core\Http\Stream\eof
‪bool eof()
Definition: Stream.php:148
‪TYPO3\CMS\Core\Http\Stream\getMetadata
‪array mixed null getMetadata($key=null)
Definition: Stream.php:318
‪TYPO3\CMS\Core\Http\Stream\isSeekable
‪bool isSeekable()
Definition: Stream.php:161
‪TYPO3\CMS\Core\Http\Stream\write
‪int write($string)
Definition: Stream.php:234
‪TYPO3\CMS\Core\Http\Stream\isReadable
‪bool isReadable()
Definition: Stream.php:251
‪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:292
‪TYPO3\CMS\Core\Http\Stream\getSize
‪int null getSize()
Definition: Stream.php:116
‪TYPO3\CMS\Core\Http\Stream\$resource
‪resource $resource
Definition: Stream.php:33
‪TYPO3\CMS\Core\Http\Stream\close
‪close()
Definition: Stream.php:88
‪TYPO3\CMS\Core\Http\Stream\detach
‪resource null detach()
Definition: Stream.php:104
‪TYPO3\CMS\Core\Http\Stream\isWritable
‪bool isWritable()
Definition: Stream.php:218
‪TYPO3\CMS\Core\Http\Stream\attach
‪attach($resource, $mode='r')
Definition: Stream.php:338
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18