ILIAS  release_7 Revision v7.30-3-g800a261c036
FlySystemFileStreamAccess.php
Go to the documentation of this file.
1<?php
2declare(strict_types=1);
3
5
12use League\Flysystem\FileExistsException;
13use League\Flysystem\FilesystemInterface;
14
23{
24
28 private $flySystemFS;
29
34 public function __construct(FilesystemInterface $flySystemFS)
35 {
36 $this->flySystemFS = $flySystemFS;
37 }
38
50 public function readStream(string $path) : FileStream
51 {
52 try {
53 $resource = $this->flySystemFS->readStream($path);
54 if ($resource === false) {
55 throw new IOException("Could not open stream for file \"$path\"");
56 }
57
58 $stream = Streams::ofResource($resource);
59 return $stream;
60 } catch (\League\Flysystem\FileNotFoundException $ex) {
61 throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
62 }
63 }
64
79 public function writeStream(string $path, FileStream $stream)
80 {
81 $resource = $stream->detach();
82 try {
83 if (!is_resource($resource)) {
84 throw new \InvalidArgumentException('The given stream must not be detached.');
85 }
86
87 $result = $this->flySystemFS->writeStream($path, $resource);
88
89 if ($result === false) {
90 throw new IOException("Could not write stream to file \"$path\"");
91 }
92 } catch (FileExistsException $ex) {
93 throw new FileAlreadyExistsException("File \"$path\" already exists.", 0, $ex);
94 } finally {
95 if (is_resource($resource)) {
96 fclose($resource);
97 }
98 }
99 }
100
114 public function putStream(string $path, FileStream $stream)
115 {
116 $resource = $stream->detach();
117 try {
118 if (!is_resource($resource)) {
119 throw new \InvalidArgumentException('The given stream must not be detached.');
120 }
121
122 $result = $this->flySystemFS->putStream($path, $resource);
123
124 if ($result === false) {
125 throw new IOException("Could not put stream content into \"$path\"");
126 }
127 } finally {
128 if (is_resource($resource)) {
129 fclose($resource);
130 }
131 }
132 }
133
147 public function updateStream(string $path, FileStream $stream)
148 {
149 $resource = $stream->detach();
150 try {
151 if (!is_resource($resource)) {
152 throw new \InvalidArgumentException('The given stream must not be detached.');
153 }
154
155 $result = $this->flySystemFS->updateStream($path, $resource);
156
157 if ($result === false) {
158 throw new IOException("Could not update file \"$path\"");
159 }
160 } catch (\League\Flysystem\FileNotFoundException $ex) {
161 throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
162 } finally {
163 if (is_resource($resource)) {
164 fclose($resource);
165 }
166 }
167 }
168}
$result
An exception for terminatinating execution or to throw for unit testing.
Class FileNotFoundException Indicates that a file is missing or not found.
Class IOException Indicates general problems with the input or output operations.
Definition: IOException.php:13
Class FlySystemFileStreamAccess Streaming access implementation of the fly system library.
readStream(string $path)
Opens a readable stream of the file.
updateStream(string $path, FileStream $stream)
Updates an existing file.
writeStream(string $path, FileStream $stream)
Writes the stream to a new file.
__construct(FilesystemInterface $flySystemFS)
FlySystemFileStreamAccess constructor.
putStream(string $path, FileStream $stream)
Creates a new file or updates an existing one.
Class Streams Stream factory which enables the user to create streams without the knowledge of the co...
Definition: Streams.php:17
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
Definition: Streams.php:43
Interface FileStreamAccess This interface describes all streaming filesystem operations.
Interface FileStream The base interface for all filesystem streams.
Definition: FileStream.php:18