ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
FlySystemDirectoryAccess.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28use League\Flysystem\CorruptedPathDetected;
29use League\Flysystem\FilesystemOperator;
30use League\Flysystem\UnableToRetrieveMetadata;
31use League\Flysystem\UnableToCreateDirectory;
32use League\Flysystem\StorageAttributes;
33
39{
43 private const KEY_TYPE = 'type';
47 private const KEY_PATH = 'path';
48
49 public function __construct(
50 private FilesystemOperator $flysystem_operator,
51 private FlySystemFileAccess $flysystem_access
52 ) {
53 }
54
55 public function hasDir(string $path): bool
56 {
57 try {
58 return $this->flysystem_operator->directoryExists($path);
59 } catch (CorruptedPathDetected) {
60 // Paths containing funky whitespace (e.g. a tab or a line break) are rejected
61 // by the path normalizer. Such a path can never be a usable directory, but it
62 // must not abort the caller either, since these paths do occur on disk.
63 return false;
64 }
65 }
66
70 public function listContents(string $path = '', bool $recursive = false): array
71 {
72 if ($path !== '') {
73 $this->ensureDirectoryExistence($path);
74 }
75
79 $contents = $this->flysystem_operator->listContents($path, $recursive);
80 $metadata_collection = [];
81
82 foreach ($contents as $content) {
83 $metadata_collection[] = $this->attributesToMetadata($content);
84 }
85
86 return $metadata_collection;
87 }
88
93 public function createDir(string $path, string $visibility = Visibility::PUBLIC_ACCESS): void
94 {
95 $this->validateVisibility($visibility);
96
97 $config = ['visibility' => $visibility];
98 try {
99 $this->flysystem_operator->createDirectory($path, $config);
100 } catch (UnableToCreateDirectory) {
101 throw new IOException("Could not create directory \"$path\"");
102 }
103 }
104
112 public function copyDir(string $source, string $destination): void
113 {
114 $this->ensureDirectoryExistence($source);
115 $this->ensureEmptyDirectory($destination);
116
117 $content_list = $this->listContents($source, true);
118
119 //foreach file and dir
120 foreach ($content_list as $content) {
121 //ignore the directories and only copy the files
122 if ($content->isFile()) {
123 //create destination path
124 $position = strpos($content->getPath(), $source);
125 if ($position !== false) {
126 $destinationFilePath = substr_replace(
127 $content->getPath(),
128 $destination,
129 $position,
130 strlen($source)
131 );
132 $this->flysystem_access->copy($content->getPath(), $destinationFilePath);
133 }
134 }
135 }
136 }
137
141 private function ensureEmptyDirectory(string $path): void
142 {
143 // check if destination dir is empty
144 try {
145 $destination_content = $this->listContents($path, true);
146 if ($destination_content !== []) {
147 throw new IOException("Destination \"$path\" is not empty can not copy files.");
148 }
149 } catch (UnableToRetrieveMetadata) {
150 //nothing needs to be done the destination was not found
151 }
152 }
153
158 private function ensureDirectoryExistence(string $path): void
159 {
160 if (!$this->hasDir($path)) {
161 throw new DirectoryNotFoundException("Directory \"$path\" not found.");
162 }
163 }
164
165 public function deleteDir(string $path): void
166 {
167 try {
168 $this->flysystem_operator->deleteDirectory($path);
169 } catch (UnableToRetrieveMetadata) {
170 throw new IOException("Could not find directory \"$path\".");
171 } catch (UnableToDeleteDirectory|\Throwable) {
172 throw new IOException("Could not delete directory \"$path\".");
173 }
174 if ($this->flysystem_operator->has($path)) {
175 throw new IOException("Could not find directory \"$path\".");
176 }
177 }
178
179 private function attributesToMetadata(StorageAttributes $attributes): Metadata
180 {
181 return new Metadata(
182 $attributes->path(),
183 $attributes->type()
184 );
185 }
186
191 private function validateVisibility(string $visibility): void
192 {
193 if (strcmp($visibility, Visibility::PRIVATE_ACCESS) === 0) {
194 return;
195 }
196 if (strcmp($visibility, Visibility::PUBLIC_ACCESS) === 0) {
197 return;
198 }
199 throw new \InvalidArgumentException("Invalid visibility expected public or private but got \"$visibility\".");
200 }
201}
This class holds all default metadata send by the filesystem adapters.
Definition: Metadata.php:33
Indicates that the directory is missing or not found.
Indicates general problems with the input or output operations.
Definition: IOException.php:28
createDir(string $path, string $visibility=Visibility::PUBLIC_ACCESS)
Please note that the Visibility interface defines two constants PUBLIC_ACCESS and PRIVATE_ACCESS to e...
__construct(private FilesystemOperator $flysystem_operator, private FlySystemFileAccess $flysystem_access)
copyDir(string $source, string $destination)
Copy all childes of the source recursive to the destination.
hasDir(string $path)
Checks whether the directory exists or not.
ensureEmptyDirectory(string $path)
Ensures that the given path does not exist or is empty.
ensureDirectoryExistence(string $path)
Checks if the directory exists.
validateVisibility(string $visibility)
Validates if the given visibility is known, otherwise an exception is thrown.
listContents(string $path='', bool $recursive=false)
Lists the content of a directory.
This interface provides the available options for the filesystem right management of the filesystem s...
Definition: Visibility.php:30
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:38
const PUBLIC_ACCESS
Public file visibility.
Definition: Visibility.php:34
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...