ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
FlySystemDirectoryAccess.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
7 ;
8 
17 
18 /******************************************************************************
19  *
20  * This file is part of ILIAS, a powerful learning management system.
21  *
22  * ILIAS is licensed with the GPL-3.0, you should have received a copy
23  * of said license along with the source code.
24  *
25  * If this is not the case or you just want to try ILIAS, you'll find
26  * us at:
27  * https://www.ilias.de
28  * https://github.com/ILIAS-eLearning
29  *
30  *****************************************************************************/
39 {
40  private FilesystemInterface $flySystemFS;
42  private static string $metaTypeKey = 'type';
43  private static string $metaPathKey = 'path';
44 
45 
52  public function __construct(FilesystemInterface $flySystemFS, FlySystemFileAccess $fileAccess)
53  {
54  $this->flySystemFS = $flySystemFS;
55  $this->fileAccess = $fileAccess;
56  }
57 
58 
71  public function hasDir(string $path): bool
72  {
73  if ($this->flySystemFS->has($path)) {
74  $meta = $this->flySystemFS->getMetadata($path);
75 
76  if (!(is_array($meta) && array_key_exists(self::$metaTypeKey, $meta))) {
77  throw new IOException("Could not evaluate path type: \"$path\"");
78  }
79 
80  return strcmp($meta[self::$metaTypeKey], MetadataType::DIRECTORY) === 0;
81  }
82 
83  return false;
84  }
85 
86 
101  public function listContents(string $path = '', bool $recursive = false): array
102  {
103  if ($path !== '') {
105  }
106 
107  $contents = $this->flySystemFS->listContents($path, $recursive);
108  $metadataCollection = [];
109 
110  foreach ($contents as $content) {
111  if (!(array_key_exists(self::$metaTypeKey, $content) && array_key_exists(self::$metaPathKey, $content))) {
112  throw new IOException("Invalid metadata received for path \"$path\"");
113  }
114 
115  $metadataCollection[] = $this->arrayToMetadata($content);
116  }
117 
118  return $metadataCollection;
119  }
120 
121 
138  public function createDir(string $path, string $visibility = Visibility::PUBLIC_ACCESS): void
139  {
140  $this->validateVisibility($visibility);
141 
142  $config = ['visibility' => $visibility];
143  $successful = $this->flySystemFS->createDir($path, $config);
144 
145  if (!$successful) {
146  throw new IOException("Could not create directory \"$path\"");
147  }
148  }
149 
150 
168  public function copyDir(string $source, string $destination): void
169  {
170  $this->ensureDirectoryExistence($source);
171  $this->ensureEmptyDirectory($destination);
172 
173  $contentList = $this->listContents($source, true);
174 
175  //foreach file and dir
176  foreach ($contentList as $content) {
177 
178  //ignore the directories and only copy the files
179  if ($content->isFile()) {
180 
181  //create destination path
182  $position = strpos($content->getPath(), $source);
183  if ($position !== false) {
184  $destinationFilePath = substr_replace($content->getPath(), $destination, $position, strlen($source));
185  $this->fileAccess->copy($content->getPath(), $destinationFilePath);
186  }
187  }
188  }
189  }
190 
191 
199  private function ensureEmptyDirectory(string $path): void
200  {
201 
202  //check if destination dir is empty
203  try {
204  $destinationContent = $this->listContents($path, true);
205  if (count($destinationContent) !== 0) {
206  throw new IOException("Destination \"$path\" is not empty can not copy files.");
207  }
208  } catch (DirectoryNotFoundException $ex) {
209  //nothing needs to be done the destination was not found
210  }
211  }
212 
213 
222  private function ensureDirectoryExistence(string $path): void
223  {
224  if (!$this->hasDir($path)) {
225  throw new DirectoryNotFoundException("Directory \"$path\" not found.");
226  }
227  }
228 
229 
241  public function deleteDir(string $path): void
242  {
243  try {
244  if ($this->flySystemFS->deleteDir($path) === false) {
245  throw new IOException("Could not delete directory \"$path\".");
246  }
247  } catch (RootViolationException $ex) {
248  throw new IOException('The filesystem root must not be deleted.', 0, $ex);
249  }
250  }
251 
252 
263  private function arrayToMetadata(array $metadataArray): \ILIAS\Filesystem\DTO\Metadata
264  {
265  return new Metadata(
266  $metadataArray[self::$metaPathKey],
267  $metadataArray[self::$metaTypeKey]
268  );
269  }
270 
271 
278  private function validateVisibility(string $visibility): void
279  {
280  if (strcmp($visibility, Visibility::PRIVATE_ACCESS) !== 0 && strcmp($visibility, Visibility::PUBLIC_ACCESS) !== 0) {
281  throw new \InvalidArgumentException("Invalid visibility expected public or private but got \"$visibility\".");
282  }
283  }
284 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ChatMainBarProvider .
__construct(FilesystemInterface $flySystemFS, FlySystemFileAccess $fileAccess)
FlySystemDirectoryAccess constructor.
validateVisibility(string $visibility)
Validates if the given visibility is known, otherwise an exception is thrown.
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ensureEmptyDirectory(string $path)
Ensures that the given path does not exist or is empty.
$path
Definition: ltiservices.php:32
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:42
ensureDirectoryExistence(string $path)
Checks if the directory exists.
copyDir(string $source, string $destination)
Copy all childes of the source recursive to the destination.
createDir(string $path, string $visibility=Visibility::PUBLIC_ACCESS)
Create a new directory.
arrayToMetadata(array $metadataArray)
Parses a metadata array into a metadata object.
const DIRECTORY
The subject is a directory.
listContents(string $path='', bool $recursive=false)
Lists the content of a directory.
hasDir(string $path)
Checks whether the directory exists or not.
const PUBLIC_ACCESS
Public file visibility.
Definition: Visibility.php:37
$source
Definition: metadata.php:93
Class FlySystemFileAccessTest disabled disabled disabled.