ILIAS  release_8 Revision v8.24
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...
 

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

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

Static Private Attributes

static array $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 32 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 }
getMetadata($key=null)
@inheritDoc
Definition: Stream.php:315

References 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 363 of file Stream.php.

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

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

+ Here is the call graph for this function:

Member Function Documentation

◆ __toString()

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

@inheritDoc

Definition at line 348 of file Stream.php.

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

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

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

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

Definition at line 110 of file Stream.php.

110 : void
111 {
112 if (is_resource($this->stream)) {
113 PHPStreamFunctions::fclose($this->stream);
114 }
115
116 $this->detach();
117 }

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

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(), and ILIAS\Filesystem\Stream\Streams\ofPsr7Stream().

+ Here is the caller graph for this function:

◆ eof()

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

@inheritDoc

Definition at line 183 of file Stream.php.

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:379

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

+ Here is the call graph for this function:

◆ getContents()

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

@inheritDoc

Definition at line 298 of file Stream.php.

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

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

Definition at line 315 of file Stream.php.

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

References ILIAS\LTI\ToolProvider\$key.

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

+ Here is the caller graph for this function:

◆ getSize()

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

@inheritDoc

Definition at line 135 of file Stream.php.

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

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

◆ isReadable()

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

@inheritDoc

Definition at line 261 of file Stream.php.

261 : bool
262 {
263 return $this->readable;
264 }

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

Definition at line 194 of file Stream.php.

194 : bool
195 {
196 return $this->seekable;
197 }

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

Definition at line 229 of file Stream.php.

229 : bool
230 {
231 return $this->writeable;
232 }

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

Definition at line 270 of file Stream.php.

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

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

Referenced by ILIAS\ResourceStorage\Consumer\StreamAccess\TokenStream\getMimeType().

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

◆ rewind()

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

@inheritDoc

Definition at line 220 of file Stream.php.

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

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

Referenced by ILIAS\Filesystem\Stream\Stream\__toString(), and ILIAS\ResourceStorage\Consumer\StreamAccess\TokenStream\getMimeType().

+ 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

Definition at line 203 of file Stream.php.

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

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

Definition at line 166 of file Stream.php.

167 {
168 $this->assertStreamAttached();
169
170 $result = PHPStreamFunctions::ftell($this->stream);
171
172 if ($result === false) {
173 throw new \RuntimeException('Unable to determine stream position');
174 }
175
176 return $result;
177 }

References 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

Definition at line 238 of file Stream.php.

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

References 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 61 of file Stream.php.

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

◆ $seekable

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

Definition at line 63 of file Stream.php.

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

◆ $size

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

Definition at line 68 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 = null
private

Definition at line 69 of file Stream.php.

◆ $writeable

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

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

◆ MASK_ACCESS_READ_WRITE

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

Definition at line 36 of file Stream.php.

◆ MASK_ACCESS_WRITE

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

Definition at line 35 of file Stream.php.


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