ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
FlySystemDirectoryAccess.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
33 
39 {
40  private const KEY_TYPE = 'type';
41  private const KEY_PATH = 'path';
42 
43  public function __construct(
44  private FilesystemOperator $flysystem_operator,
45  private FlySystemFileAccess $flysystem_access
46  ) {
47  }
48 
49  public function hasDir(string $path): bool
50  {
51  return $this->flysystem_operator->directoryExists($path);
52  }
53 
57  public function listContents(string $path = '', bool $recursive = false): array
58  {
59  if ($path !== '') {
61  }
62 
66  $contents = $this->flysystem_operator->listContents($path, $recursive);
67  $metadata_collection = [];
68 
69  foreach ($contents as $content) {
70  $metadata_collection[] = $this->attributesToMetadata($content);
71  }
72 
73  return $metadata_collection;
74  }
75 
80  public function createDir(string $path, string $visibility = Visibility::PUBLIC_ACCESS): void
81  {
82  $this->validateVisibility($visibility);
83 
84  $config = ['visibility' => $visibility];
85  try {
86  $this->flysystem_operator->createDirectory($path, $config);
87  } catch (UnableToCreateDirectory) {
88  throw new IOException("Could not create directory \"$path\"");
89  }
90  }
91 
99  public function copyDir(string $source, string $destination): void
100  {
101  $this->ensureDirectoryExistence($source);
102  $this->ensureEmptyDirectory($destination);
103 
104  $content_list = $this->listContents($source, true);
105 
106  //foreach file and dir
107  foreach ($content_list as $content) {
108  //ignore the directories and only copy the files
109  if ($content->isFile()) {
110  //create destination path
111  $position = strpos($content->getPath(), $source);
112  if ($position !== false) {
113  $destinationFilePath = substr_replace(
114  $content->getPath(),
115  $destination,
116  $position,
117  strlen($source)
118  );
119  $this->flysystem_access->copy($content->getPath(), $destinationFilePath);
120  }
121  }
122  }
123  }
124 
128  private function ensureEmptyDirectory(string $path): void
129  {
130  // check if destination dir is empty
131  try {
132  $destination_content = $this->listContents($path, true);
133  if ($destination_content !== []) {
134  throw new IOException("Destination \"$path\" is not empty can not copy files.");
135  }
136  } catch (UnableToRetrieveMetadata) {
137  //nothing needs to be done the destination was not found
138  }
139  }
140 
145  private function ensureDirectoryExistence(string $path): void
146  {
147  if (!$this->hasDir($path)) {
148  throw new DirectoryNotFoundException("Directory \"$path\" not found.");
149  }
150  }
151 
152  public function deleteDir(string $path): void
153  {
154  try {
155  $this->flysystem_operator->deleteDirectory($path);
156  } catch (UnableToRetrieveMetadata) {
157  throw new IOException("Could not find directory \"$path\".");
158  } catch (UnableToDeleteDirectory|\Throwable) {
159  throw new IOException("Could not delete directory \"$path\".");
160  }
161  if ($this->flysystem_operator->has($path)) {
162  throw new IOException("Could not find directory \"$path\".");
163  }
164  }
165 
166  private function attributesToMetadata(StorageAttributes $attributes): \ILIAS\Filesystem\DTO\Metadata
167  {
168  return new Metadata(
169  $attributes->path(),
170  $attributes->type()
171  );
172  }
173 
178  private function validateVisibility(string $visibility): void
179  {
180  if (strcmp($visibility, Visibility::PRIVATE_ACCESS) === 0) {
181  return;
182  }
183  if (strcmp($visibility, Visibility::PUBLIC_ACCESS) === 0) {
184  return;
185  }
186  throw new \InvalidArgumentException("Invalid visibility expected public or private but got \"$visibility\".");
187  }
188 }
Indicates general problems with the input or output operations.
Definition: IOException.php:27
__construct(private FilesystemOperator $flysystem_operator, private FlySystemFileAccess $flysystem_access)
Class ChatMainBarProvider .
validateVisibility(string $visibility)
Validates if the given visibility is known, otherwise an exception is thrown.
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.
Indicates that the directory is missing or not found.
$path
Definition: ltiservices.php:32
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class holds all default metadata send by the filesystem adapters.
Definition: Metadata.php:31
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:38
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)
Please note that the Visibility interface defines two constants PUBLIC_ACCESS and PRIVATE_ACCESS to e...
hasDir(string $path)
Checks whether the directory exists or not.
const PUBLIC_ACCESS
Public file visibility.
Definition: Visibility.php:34
listContents(string $path='', bool $recursive=false)
Lists the content of a directory.