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