ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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 15 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 78 of file Stream.php.

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

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

◆ __destruct()

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

Definition at line 347 of file Stream.php.

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

348  {
349 
350  //cleanup the resource on object destruction if the stream is not detached.
351  if (!is_null($this->stream)) {
352  $this->close();
353  }
354  }
+ Here is the call graph for this function:

Member Function Documentation

◆ __toString()

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

Definition at line 333 of file Stream.php.

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

334  {
335  try {
336  $this->rewind();
337  return strval($this->getContents());
338  } catch (\Exception $ex) {
339  //to string must not throw an error.
340  return '';
341  }
342  }
+ 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 361 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().

362  {
363  if ($this->stream === null) {
364  throw new \RuntimeException('Stream is detached');
365  }
366  }
+ Here is the caller graph for this function:

◆ close()

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

Definition at line 107 of file Stream.php.

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

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

108  {
109  if ($this->stream !== null && is_resource($this->stream)) {
110  PHPStreamFunctions::fclose($this->stream);
111  }
112 
113  $this->detach();
114  }
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 ( )

Definition at line 119 of file Stream.php.

References ILIAS\Filesystem\Stream\Stream\$stream.

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

120  {
122  $this->stream = $this->size = $this->uri = null;
123 
124  return $stream;
125  }
+ Here is the caller graph for this function:

◆ eof()

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

Definition at line 177 of file Stream.php.

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

178  {
179  $this->assertStreamAttached();
180 
181  return feof($this->stream);
182  }
assertStreamAttached()
Checks if the stream is attached to the wrapper.
Definition: Stream.php:361
+ Here is the call graph for this function:

◆ getContents()

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

Definition at line 284 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().

285  {
286  $this->assertStreamAttached();
287 
288  $content = PHPStreamFunctions::stream_get_contents($this->stream);
289 
290  if ($content === false) {
291  throw new \RuntimeException('Unable to read stream contents');
292  }
293 
294  return $content;
295  }
assertStreamAttached()
Checks if the stream is attached to the wrapper.
Definition: Stream.php:361
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)

Definition at line 300 of file Stream.php.

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

301  {
302 
303  //return empty array if stream is detached
304  if ($this->stream === null) {
305  return [];
306  }
307 
308  //return merged metadata if key is missing
309  if ($key === null) {
310  return array_merge(stream_get_meta_data($this->stream), $this->customMetadata);
311  }
312 
313  //return value if key was provided
314 
315  //try fetch data from custom metadata
316  if (array_key_exists($key, $this->customMetadata)) {
317  return $this->customMetadata[$key];
318  }
319 
320  //try to fetch data from php resource metadata
321  $meta = stream_get_meta_data($this->stream);
322  if (array_key_exists($key, $meta)) {
323  return $meta[$key];
324  }
325 
326  //the key was not found in standard and custom metadata.
327  return null;
328  }
+ Here is the caller graph for this function:

◆ getSize()

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

Definition at line 130 of file Stream.php.

References ILIAS\Filesystem\Stream\Stream\$size.

131  {
132 
133  //check if we know the size
134  if ($this->size !== null) {
135  return $this->size;
136  }
137 
138  //check if stream is detached
139  if ($this->stream === null) {
140  return null;
141  }
142 
143  //clear stat cache if we got a uri (indicates that we have a file resource)
144  if ($this->uri !== null) {
145  clearstatcache(true, $this->uri);
146  }
147 
148  $stats = fstat($this->stream);
149  if (array_key_exists('size', $stats)) {
150  $this->size = $stats['size'];
151  return $this->size;
152  }
153 
154  //unable to determine stream size
155  return null;
156  }

◆ isReadable()

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

Definition at line 249 of file Stream.php.

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

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

250  {
251  return $this->readable;
252  }
+ Here is the caller graph for this function:

◆ isSeekable()

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

Definition at line 187 of file Stream.php.

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

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

188  {
189  return $this->seekable;
190  }
+ Here is the caller graph for this function:

◆ isWritable()

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

Definition at line 219 of file Stream.php.

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

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

220  {
221  return $this->writeable;
222  }
+ Here is the caller graph for this function:

◆ read()

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

Definition at line 257 of file Stream.php.

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

258  {
259  $this->assertStreamAttached();
260 
261  if (!$this->isReadable()) {
262  throw new \RuntimeException('Can not read from non-readable stream');
263  }
264 
265  if ($length < 0) {
266  throw new \RuntimeException('Length parameter must not be negative');
267  }
268 
269  if ($length === 0) {
270  return '';
271  }
272 
273  $junk = PHPStreamFunctions::fread($this->stream, $length);
274  if ($junk === false) {
275  throw new \RuntimeException('Unable to read from stream');
276  }
277 
278  return $junk;
279  }
assertStreamAttached()
Checks if the stream is attached to the wrapper.
Definition: Stream.php:361
static fread($handle, $length)
fread wrapper
+ Here is the call graph for this function:

◆ rewind()

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

Definition at line 211 of file Stream.php.

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

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

212  {
213  $this->seek(0);
214  }
seek($offset, $whence=SEEK_SET)
Definition: Stream.php:195
+ 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 
)

Definition at line 195 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().

196  {
197  $this->assertStreamAttached();
198 
199  if (!$this->isSeekable()) {
200  throw new \RuntimeException('Stream is not seekable');
201  }
202 
203  if (PHPStreamFunctions::fseek($this->stream, $offset, $whence) === -1) {
204  throw new \RuntimeException("Unable to seek to stream position \"$offset\" with whence \"$whence\"");
205  }
206  }
assertStreamAttached()
Checks if the stream is attached to the wrapper.
Definition: Stream.php:361
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 ( )

Definition at line 161 of file Stream.php.

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

162  {
163  $this->assertStreamAttached();
164 
165  $result = PHPStreamFunctions::ftell($this->stream);
166 
167  if ($result === false) {
168  throw new \RuntimeException('Unable to determine stream position');
169  }
170 
171  return $result;
172  }
$result
assertStreamAttached()
Checks if the stream is attached to the wrapper.
Definition: Stream.php:361
+ Here is the call graph for this function:

◆ write()

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

Definition at line 227 of file Stream.php.

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

228  {
229  $this->assertStreamAttached();
230 
231  if (!$this->isWritable()) {
232  throw new \RuntimeException('Can not write to a non-writable stream');
233  }
234 
235  //we can't know the new size
236  $this->size = null;
237  $result = PHPStreamFunctions::fwrite($this->stream, $string);
238 
239  if ($result === false) {
240  throw new \RuntimeException('Unable to write to stream');
241  }
242 
243  return $result;
244  }
$result
assertStreamAttached()
Checks if the stream is attached to the wrapper.
Definition: Stream.php:361
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 21 of file Stream.php.

◆ $customMetadata

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

Definition at line 71 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 47 of file Stream.php.

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

◆ $seekable

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

Definition at line 55 of file Stream.php.

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

◆ $size

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

Definition at line 63 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 67 of file Stream.php.

◆ $writeable

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

Definition at line 51 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 17 of file Stream.php.

◆ MASK_ACCESS_READ_WRITE

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

Definition at line 19 of file Stream.php.

◆ MASK_ACCESS_WRITE

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

Definition at line 18 of file Stream.php.


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