ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 ()
 @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
 

Protected Member Functions

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

Protected Attributes

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.

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

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

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 }

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

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 }

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

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.

Reimplemented in ILIAS\Filesystem\Stream\ReattachableStream.

Definition at line 366 of file Stream.php.

366 : void
367 {
368 if ($this->stream === null) {
369 throw new \RuntimeException('Stream is detached');
370 }
371 }

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

114 : void
115 {
116 if (is_resource($this->stream)) {
117 PHPStreamFunctions::fclose($this->stream);
118 }
119
120 $this->detach();
121 }

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

Referenced by ILIAS\Filesystem\Stream\Stream\__destruct(), and ILIAS\Repository\IRSS\IRSSWrapper\hasContainerEntry().

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

127 {
129 $this->stream = $this->size = $this->uri = null;
130
131 return $stream;
132 }

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

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

+ 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:366

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

+ Here is the call graph for this function:

◆ getContents()

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

@inheritDoc

Definition at line 290 of file Stream.php.

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

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 }

Referenced by ILIAS\Filesystem\Stream\Stream\__construct(), ILIAS\Repository\IRSS\IRSSWrapper\getResourcePath(), ILIAS\ResourceStorage\Flavour\Machine\DefaultMachines\Extract\PDF\readImage(), and ILIAS\ResourceStorage\Flavour\Machine\DefaultMachines\Extract\Video\readImage().

+ Here is the caller graph for this function:

◆ getSize()

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

@inheritDoc

Definition at line 137 of file Stream.php.

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 }

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

◆ isReadable()

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

@inheritDoc

Definition at line 255 of file Stream.php.

255 : bool
256 {
257 return $this->readable;
258 }

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

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

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

225 : bool
226 {
227 return $this->writeable;
228 }

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

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 }

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

Definition at line 217 of file Stream.php.

217 : void
218 {
219 $this->seek(0);
220 }
seek($offset, $whence=SEEK_SET)
@inheritDoc
Definition: Stream.php:201

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

Reimplemented in ILIAS\Filesystem\Stream\ZIPStream.

Definition at line 201 of file Stream.php.

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)

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

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 }

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

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

◆ $_mode

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

Definition at line 57 of file Stream.php.

◆ $accessMap

◆ $customMetadata

string[] 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: