ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
FlySystemDirectoryAccess.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types=1);
3 
5 
6 ;
7 
16 
25 {
26 
30  private $flySystemFS;
34  private $fileAccess;
35  private static $metaTypeKey = 'type';
36  private static $metaPathKey = 'path';
37 
38 
46  {
47  $this->flySystemFS = $flySystemFS;
48  $this->fileAccess = $fileAccess;
49  }
50 
51 
64  public function hasDir(string $path) : bool
65  {
66  if ($this->flySystemFS->has($path)) {
67  $meta = $this->flySystemFS->getMetadata($path);
68 
69  if (!(is_array($meta) && array_key_exists(self::$metaTypeKey, $meta))) {
70  throw new IOException("Could not evaluate path type: \"$path\"");
71  }
72 
73  return strcmp($meta[self::$metaTypeKey], MetadataType::DIRECTORY) === 0;
74  }
75 
76  return false;
77  }
78 
79 
94  public function listContents(string $path = '', bool $recursive = false) : array
95  {
96  if ($path !== '') {
98  }
99 
100  $contents = $this->flySystemFS->listContents($path, $recursive);
101  $metadataCollection = [];
102 
103  foreach ($contents as $content) {
104  if (!(array_key_exists(self::$metaTypeKey, $content) && array_key_exists(self::$metaPathKey, $content))) {
105  throw new IOException("Invalid metadata received for path \"$path\"");
106  }
107 
108  $metadataCollection[] = $this->arrayToMetadata($content);
109  }
110 
111  return $metadataCollection;
112  }
113 
114 
132  public function createDir(string $path, string $visibility = Visibility::PUBLIC_ACCESS)
133  {
134  $this->validateVisibility($visibility);
135 
136  $config = ['visibility' => $visibility];
137  $successful = $this->flySystemFS->createDir($path, $config);
138 
139  if (!$successful) {
140  throw new IOException("Could not create directory \"$path\"");
141  }
142  }
143 
144 
163  public function copyDir(string $source, string $destination)
164  {
165  $this->ensureDirectoryExistence($source);
166  $this->ensureEmptyDirectory($destination);
167 
168  $contentList = $this->listContents($source, true);
169 
170  //foreach file and dir
171  foreach ($contentList as $content) {
172 
173  //ignore the directories and only copy the files
174  if ($content->isFile()) {
175 
176  //create destination path
177  $position = strpos($content->getPath(), $source);
178  if ($position !== false) {
179  $destinationFilePath = substr_replace($content->getPath(), $destination, $position, strlen($source));
180  $this->fileAccess->copy($content->getPath(), $destinationFilePath);
181  }
182  }
183  }
184  }
185 
186 
194  private function ensureEmptyDirectory(string $path)
195  {
196 
197  //check if destination dir is empty
198  try {
199  $destinationContent = $this->listContents($path, true);
200  if (count($destinationContent) !== 0) {
201  throw new IOException("Destination \"$path\" is not empty can not copy files.");
202  }
203  } catch (DirectoryNotFoundException $ex) {
204  //nothing needs to be done the destination was not found
205  }
206  }
207 
208 
217  private function ensureDirectoryExistence(string $path)
218  {
219  if (!$this->hasDir($path)) {
220  throw new DirectoryNotFoundException("Directory \"$path\" not found.");
221  }
222  }
223 
224 
237  public function deleteDir(string $path)
238  {
239  try {
240  if ($this->flySystemFS->deleteDir($path) === false) {
241  throw new IOException("Could not delete directory \"$path\".");
242  }
243  } catch (RootViolationException $ex) {
244  throw new IOException('The filesystem root must not be deleted.', 0, $ex);
245  }
246  }
247 
248 
261  private function arrayToMetadata(array $metadataArray)
262  {
263  return new Metadata(
264  $metadataArray[self::$metaPathKey],
265  $metadataArray[self::$metaTypeKey]
266  );
267  }
268 
269 
277  private function validateVisibility($visibility)
278  {
279  if (strcmp($visibility, Visibility::PRIVATE_ACCESS) !== 0 && strcmp($visibility, Visibility::PUBLIC_ACCESS) !== 0) {
280  throw new \InvalidArgumentException("Invalid visibility expected public or private but got \"$visibility\".");
281  }
282  }
283 }
$path
Definition: aliased.php:25
$config
Definition: bootstrap.php:15
__construct(FilesystemInterface $flySystemFS, FlySystemFileAccess $fileAccess)
FlySystemDirectoryAccess constructor.
$destination
ensureEmptyDirectory(string $path)
Ensures that the given path does not exist or is empty.
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:30
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.
validateVisibility($visibility)
Validates if the given visibility is known, otherwise an exception is thrown.
$source
Definition: linkback.php:22
const PUBLIC_ACCESS
Public file visibility.
Definition: Visibility.php:25