ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 ()
 @inheritDoc More...
 
 detach ()
 @inheritDoc More...
 
 getSize ()
 @inheritDoc More...
 
 tell ()
 @inheritDoc More...
 
 eof ()
 @inheritDoc More...
 
 isSeekable ()
 @inheritDoc More...
 
 seek ($offset, $whence=SEEK_SET)
 @inheritDoc More...
 
 rewind ()
 @inheritDoc More...
 
 isWritable ()
 @inheritDoc More...
 
 write ($string)
 @inheritDoc More...
 
 isReadable ()
 @inheritDoc More...
 
 read ($length)
 @inheritDoc More...
 
 getContents ()
 @inheritDoc More...
 
 getMetadata ($key=null)
 @inheritDoc More...
 
 __toString ()
 @inheritDoc More...
 
 __destruct ()
 @inheritDoc More...
 
 __toString ()
 Reads all data from the stream into a string, from the beginning to end. More...
 
 close ()
 Closes the stream and any underlying resources. More...
 
 detach ()
 Separates any underlying resources from the stream. More...
 
 getSize ()
 Get the size of the stream if known. More...
 
 tell ()
 Returns the current position of the file read/write pointer. More...
 
 eof ()
 Returns true if the stream is at the end of the stream. More...
 
 isSeekable ()
 Returns whether or not the stream is seekable. More...
 
 seek ($offset, $whence=SEEK_SET)
 Seek to a position in the stream. More...
 
 rewind ()
 Seek to the beginning of the stream. More...
 
 isWritable ()
 Returns whether or not the stream is writable. More...
 
 write ($string)
 Write data to the stream. More...
 
 isReadable ()
 Returns whether or not the stream is readable. More...
 
 read ($length)
 Read data from the stream. More...
 
 getContents ()
 Returns the remaining contents in a string. More...
 
 getMetadata ($key=null)
 Get stream metadata as an associative array or retrieve a specific key. More...
 

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 17 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 82 of file Stream.php.

83 {
84 if (!is_resource($stream)) {
85 throw new \InvalidArgumentException('Stream must be a valid resource but "' . gettype($stream) . '" was given.');
86 }
87
88 if ($options !== null) {
89 $this->customMetadata = $options->getMetadata();
90 $this->size = ($options->getSize() !== -1) ? $options->getSize() : null;
91 } else {
92 $this->customMetadata = [];
93 }
94
95 $this->stream = $stream;
96
97 $meta = stream_get_meta_data($this->stream);
98 $mode = $meta['mode'];
99
100 $this->readable = array_key_exists($mode, self::$accessMap) && boolval(self::$accessMap[$mode] & self::MASK_ACCESS_READ);
101 $this->writeable = array_key_exists($mode, self::$accessMap) && boolval(self::$accessMap[$mode] & self::MASK_ACCESS_WRITE);
102 $this->seekable = boolval($meta['seekable']);
103 $this->uri = $this->getMetadata('uri');
104 }
if(!isset( $_REQUEST[ 'ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20
getMetadata($key=null)
@inheritDoc
Definition: Stream.php:316

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

+ Here is the call graph for this function:

◆ __destruct()

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

@inheritDoc

Definition at line 364 of file Stream.php.

365 {
366
367 //cleanup the resource on object destruction if the stream is not detached.
368 if (!is_null($this->stream)) {
369 $this->close();
370 }
371 }

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

+ Here is the call graph for this function:

Member Function Documentation

◆ __toString()

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

@inheritDoc

Implements Psr\Http\Message\StreamInterface.

Definition at line 349 of file Stream.php.

350 {
351 try {
352 $this->rewind();
353 return strval($this->getContents());
354 } catch (\Exception $ex) {
355 //to string must not throw an error.
356 return '';
357 }
358 }

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

+ 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

RuntimeException Thrown if the stream is already detached.

Definition at line 380 of file Stream.php.

381 {
382 if ($this->stream === null) {
383 throw new \RuntimeException('Stream is detached');
384 }
385 }

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

+ Here is the caller graph for this function:

◆ close()

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

@inheritDoc

Implements Psr\Http\Message\StreamInterface.

Definition at line 110 of file Stream.php.

111 {
112 if ($this->stream !== null && is_resource($this->stream)) {
113 PHPStreamFunctions::fclose($this->stream);
114 }
115
116 $this->detach();
117 }
static fclose($handle)
fclose wrapper

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

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ detach()

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

@inheritDoc

Implements Psr\Http\Message\StreamInterface.

Definition at line 123 of file Stream.php.

124 {
126 $this->stream = $this->size = $this->uri = null;
127
128 return $stream;
129 }

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

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

+ Here is the caller graph for this function:

◆ eof()

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

@inheritDoc

Implements Psr\Http\Message\StreamInterface.

Definition at line 184 of file Stream.php.

185 {
186 $this->assertStreamAttached();
187
188 return feof($this->stream);
189 }
assertStreamAttached()
Checks if the stream is attached to the wrapper.
Definition: Stream.php:380

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

+ Here is the call graph for this function:

◆ getContents()

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

@inheritDoc

Implements Psr\Http\Message\StreamInterface.

Definition at line 299 of file Stream.php.

300 {
301 $this->assertStreamAttached();
302
303 $content = PHPStreamFunctions::stream_get_contents($this->stream);
304
305 if ($content === false) {
306 throw new \RuntimeException('Unable to read stream contents');
307 }
308
309 return $content;
310 }
static stream_get_contents($handle, $length=-1)
stream_get_contents wrapper

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

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getMetadata()

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

@inheritDoc

Implements Psr\Http\Message\StreamInterface.

Definition at line 316 of file Stream.php.

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

References $key.

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

+ Here is the caller graph for this function:

◆ getSize()

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

@inheritDoc

Implements Psr\Http\Message\StreamInterface.

Definition at line 135 of file Stream.php.

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

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

◆ isReadable()

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

@inheritDoc

Implements Psr\Http\Message\StreamInterface.

Definition at line 262 of file Stream.php.

263 {
264 return $this->readable;
265 }

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

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

+ Here is the caller graph for this function:

◆ isSeekable()

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

@inheritDoc

Implements Psr\Http\Message\StreamInterface.

Definition at line 195 of file Stream.php.

196 {
197 return $this->seekable;
198 }

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

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

+ Here is the caller graph for this function:

◆ isWritable()

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

@inheritDoc

Implements Psr\Http\Message\StreamInterface.

Definition at line 230 of file Stream.php.

231 {
232 return $this->writeable;
233 }

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

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

+ Here is the caller graph for this function:

◆ read()

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

@inheritDoc

Implements Psr\Http\Message\StreamInterface.

Definition at line 271 of file Stream.php.

272 {
273 $this->assertStreamAttached();
274
275 if (!$this->isReadable()) {
276 throw new \RuntimeException('Can not read from non-readable stream');
277 }
278
279 if ($length < 0) {
280 throw new \RuntimeException('Length parameter must not be negative');
281 }
282
283 if ($length === 0) {
284 return '';
285 }
286
287 $junk = PHPStreamFunctions::fread($this->stream, $length);
288 if ($junk === false) {
289 throw new \RuntimeException('Unable to read from stream');
290 }
291
292 return $junk;
293 }
static fread($handle, $length)
fread wrapper

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

+ Here is the call graph for this function:

◆ rewind()

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

@inheritDoc

Implements Psr\Http\Message\StreamInterface.

Definition at line 221 of file Stream.php.

222 {
223 $this->seek(0);
224 }
seek($offset, $whence=SEEK_SET)
@inheritDoc
Definition: Stream.php:204

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

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

+ 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 
)

@inheritDoc

Implements Psr\Http\Message\StreamInterface.

Definition at line 204 of file Stream.php.

205 {
206 $this->assertStreamAttached();
207
208 if (!$this->isSeekable()) {
209 throw new \RuntimeException('Stream is not seekable');
210 }
211
212 if (PHPStreamFunctions::fseek($this->stream, $offset, $whence) === -1) {
213 throw new \RuntimeException("Unable to seek to stream position \"$offset\" with whence \"$whence\"");
214 }
215 }
static fseek($stream, $offset, $whence)
fseek wrapper.

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

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ tell()

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

@inheritDoc

Implements Psr\Http\Message\StreamInterface.

Definition at line 167 of file Stream.php.

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 }
$result

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

+ Here is the call graph for this function:

◆ write()

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

@inheritDoc

Implements Psr\Http\Message\StreamInterface.

Definition at line 239 of file Stream.php.

240 {
241 $this->assertStreamAttached();
242
243 if (!$this->isWritable()) {
244 throw new \RuntimeException('Can not write to a non-writable stream');
245 }
246
247 //we can't know the new size
248 $this->size = null;
249 $result = PHPStreamFunctions::fwrite($this->stream, $string);
250
251 if ($result === false) {
252 throw new \RuntimeException('Unable to write to stream');
253 }
254
255 return $result;
256 }
static fwrite($handle, $string, $length=null)
fwrite wrapper

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

+ Here is the call graph for this function:

Field Documentation

◆ $accessMap

◆ $customMetadata

string[] ILIAS\Filesystem\Stream\Stream::$customMetadata
private

Definition at line 73 of file Stream.php.

◆ $readable

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

Definition at line 49 of file Stream.php.

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

◆ $seekable

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

Definition at line 57 of file Stream.php.

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

◆ $size

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

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

◆ $writeable

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

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

◆ MASK_ACCESS_READ_WRITE

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

Definition at line 21 of file Stream.php.

◆ MASK_ACCESS_WRITE

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

Definition at line 20 of file Stream.php.


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