ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
FlySystemFileStreamAccess.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
15 
16 /******************************************************************************
17  *
18  * This file is part of ILIAS, a powerful learning management system.
19  *
20  * ILIAS is licensed with the GPL-3.0, you should have received a copy
21  * of said license along with the source code.
22  *
23  * If this is not the case or you just want to try ILIAS, you'll find
24  * us at:
25  * https://www.ilias.de
26  * https://github.com/ILIAS-eLearning
27  *
28  *****************************************************************************/
39 {
40  private FilesystemInterface $flySystemFS;
41 
47  public function __construct(FilesystemInterface $flySystemFS)
48  {
49  $this->flySystemFS = $flySystemFS;
50  }
51 
68  public function readStream(string $path): FileStream
69  {
70  try {
71  $resource = $this->flySystemFS->readStream($path);
72  if ($resource === false) {
73  throw new IOException("Could not open stream for file \"$path\"");
74  }
75 
76  $stream = Streams::ofResource($resource);
77  return $stream;
78  } catch (\League\Flysystem\FileNotFoundException $ex) {
79  throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
80  }
81  }
82 
83 
100  public function writeStream(string $path, FileStream $stream): void
101  {
102  $resource = $stream->detach();
103  try {
104  if (!is_resource($resource)) {
105  throw new \InvalidArgumentException('The given stream must not be detached.');
106  }
107 
108  $result = $this->flySystemFS->writeStream($path, $resource);
109 
110  if ($result === false) {
111  throw new IOException("Could not write stream to file \"$path\"");
112  }
113  } catch (FileExistsException $ex) {
114  throw new FileAlreadyExistsException("File \"$path\" already exists.", 0, $ex);
115  } finally {
116  if (is_resource($resource)) {
117  fclose($resource);
118  }
119  }
120  }
121 
122 
138  public function putStream(string $path, FileStream $stream): void
139  {
140  $resource = $stream->detach();
141  try {
142  if (!is_resource($resource)) {
143  throw new \InvalidArgumentException('The given stream must not be detached.');
144  }
145 
146  $result = $this->flySystemFS->putStream($path, $resource);
147 
148  if ($result === false) {
149  throw new IOException("Could not put stream content into \"$path\"");
150  }
151  } finally {
152  if (is_resource($resource)) {
153  fclose($resource);
154  }
155  }
156  }
157 
158 
174  public function updateStream(string $path, FileStream $stream): void
175  {
176  $resource = $stream->detach();
177  try {
178  if (!is_resource($resource)) {
179  throw new \InvalidArgumentException('The given stream must not be detached.');
180  }
181 
182  $result = $this->flySystemFS->updateStream($path, $resource);
183 
184  if ($result === false) {
185  throw new IOException("Could not update file \"$path\"");
186  }
187  } catch (\League\Flysystem\FileNotFoundException $ex) {
188  throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
189  } finally {
190  if (is_resource($resource)) {
191  fclose($resource);
192  }
193  }
194  }
195 }
__construct(FilesystemInterface $flySystemFS)
FlySystemFileStreamAccess constructor.
$path
Definition: ltiservices.php:32
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
Definition: Streams.php:65
updateStream(string $path, FileStream $stream)
Updates an existing file.
readStream(string $path)
Opens a readable stream of the file.
writeStream(string $path, FileStream $stream)
Writes the stream to a new file.
Interface FileStream.
Definition: FileStream.php:33
putStream(string $path, FileStream $stream)
Creates a new file or updates an existing one.