use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
+use SplFileInfo;
class Directories
{
public static function copy(string $src, string $target): void
{
- mkdir($target);
+ if (!file_exists($target)) {
+ mkdir($target);
+ }
/** @var RecursiveDirectoryIterator $files */
$files = new RecursiveIteratorIterator(
RecursiveIteratorIterator::SELF_FIRST
);
- /** @var \SplFileInfo $fileinfo */
+ /** @var SplFileInfo $fileinfo */
foreach ($files as $fileinfo) {
$srcPath = $fileinfo->getRealPath();
$subPath = $files->getSubPathName();
$destPath = Paths::join($target, $subPath);
- if ($fileinfo->isDir()) {
+ $result = true;
+ if ($fileinfo->isDir() && !file_exists($destPath)) {
+ echo $destPath . "\n";
$result = mkdir($destPath);
- } else {
+ } else if ($fileinfo->isFile()) {
$result = copy($srcPath, $destPath);
}
}
}
+ /**
+ * Delete the contents of the given directory.
+ * Will not delete symlinked folders to ensure that symlinks remain consistent,
+ * but will aim to delete contents of symlinked folders.
+ */
public static function delete(string $dir): void
{
$files = new RecursiveIteratorIterator(
RecursiveIteratorIterator::CHILD_FIRST
);
+ $links = '::';
+
+ /** @var SplFileInfo $fileinfo */
foreach ($files as $fileinfo) {
$path = $fileinfo->getRealPath();
+ $result = true;
if ($fileinfo->isDir()) {
- $result = rmdir($path);
- } else {
+ if ($fileinfo->isLink()) {
+ $links .= $fileinfo->getPath() . '::';
+ } else if (!str_contains($links, '::' . $path)) {
+ $result = rmdir($path);
+ }
+ } else if ($fileinfo->isFile()) {
$result = unlink($path);
}
}
}
- rmdir($dir);
+ if ($links === '::') {
+ rmdir($dir);
+ }
}
}
\ No newline at end of file