ILIAS  trunk Revision v11.0_alpha-2645-g16283d3b3f8
FlySystemDirectoryAccess.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
32 
38 {
42  private const KEY_TYPE = 'type';
46  private const KEY_PATH = 'path';
47 
48  public function __construct(
49  private FilesystemOperator $flysystem_operator,
50  private FlySystemFileAccess $flysystem_access
51  ) {
52  }
53 
54  public function hasDir(string $path): bool
55  {
56  return $this->flysystem_operator->directoryExists($path);
57  }
58 
62  public function listContents(string $path = '', bool $recursive = false): array
63  {
64  if ($path !== '') {
66  }
67 
71  $contents = $this->flysystem_operator->listContents($path, $recursive);
72  $metadata_collection = [];
73 
74  foreach ($contents as $content) {
75  $metadata_collection[] = $this->attributesToMetadata($content);
76  }
77 
78  return $metadata_collection;
79  }
80 
85  public function createDir(string $path, string $visibility = Visibility::PUBLIC_ACCESS): void
86  {
87  $this->validateVisibility($visibility);
88 
89  $config = ['visibility' => $visibility];
90  try {
91  $this->flysystem_operator->createDirectory($path, $config);
92  } catch (UnableToCreateDirectory) {
93  throw new IOException("Could not create directory \"$path\"");
94  }
95  }
96 
104  public function copyDir(string $source, string $destination): void
105  {
106  $this->ensureDirectoryExistence($source);
107  $this->ensureEmptyDirectory($destination);
108 
109  $content_list = $this->listContents($source, true);
110 
111  //foreach file and dir
112  foreach ($content_list as $content) {
113  //ignore the directories and only copy the files
114  if ($content->isFile()) {
115  //create destination path
116  $position = strpos($content->getPath(), $source);
117  if ($position !== false) {
118  $destinationFilePath = substr_replace(
119  $content->getPath(),
120  $destination,
121  $position,
122  strlen($source)
123  );
124  $this->flysystem_access->copy($content->getPath(), $destinationFilePath);
125  }
126  }
127  }
128  }
129 
133  private function ensureEmptyDirectory(string $path): void
134  {
135  // check if destination dir is empty
136  try {
137  $destination_content = $this->listContents($path, true);
138  if ($destination_content !== []) {
139  throw new IOException("Destination \"$path\" is not empty can not copy files.");
140  }
141  } catch (UnableToRetrieveMetadata) {
142  //nothing needs to be done the destination was not found
143  }
144  }
145 
150  private function ensureDirectoryExistence(string $path): void
151  {
152  if (!$this->hasDir($path)) {
153  throw new DirectoryNotFoundException("Directory \"$path\" not found.");
154  }
155  }
156 
157  public function deleteDir(string $path): void
158  {
159  try {
160  $this->flysystem_operator->deleteDirectory($path);
161  } catch (UnableToRetrieveMetadata) {
162  throw new IOException("Could not find directory \"$path\".");
163  } catch (UnableToDeleteDirectory|\Throwable) {
164  throw new IOException("Could not delete directory \"$path\".");
165  }
166  if ($this->flysystem_operator->has($path)) {
167  throw new IOException("Could not find directory \"$path\".");
168  }
169  }
170 
171  private function attributesToMetadata(StorageAttributes $attributes): Metadata
172  {
173  return new Metadata(
174  $attributes->path(),
175  $attributes->type()
176  );
177  }
178 
183  private function validateVisibility(string $visibility): void
184  {
185  if (strcmp($visibility, Visibility::PRIVATE_ACCESS) === 0) {
186  return;
187  }
188  if (strcmp($visibility, Visibility::PUBLIC_ACCESS) === 0) {
189  return;
190  }
191  throw new \InvalidArgumentException("Invalid visibility expected public or private but got \"$visibility\".");
192  }
193 }
Indicates general problems with the input or output operations.
Definition: IOException.php:27
__construct(private FilesystemOperator $flysystem_operator, private FlySystemFileAccess $flysystem_access)
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:29
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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.