ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
FlySystemFileStreamAccess.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
35 
41 {
42  public function __construct(
43  private FilesystemOperator $flysystem_operator
44  ) {
45  }
46 
52  public function readStream(string $path): FileStream
53  {
54  try {
55  $resource = $this->flysystem_operator->readStream($path);
56  if ($resource === false) {
57  throw new IOException("Could not open stream for file \"$path\"");
58  }
59  return Streams::ofResource($resource);
60  } catch (UnableToRetrieveMetadata|UnableToReadFile $ex) {
61  throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
62  }
63  }
64 
73  public function writeStream(string $path, FileStream $stream): void
74  {
75  $resource = $stream->detach();
76  if (!is_resource($resource)) {
77  throw new \InvalidArgumentException('The given stream must not be detached.');
78  }
79  if ($this->flysystem_operator->fileExists($path)) {
80  throw new FileAlreadyExistsException("File \"$path\" already exists.");
81  }
82  try {
83  $this->flysystem_operator->writeStream($path, $resource);
84  } catch (UnableToWriteFile $ex) {
85  throw new IOException("Could not write stream to file \"$path\"", 0, $ex);
86  } finally {
87  if (is_resource($resource)) {
88  fclose($resource);
89  }
90  }
91  }
92 
98  public function putStream(string $path, FileStream $stream): void
99  {
100  $resource = $stream->detach();
101  try {
102  if (!is_resource($resource)) {
103  throw new \InvalidArgumentException('The given stream must not be detached.');
104  }
105 
106  $result = $this->flysystem_operator->putStream($path, $resource);
107 
108  if ($result === false) {
109  throw new IOException("Could not put stream content into \"$path\"");
110  }
111  } finally {
112  if (is_resource($resource)) {
113  fclose($resource);
114  }
115  }
116  }
117 
125  public function updateStream(string $path, FileStream $stream): void
126  {
127  $resource = $stream->detach();
128  try {
129  if (!is_resource($resource)) {
130  throw new \InvalidArgumentException('The given stream must not be detached.');
131  }
132  // FlySystem 3 has no updateStream method, so we have to use writeStream instead.
133  $this->flysystem_operator->writeStream($path, $resource);
134  } catch (UnableToWriteFile $ex) {
135  throw new FileNotFoundException("Unable to update Stream in \"$path\".", 0, $ex);
136  } finally {
137  if (is_resource($resource)) {
138  fclose($resource);
139  }
140  }
141  }
142 }
Indicates general problems with the input or output operations.
Definition: IOException.php:27
$path
Definition: ltiservices.php:30
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
Definition: Streams.php:64
updateStream(string $path, FileStream $stream)
Updates an existing file.
readStream(string $path)
Opens a readable stream of the file.
Indicates that a file is missing or not found.
__construct(private FilesystemOperator $flysystem_operator)
writeStream(string $path, FileStream $stream)
Writes the stream to a new file.
The base interface for all filesystem streams.
Definition: FileStream.php:31
putStream(string $path, FileStream $stream)
Creates a new file or updates an existing one.