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