ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
FlySystemDirectoryAccess.php
Go to the documentation of this file.
1 <?php
2 
4 
5 ;
6 
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($path)
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($path = '', $recursive = false)
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($path, $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($source, $destination)
164  {
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($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($path)
218  {
219  if (!$this->hasDir($path)) {
220  throw new DirectoryNotFoundException("Directory \"$path\" not found.");
221  }
222  }
223 
224 
237  public function deleteDir($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 }
__construct(FilesystemInterface $flySystemFS, FlySystemFileAccess $fileAccess)
FlySystemDirectoryAccess constructor.
listContents($path='', $recursive=false)
Lists the content of a directory.
$destination
hasDir($path)
Checks whether the directory exists or not.
ensureEmptyDirectory($path)
Ensures that the given path does not exist or is empty.
copyDir($source, $destination)
Copy all childes of the source recursive to the destination.
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:30
createDir($path, $visibility=Visibility::PUBLIC_ACCESS)
Create a new directory.
arrayToMetadata(array $metadataArray)
Parses a metadata array into a metadata object.
Create styles array
The data for the language used.
const DIRECTORY
The subject is a directory.
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