ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ILIAS\Filesystem\Stream\Stream Class Reference

Class Stream. More...

+ Inheritance diagram for ILIAS\Filesystem\Stream\Stream:
+ Collaboration diagram for ILIAS\Filesystem\Stream\Stream:

Public Member Functions

 __construct ($stream, StreamOptions $options=null)
 Stream constructor. More...
 
 close ()
 
 detach ()
 
 getSize ()
 
 tell ()
 
 eof ()
 
 isSeekable ()
 
 seek ($offset, $whence=SEEK_SET)
 
 rewind ()
 
 isWritable ()
 
 write ($string)
 
 isReadable ()
 
 read ($length)
 
 getContents ()
 
 getMetadata ($key=null)
 
 __toString ()
 
 __destruct ()
 

Data Fields

const MASK_ACCESS_READ = 01
 
const MASK_ACCESS_WRITE = 02
 
const MASK_ACCESS_READ_WRITE = 03
 

Private Member Functions

 assertStreamAttached ()
 Checks if the stream is attached to the wrapper. More...
 

Private Attributes

 $readable
 
 $writeable
 
 $seekable
 
 $stream
 
 $size
 
 $uri
 
 $customMetadata
 

Static Private Attributes

static $accessMap
 

Detailed Description

Class Stream.

Author
Nicolas Schäfli ns@st.nosp@m.uder.nosp@m.-raim.nosp@m.ann..nosp@m.ch
Since
5.3
Version
1.0.0

Definition at line 18 of file Stream.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Filesystem\Stream\Stream::__construct (   $stream,
StreamOptions  $options = null 
)

Stream constructor.

Parameters
resource$streamThe resource which should be wrapped by the Stream.
StreamOptions$optionsThe additional options which are accessible via getMetadata

Definition at line 83 of file Stream.php.

References PHPMailer\PHPMailer\$options, ILIAS\Filesystem\Stream\Stream\$stream, ILIAS\Filesystem\Stream\Stream\getMetadata(), and size.

84  {
85  if (!is_resource($stream)) {
86  throw new \InvalidArgumentException('Stream must be a valid resource but "' . gettype($stream) . '" was given.');
87  }
88 
89  if ($options !== null) {
90  $this->customMetadata = $options->getMetadata();
91  $this->size = ($options->getSize() !== -1) ? $options->getSize() : null;
92  } else {
93  $this->customMetadata = [];
94  }
95 
96  $this->stream = $stream;
97 
98  $meta = stream_get_meta_data($this->stream);
99  $mode = $meta['mode'];
100 
101  $this->readable = array_key_exists($mode, self::$accessMap) && boolval(self::$accessMap[$mode] & self::MASK_ACCESS_READ);
102  $this->writeable = array_key_exists($mode, self::$accessMap) && boolval(self::$accessMap[$mode] & self::MASK_ACCESS_WRITE);
103  $this->seekable = boolval($meta['seekable']);
104  $this->uri = $this->getMetadata('uri');
105  }
font size
Definition: langcheck.php:162
+ Here is the call graph for this function:

◆ __destruct()

ILIAS\Filesystem\Stream\Stream::__destruct ( )

Definition at line 365 of file Stream.php.

References ILIAS\Filesystem\Stream\Stream\close().

366  {
367 
368  //cleanup the resource on object destruction if the stream is not detached.
369  if (!is_null($this->stream)) {
370  $this->close();
371  }
372  }
+ Here is the call graph for this function:

Member Function Documentation

◆ __toString()

ILIAS\Filesystem\Stream\Stream::__toString ( )

Implements Psr\Http\Message\StreamInterface.

Definition at line 350 of file Stream.php.

References ILIAS\Filesystem\Stream\Stream\getContents(), and ILIAS\Filesystem\Stream\Stream\rewind().

351  {
352  try {
353  $this->rewind();
354  return strval($this->getContents());
355  } catch (\Exception $ex) {
356  //to string must not throw an error.
357  return '';
358  }
359  }
+ Here is the call graph for this function:

◆ assertStreamAttached()

ILIAS\Filesystem\Stream\Stream::assertStreamAttached ( )
private

Checks if the stream is attached to the wrapper.

An exception if thrown if the stream is already detached.

Exceptions

Definition at line 381 of file Stream.php.

Referenced by ILIAS\Filesystem\Stream\Stream\eof(), ILIAS\Filesystem\Stream\Stream\getContents(), ILIAS\Filesystem\Stream\Stream\read(), ILIAS\Filesystem\Stream\Stream\seek(), ILIAS\Filesystem\Stream\Stream\tell(), and ILIAS\Filesystem\Stream\Stream\write().

382  {
383  if ($this->stream === null) {
384  throw new \RuntimeException('Stream is detached');
385  }
386  }
+ Here is the caller graph for this function:

◆ close()

ILIAS\Filesystem\Stream\Stream::close ( )

Implements Psr\Http\Message\StreamInterface.

Definition at line 111 of file Stream.php.

References ILIAS\Filesystem\Stream\Stream\detach(), and ILIAS\Filesystem\Util\PHPStreamFunctions\fclose().

Referenced by ILIAS\Filesystem\Stream\Stream\__destruct().

112  {
113  if ($this->stream !== null && is_resource($this->stream)) {
114  PHPStreamFunctions::fclose($this->stream);
115  }
116 
117  $this->detach();
118  }
static fclose($handle)
fclose wrapper
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ detach()

ILIAS\Filesystem\Stream\Stream::detach ( )

Implements Psr\Http\Message\StreamInterface.

Definition at line 124 of file Stream.php.

References ILIAS\Filesystem\Stream\Stream\$stream, and size.

Referenced by ILIAS\Filesystem\Stream\Stream\close().

125  {
127  $this->stream = $this->size = $this->uri = null;
128 
129  return $stream;
130  }
font size
Definition: langcheck.php:162
+ Here is the caller graph for this function:

◆ eof()

ILIAS\Filesystem\Stream\Stream::eof ( )

Implements Psr\Http\Message\StreamInterface.

Definition at line 185 of file Stream.php.

References ILIAS\Filesystem\Stream\Stream\assertStreamAttached().

186  {
187  $this->assertStreamAttached();
188 
189  return feof($this->stream);
190  }
assertStreamAttached()
Checks if the stream is attached to the wrapper.
Definition: Stream.php:381
+ Here is the call graph for this function:

◆ getContents()

ILIAS\Filesystem\Stream\Stream::getContents ( )

Implements Psr\Http\Message\StreamInterface.

Definition at line 300 of file Stream.php.

References ILIAS\Filesystem\Stream\Stream\assertStreamAttached(), and ILIAS\Filesystem\Util\PHPStreamFunctions\stream_get_contents().

Referenced by ILIAS\Filesystem\Stream\Stream\__toString().

301  {
302  $this->assertStreamAttached();
303 
304  $content = PHPStreamFunctions::stream_get_contents($this->stream);
305 
306  if ($content === false) {
307  throw new \RuntimeException('Unable to read stream contents');
308  }
309 
310  return $content;
311  }
assertStreamAttached()
Checks if the stream is attached to the wrapper.
Definition: Stream.php:381
static stream_get_contents($handle, $length=-1)
stream_get_contents wrapper
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getMetadata()

ILIAS\Filesystem\Stream\Stream::getMetadata (   $key = null)

Implements Psr\Http\Message\StreamInterface.

Definition at line 317 of file Stream.php.

References $key.

Referenced by ILIAS\Filesystem\Stream\Stream\__construct().

318  {
319 
320  //return empty array if stream is detached
321  if ($this->stream === null) {
322  return [];
323  }
324 
325  //return merged metadata if key is missing
326  if ($key === null) {
327  return array_merge(stream_get_meta_data($this->stream), $this->customMetadata);
328  }
329 
330  //return value if key was provided
331 
332  //try fetch data from custom metadata
333  if (array_key_exists($key, $this->customMetadata)) {
334  return $this->customMetadata[$key];
335  }
336 
337  //try to fetch data from php resource metadata
338  $meta = stream_get_meta_data($this->stream);
339  if (array_key_exists($key, $meta)) {
340  return $meta[$key];
341  }
342 
343  //the key was not found in standard and custom metadata.
344  return null;
345  }
$key
Definition: croninfo.php:18
+ Here is the caller graph for this function:

◆ getSize()

ILIAS\Filesystem\Stream\Stream::getSize ( )

Implements Psr\Http\Message\StreamInterface.

Definition at line 136 of file Stream.php.

References ILIAS\Filesystem\Stream\Stream\$size, Sabre\VObject\$stats, and size.

137  {
138 
139  //check if we know the size
140  if ($this->size !== null) {
141  return $this->size;
142  }
143 
144  //check if stream is detached
145  if ($this->stream === null) {
146  return null;
147  }
148 
149  //clear stat cache if we got a uri (indicates that we have a file resource)
150  if ($this->uri !== null) {
151  clearstatcache(true, $this->uri);
152  }
153 
154  $stats = fstat($this->stream);
155  if (array_key_exists('size', $stats)) {
156  $this->size = $stats['size'];
157  return $this->size;
158  }
159 
160  //unable to determine stream size
161  return null;
162  }
font size
Definition: langcheck.php:162

◆ isReadable()

ILIAS\Filesystem\Stream\Stream::isReadable ( )

Implements Psr\Http\Message\StreamInterface.

Definition at line 263 of file Stream.php.

References ILIAS\Filesystem\Stream\Stream\$readable.

Referenced by ILIAS\Filesystem\Stream\Stream\read().

264  {
265  return $this->readable;
266  }
+ Here is the caller graph for this function:

◆ isSeekable()

ILIAS\Filesystem\Stream\Stream::isSeekable ( )

Implements Psr\Http\Message\StreamInterface.

Definition at line 196 of file Stream.php.

References ILIAS\Filesystem\Stream\Stream\$seekable.

Referenced by ILIAS\Filesystem\Stream\Stream\seek().

197  {
198  return $this->seekable;
199  }
+ Here is the caller graph for this function:

◆ isWritable()

ILIAS\Filesystem\Stream\Stream::isWritable ( )

Implements Psr\Http\Message\StreamInterface.

Definition at line 231 of file Stream.php.

References ILIAS\Filesystem\Stream\Stream\$writeable.

Referenced by ILIAS\Filesystem\Stream\Stream\write().

232  {
233  return $this->writeable;
234  }
+ Here is the caller graph for this function:

◆ read()

ILIAS\Filesystem\Stream\Stream::read (   $length)

Implements Psr\Http\Message\StreamInterface.

Definition at line 272 of file Stream.php.

References ILIAS\Filesystem\Stream\Stream\assertStreamAttached(), ILIAS\Filesystem\Util\PHPStreamFunctions\fread(), and ILIAS\Filesystem\Stream\Stream\isReadable().

273  {
274  $this->assertStreamAttached();
275 
276  if (!$this->isReadable()) {
277  throw new \RuntimeException('Can not read from non-readable stream');
278  }
279 
280  if ($length < 0) {
281  throw new \RuntimeException('Length parameter must not be negative');
282  }
283 
284  if ($length === 0) {
285  return '';
286  }
287 
288  $junk = PHPStreamFunctions::fread($this->stream, $length);
289  if ($junk === false) {
290  throw new \RuntimeException('Unable to read from stream');
291  }
292 
293  return $junk;
294  }
assertStreamAttached()
Checks if the stream is attached to the wrapper.
Definition: Stream.php:381
static fread($handle, $length)
fread wrapper
+ Here is the call graph for this function:

◆ rewind()

ILIAS\Filesystem\Stream\Stream::rewind ( )

Implements Psr\Http\Message\StreamInterface.

Definition at line 222 of file Stream.php.

References ILIAS\Filesystem\Stream\Stream\seek().

Referenced by ILIAS\Filesystem\Stream\Stream\__toString().

223  {
224  $this->seek(0);
225  }
seek($offset, $whence=SEEK_SET)
Definition: Stream.php:205
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ seek()

ILIAS\Filesystem\Stream\Stream::seek (   $offset,
  $whence = SEEK_SET 
)

Implements Psr\Http\Message\StreamInterface.

Definition at line 205 of file Stream.php.

References ILIAS\Filesystem\Stream\Stream\assertStreamAttached(), ILIAS\Filesystem\Util\PHPStreamFunctions\fseek(), and ILIAS\Filesystem\Stream\Stream\isSeekable().

Referenced by ILIAS\Filesystem\Stream\Stream\rewind().

206  {
207  $this->assertStreamAttached();
208 
209  if (!$this->isSeekable()) {
210  throw new \RuntimeException('Stream is not seekable');
211  }
212 
213  if (PHPStreamFunctions::fseek($this->stream, $offset, $whence) === -1) {
214  throw new \RuntimeException("Unable to seek to stream position \"$offset\" with whence \"$whence\"");
215  }
216  }
assertStreamAttached()
Checks if the stream is attached to the wrapper.
Definition: Stream.php:381
static fseek($stream, $offset, $whence)
fseek wrapper.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ tell()

ILIAS\Filesystem\Stream\Stream::tell ( )

Implements Psr\Http\Message\StreamInterface.

Definition at line 168 of file Stream.php.

References $result, ILIAS\Filesystem\Stream\Stream\assertStreamAttached(), and ILIAS\Filesystem\Util\PHPStreamFunctions\ftell().

169  {
170  $this->assertStreamAttached();
171 
172  $result = PHPStreamFunctions::ftell($this->stream);
173 
174  if ($result === false) {
175  throw new \RuntimeException('Unable to determine stream position');
176  }
177 
178  return $result;
179  }
$result
assertStreamAttached()
Checks if the stream is attached to the wrapper.
Definition: Stream.php:381
+ Here is the call graph for this function:

◆ write()

ILIAS\Filesystem\Stream\Stream::write (   $string)

Implements Psr\Http\Message\StreamInterface.

Definition at line 240 of file Stream.php.

References $result, ILIAS\Filesystem\Stream\Stream\assertStreamAttached(), ILIAS\Filesystem\Util\PHPStreamFunctions\fwrite(), ILIAS\Filesystem\Stream\Stream\isWritable(), and size.

241  {
242  $this->assertStreamAttached();
243 
244  if (!$this->isWritable()) {
245  throw new \RuntimeException('Can not write to a non-writable stream');
246  }
247 
248  //we can't know the new size
249  $this->size = null;
250  $result = PHPStreamFunctions::fwrite($this->stream, $string);
251 
252  if ($result === false) {
253  throw new \RuntimeException('Unable to write to stream');
254  }
255 
256  return $result;
257  }
$result
assertStreamAttached()
Checks if the stream is attached to the wrapper.
Definition: Stream.php:381
font size
Definition: langcheck.php:162
static fwrite($handle, $string, $length=null)
fwrite wrapper
+ Here is the call graph for this function:

Field Documentation

◆ $accessMap

ILIAS\Filesystem\Stream\Stream::$accessMap
staticprivate
Initial value:
= [
'r' => self::MASK_ACCESS_READ

Definition at line 24 of file Stream.php.

◆ $customMetadata

ILIAS\Filesystem\Stream\Stream::$customMetadata
private

Definition at line 74 of file Stream.php.

◆ $readable

bool ILIAS\Filesystem\Stream\Stream::$readable
private
Initial value:
=> self::MASK_ACCESS_READ_WRITE,
'r+' => self::MASK_ACCESS_READ_WRITE,
'x+' => self::MASK_ACCESS_READ_WRITE,
'c+' => self::MASK_ACCESS_READ_WRITE,
'rb' => self::MASK_ACCESS_READ,
'w+b' => self::MASK_ACCESS_READ_WRITE,
'r+b' => self::MASK_ACCESS_READ_WRITE,
'x+b' => self::MASK_ACCESS_READ_WRITE,
'c+b' => self::MASK_ACCESS_READ_WRITE,
'rt' => self::MASK_ACCESS_READ,
'w+t' => self::MASK_ACCESS_READ_WRITE,
'r+t' => self::MASK_ACCESS_READ_WRITE,
'x+t' => self::MASK_ACCESS_READ_WRITE,
'c+t' => self::MASK_ACCESS_READ_WRITE,
'a+' => self::MASK_ACCESS_READ_WRITE,
'w' => self::MASK_ACCESS_WRITE,
'rw' => self::MASK_ACCESS_WRITE,
'wb' => self::MASK_ACCESS_WRITE,
'a' => self::MASK_ACCESS_WRITE
]

Definition at line 50 of file Stream.php.

Referenced by ILIAS\Filesystem\Stream\Stream\isReadable().

◆ $seekable

bool ILIAS\Filesystem\Stream\Stream::$seekable
private

Definition at line 58 of file Stream.php.

Referenced by ILIAS\Filesystem\Stream\Stream\isSeekable().

◆ $size

int ILIAS\Filesystem\Stream\Stream::$size
private

Definition at line 66 of file Stream.php.

Referenced by ILIAS\Filesystem\Stream\Stream\getSize().

◆ $stream

resource ILIAS\Filesystem\Stream\Stream::$stream
private

◆ $uri

string ILIAS\Filesystem\Stream\Stream::$uri
private

Definition at line 70 of file Stream.php.

◆ $writeable

bool ILIAS\Filesystem\Stream\Stream::$writeable
private

Definition at line 54 of file Stream.php.

Referenced by ILIAS\Filesystem\Stream\Stream\isWritable().

◆ MASK_ACCESS_READ

const ILIAS\Filesystem\Stream\Stream::MASK_ACCESS_READ = 01

Definition at line 20 of file Stream.php.

◆ MASK_ACCESS_READ_WRITE

const ILIAS\Filesystem\Stream\Stream::MASK_ACCESS_READ_WRITE = 03

Definition at line 22 of file Stream.php.

◆ MASK_ACCESS_WRITE

const ILIAS\Filesystem\Stream\Stream::MASK_ACCESS_WRITE = 02

Definition at line 21 of file Stream.php.


The documentation for this class was generated from the following file: