ILIAS  trunk Revision v11.0_alpha-1761-g6dbbfa7b760
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ILIAS\Filesystem\Stream\Stream Class Reference
+ 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
 

Protected Member Functions

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

Protected Attributes

static array string $_mode = null
 
bool $readable
 
bool $writeable
 
bool $seekable
 
 $stream
 
int $size = null
 
string $uri = null
 
array $customMetadata
 

Static Protected Attributes

static array $accessMap
 

Detailed Description

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 | null$optionsThe additional options which are accessible via getMetadata

Definition at line 79 of file Stream.php.

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

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

◆ __destruct()

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

Definition at line 352 of file Stream.php.

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

353  {
354  //cleanup the resource on object destruction if the stream is not detached.
355  if (!is_null($this->stream)) {
356  $this->close();
357  }
358  }
+ Here is the call graph for this function:

Member Function Documentation

◆ __toString()

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

Definition at line 338 of file Stream.php.

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

338  : string
339  {
340  try {
341  $this->rewind();
342  return $this->getContents();
343  } catch (\Exception) {
344  //to string must not throw an error.
345  return '';
346  }
347  }
+ Here is the call graph for this function:

◆ assertStreamAttached()

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

Checks if the stream is attached to the wrapper.

An exception if thrown if the stream is already detached.

Exceptions

Definition at line 366 of file Stream.php.

References null.

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().

366  : void
367  {
368  if ($this->stream === null) {
369  throw new \RuntimeException('Stream is detached');
370  }
371  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
+ Here is the caller graph for this function:

◆ close()

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

Definition at line 114 of file Stream.php.

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

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

114  : void
115  {
116  if (is_resource($this->stream)) {
117  PHPStreamFunctions::fclose($this->stream);
118  }
119 
120  $this->detach();
121  }
+ 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 126 of file Stream.php.

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

Referenced by ILIAS\Filesystem\Stream\Stream\close(), ILIAS\Filesystem\Stream\Streams\ofPsr7Stream(), ILIAS\ResourceStorage\Flavour\Machine\DefaultMachines\Extract\General\readImage(), and ILIAS\ResourceStorage\Flavour\Machine\DefaultMachines\Extract\PDF\readImage().

127  {
129  $this->stream = $this->size = $this->uri = null;
130 
131  return $stream;
132  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
+ Here is the caller graph for this function:

◆ eof()

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

Definition at line 183 of file Stream.php.

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

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

◆ getContents()

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

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

290  : string
291  {
292  $this->assertStreamAttached();
293 
294  $content = PHPStreamFunctions::stream_get_contents($this->stream);
295 
296  if ($content === false) {
297  throw new \RuntimeException('Unable to read stream contents');
298  }
299 
300  return $content;
301  }
assertStreamAttached()
Checks if the stream is attached to the wrapper.
Definition: Stream.php:366
static stream_get_contents($handle, $length=-1)
+ 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 306 of file Stream.php.

References null.

Referenced by ILIAS\Filesystem\Stream\Stream\__construct(), ILIAS\Export\ExportHandler\Repository\Element\Wrapper\IRSS\Handler\getStreamOfContainerEntry(), ILIAS\ResourceStorage\Flavour\Machine\DefaultMachines\Extract\Video\readImage(), and ILIAS\ResourceStorage\Flavour\Machine\DefaultMachines\Extract\PDF\readImage().

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

◆ getSize()

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

Definition at line 137 of file Stream.php.

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

137  : ?int
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  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null

◆ isReadable()

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

Definition at line 255 of file Stream.php.

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

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

255  : bool
256  {
257  return $this->readable;
258  }
+ Here is the caller graph for this function:

◆ isSeekable()

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

Definition at line 193 of file Stream.php.

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

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

193  : bool
194  {
195  return $this->seekable;
196  }
+ Here is the caller graph for this function:

◆ isWritable()

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

Definition at line 225 of file Stream.php.

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

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

225  : bool
226  {
227  return $this->writeable;
228  }
+ Here is the caller graph for this function:

◆ read()

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

Definition at line 263 of file Stream.php.

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

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

◆ rewind()

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

Definition at line 217 of file Stream.php.

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

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

217  : void
218  {
219  $this->seek(0);
220  }
seek($offset, $whence=SEEK_SET)
Definition: Stream.php:201
+ 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 201 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().

201  : void
202  {
203  $this->assertStreamAttached();
204 
205  if (!$this->isSeekable()) {
206  throw new \RuntimeException('Stream is not seekable');
207  }
208 
209  if (PHPStreamFunctions::fseek($this->stream, $offset, $whence) === -1) {
210  throw new \RuntimeException("Unable to seek to stream position \"$offset\" with whence \"$whence\"");
211  }
212  }
static fseek($stream, int $offset, int $whence)
assertStreamAttached()
Checks if the stream is attached to the wrapper.
Definition: Stream.php:366
+ 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 167 of file Stream.php.

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

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

◆ write()

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

Definition at line 233 of file Stream.php.

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

233  : int
234  {
235  $this->assertStreamAttached();
236 
237  if (!$this->isWritable()) {
238  throw new \RuntimeException('Can not write to a non-writable stream');
239  }
240 
241  //we can't know the new size
242  $this->size = null;
243  $result = PHPStreamFunctions::fwrite($this->stream, $string);
244 
245  if ($result === false) {
246  throw new \RuntimeException('Unable to write to stream');
247  }
248 
249  return $result;
250  }
assertStreamAttached()
Checks if the stream is attached to the wrapper.
Definition: Stream.php:366
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static fwrite($handle, string $string, ?int $length=null)
+ Here is the call graph for this function:

Field Documentation

◆ $_mode

array string ILIAS\Filesystem\Stream\Stream::$_mode = null
protected

Definition at line 57 of file Stream.php.

◆ $accessMap

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

Definition at line 35 of file Stream.php.

◆ $customMetadata

array ILIAS\Filesystem\Stream\Stream::$customMetadata
protected

Definition at line 71 of file Stream.php.

◆ $readable

bool ILIAS\Filesystem\Stream\Stream::$readable
protected

Definition at line 59 of file Stream.php.

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

◆ $seekable

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

Definition at line 61 of file Stream.php.

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

◆ $size

int ILIAS\Filesystem\Stream\Stream::$size = null
protected

Definition at line 66 of file Stream.php.

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

◆ $stream

null ILIAS\Filesystem\Stream\Stream::$stream
protected

◆ $uri

string ILIAS\Filesystem\Stream\Stream::$uri = null
protected

Definition at line 67 of file Stream.php.

◆ $writeable

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

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

◆ MASK_ACCESS_READ_WRITE

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

Definition at line 33 of file Stream.php.

◆ MASK_ACCESS_WRITE

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

Definition at line 32 of file Stream.php.


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