ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
FlySystemFileStreamAccess.php
Go to the documentation of this file.
1<?php
2
4
13
24{
25
29 private $flySystemFS;
30
37 {
38 $this->flySystemFS = $flySystemFS;
39 }
40
57 public function readStream($path)
58 {
59 try {
60 $resource = $this->flySystemFS->readStream($path);
61 if ($resource === false) {
62 throw new IOException("Could not open stream for file \"$path\"");
63 }
64
65 $stream = Streams::ofResource($resource);
66 return $stream;
67 } catch (\League\Flysystem\FileNotFoundException $ex) {
68 throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
69 }
70 }
71
72
92 {
93 $resource = $stream->detach();
94 try {
95 if (!is_resource($resource)) {
96 throw new \InvalidArgumentException('The given stream must not be detached.');
97 }
98
99 $result = $this->flySystemFS->writeStream($path, $resource);
100
101 if ($result === false) {
102 throw new IOException("Could not write stream to file \"$path\"");
103 }
104 } catch (FileExistsException $ex) {
105 throw new FileAlreadyExistsException("File \"$path\" already exists.", 0, $ex);
106 } finally {
107 if (is_resource($resource)) {
108 fclose($resource);
109 }
110 }
111 }
112
113
132 {
133 $resource = $stream->detach();
134 try {
135 if (!is_resource($resource)) {
136 throw new \InvalidArgumentException('The given stream must not be detached.');
137 }
138
139 $result = $this->flySystemFS->putStream($path, $resource);
140
141 if ($result === false) {
142 throw new IOException("Could not put stream content into \"$path\"");
143 }
144 } finally {
145 if (is_resource($resource)) {
146 fclose($resource);
147 }
148 }
149 }
150
151
169 {
170 $resource = $stream->detach();
171 try {
172 if (!is_resource($resource)) {
173 throw new \InvalidArgumentException('The given stream must not be detached.');
174 }
175
176 $result = $this->flySystemFS->updateStream($path, $resource);
177
178 if ($result === false) {
179 throw new IOException("Could not update file \"$path\"");
180 }
181 } catch (\League\Flysystem\FileNotFoundException $ex) {
182 throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
183 } finally {
184 if (is_resource($resource)) {
185 fclose($resource);
186 }
187 }
188 }
189}
$result
An exception for terminatinating execution or to throw for unit testing.
updateStream($path, FileStream $stream)
Updates an existing file.
writeStream($path, FileStream $stream)
Writes the stream to a new file.
__construct(FilesystemInterface $flySystemFS)
FlySystemFileStreamAccess constructor.
putStream($path, FileStream $stream)
Creates a new file or updates an existing one.
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
Definition: Streams.php:51
$stream
PHP stream implementation.