ILIAS  release_7 Revision v7.30-3-g800a261c036
FlySystemDirectoryAccess.php
Go to the documentation of this file.
1<?php
2declare(strict_types=1);
3
5
6;
7
14use League\Flysystem\FilesystemInterface;
15use League\Flysystem\RootViolationException;
16
24{
25
29 private $flySystemFS;
33 private $fileAccess;
34 private static $metaTypeKey = 'type';
35 private static $metaPathKey = 'path';
36
43 public function __construct(FilesystemInterface $flySystemFS, FlySystemFileAccess $fileAccess)
44 {
45 $this->flySystemFS = $flySystemFS;
46 $this->fileAccess = $fileAccess;
47 }
48
57 public function hasDir(string $path) : bool
58 {
59 if ($this->flySystemFS->has($path)) {
60 $meta = $this->flySystemFS->getMetadata($path);
61
62 if (!(is_array($meta) && array_key_exists(self::$metaTypeKey, $meta))) {
63 throw new IOException("Could not evaluate path type: \"$path\"");
64 }
65
66 return strcmp($meta[self::$metaTypeKey], MetadataType::DIRECTORY) === 0;
67 }
68
69 return false;
70 }
71
82 public function listContents(string $path = '', bool $recursive = false) : array
83 {
84 if ($path !== '') {
85 $this->ensureDirectoryExistence($path);
86 }
87
88 $contents = $this->flySystemFS->listContents($path, $recursive);
89 $metadataCollection = [];
90
91 foreach ($contents as $content) {
92 if (!(array_key_exists(self::$metaTypeKey, $content) && array_key_exists(self::$metaPathKey, $content))) {
93 throw new IOException("Invalid metadata received for path \"$path\"");
94 }
95
96 $metadataCollection[] = $this->arrayToMetadata($content);
97 }
98
99 return $metadataCollection;
100 }
101
114 public function createDir(string $path, string $visibility = Visibility::PUBLIC_ACCESS)
115 {
116 $this->validateVisibility($visibility);
117
118 $config = ['visibility' => $visibility];
119 $successful = $this->flySystemFS->createDir($path, $config);
120
121 if (!$successful) {
122 throw new IOException("Could not create directory \"$path\"");
123 }
124 }
125
139 public function copyDir(string $source, string $destination)
140 {
141 $this->ensureDirectoryExistence($source);
142 $this->ensureEmptyDirectory($destination);
143
144 $contentList = $this->listContents($source, true);
145
146 //foreach file and dir
147 foreach ($contentList as $content) {
148
149 //ignore the directories and only copy the files
150 if ($content->isFile()) {
151
152 //create destination path
153 $position = strpos($content->getPath(), $source);
154 if ($position !== false) {
155 $destinationFilePath = substr_replace($content->getPath(), $destination, $position,
156 strlen($source));
157 $this->fileAccess->copy($content->getPath(), $destinationFilePath);
158 }
159 }
160 }
161 }
162
168 private function ensureEmptyDirectory(string $path)
169 {
170
171 //check if destination dir is empty
172 try {
173 $destinationContent = $this->listContents($path, true);
174 if (count($destinationContent) !== 0) {
175 throw new IOException("Destination \"$path\" is not empty can not copy files.");
176 }
177 } catch (DirectoryNotFoundException $ex) {
178 //nothing needs to be done the destination was not found
179 }
180 }
181
188 private function ensureDirectoryExistence(string $path)
189 {
190 if (!$this->hasDir($path)) {
191 throw new DirectoryNotFoundException("Directory \"$path\" not found.");
192 }
193 }
194
203 public function deleteDir(string $path)
204 {
205 try {
206 if ($this->flySystemFS->deleteDir($path) === false) {
207 throw new IOException("Could not delete directory \"$path\".");
208 }
209 } catch (RootViolationException $ex) {
210 throw new IOException('The filesystem root must not be deleted.', 0, $ex);
211 }
212 }
213
224 private function arrayToMetadata(array $metadataArray)
225 {
226 return new Metadata(
227 $metadataArray[self::$metaPathKey],
228 $metadataArray[self::$metaTypeKey]
229 );
230 }
231
238 private function validateVisibility($visibility)
239 {
240 if (strcmp($visibility, Visibility::PRIVATE_ACCESS) !== 0 && strcmp($visibility,
242 throw new \InvalidArgumentException("Invalid visibility expected public or private but got \"$visibility\".");
243 }
244 }
245}
An exception for terminatinating execution or to throw for unit testing.
Class Metadata This class holds all default metadata send by the filesystem adapters.
Definition: Metadata.php:17
Class DirectoryNotFoundException Indicates that the directory is missing or not found.
Class IOException Indicates general problems with the input or output operations.
Definition: IOException.php:13
createDir(string $path, string $visibility=Visibility::PUBLIC_ACCESS)
Create a new directory.
copyDir(string $source, string $destination)
Copy all childes of the source recursive to the destination.
listContents(string $path='', bool $recursive=false)
Lists the content of a directory.
hasDir(string $path)
Checks whether the directory exists or not.
__construct(FilesystemInterface $flySystemFS, FlySystemFileAccess $fileAccess)
FlySystemDirectoryAccess constructor.
validateVisibility($visibility)
Validates if the given visibility is known, otherwise an exception is thrown.
ensureEmptyDirectory(string $path)
Ensures that the given path does not exist or is empty.
ensureDirectoryExistence(string $path)
Checks if the directory exists.
arrayToMetadata(array $metadataArray)
Parses a metadata array into a metadata object.
Class FlySystemFileAccess Fly system file access implementation.
Class MetadataType The possible metadata types of the filesystem metadata.
const DIRECTORY
The subject is a directory.
Interface DirectoryAccess Defines all directory access operations of the filesystem.
Interface Visibility This interface provides the available options for the filesystem right managemen...
Definition: Visibility.php:16
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:27
const PUBLIC_ACCESS
Public file visibility.
Definition: Visibility.php:22
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
$source
Definition: metadata.php:76