ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Streams.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use Psr\Http\Message\StreamInterface;
24
31final class Streams
32{
41 public static function ofString(string $string): Stream
42 {
43 if (!is_string($string)) {
44 throw new \InvalidArgumentException(
45 'The argument $string must be of type string but was "' . gettype($string) . '"'
46 );
47 }
48
49 $stream = new Stream(fopen('php://memory', 'rw'));
50 $stream->write($string);
51 return $stream;
52 }
53
64 public static function ofResource($resource): Stream
65 {
66 if (!is_resource($resource)) {
67 throw new \InvalidArgumentException(
68 'The argument $resource must be of type resource but was "' . gettype($resource) . '"'
69 );
70 }
71 return new Stream($resource);
72 }
73
74 public static function ofReattachableResource($resource): ReattachableStream
75 {
76 if (!is_resource($resource)) {
77 throw new \InvalidArgumentException(
78 'The argument $resource must be of type resource but was "' . gettype($resource) . '"'
79 );
80 }
81 return new ReattachableStream($resource);
82 }
83
84 public static function ofFileInsideZIP(string $path_to_zip, string $path_inside_zip): ZIPStream
85 {
86 // we try to open the zip file with the path inside the zip file, once with a leading slash and once without
87 try {
88 $resource = fopen('zip://' . $path_to_zip . '#/' . $path_inside_zip, 'rb');
89 } catch (\Throwable) {
90 $resource = null;
91 }
92 try {
93 $resource = $resource ?: fopen('zip://' . $path_to_zip . '#' . $path_inside_zip, 'rb');
94 } catch (\Throwable) {
95 $resource = null;
96 }
97
98 if (!is_resource($resource)) {
99 throw new \InvalidArgumentException(
100 'The argument $path_to_zip must be an existing zip file path and $path_inside_zip must be a valid path inside the zip file.'
101 );
102 }
103 return new ZIPStream($resource);
104 }
105
113 public static function ofPsr7Stream(StreamInterface $stream): Stream
114 {
115 $resource = $stream->detach();
116 return self::ofResource($resource);
117 }
118}
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static ofReattachableResource($resource)
Definition: Streams.php:74
static ofPsr7Stream(StreamInterface $stream)
Create a FileStream from a Psr7 compliant stream.
Definition: Streams.php:113
static ofFileInsideZIP(string $path_to_zip, string $path_inside_zip)
Definition: Streams.php:84
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
Definition: Streams.php:64
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: FileStream.php:19