1 <?php declare(strict_types=1);
3 namespace Cli\Services;
4 use FilesystemIterator;
5 use RecursiveDirectoryIterator;
6 use RecursiveIteratorIterator;
12 public static function move(string $src, string $target): void
14 static::copy($src, $target);
18 public static function copy(string $src, string $target): void
20 if (!file_exists($target)) {
24 /** @var RecursiveDirectoryIterator $files */
25 $files = new RecursiveIteratorIterator(
26 new RecursiveDirectoryIterator($src, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS),
27 RecursiveIteratorIterator::SELF_FIRST
30 /** @var SplFileInfo $fileinfo */
31 foreach ($files as $fileinfo) {
32 $srcPath = $fileinfo->getRealPath();
33 $subPath = $files->getSubPathName();
34 $destPath = Paths::join($target, $subPath);
36 if ($fileinfo->isDir() && !file_exists($destPath)) {
37 echo $destPath . "\n";
38 $result = mkdir($destPath);
39 } else if ($fileinfo->isFile()) {
40 $result = copy($srcPath, $destPath);
43 if ($result === false) {
44 throw new \Exception("Failed to copy file or directory from {$srcPath} to {$destPath}");
50 * Delete the contents of the given directory.
51 * Will not delete symlinked folders to ensure that symlinks remain consistent,
52 * but will aim to delete contents of symlinked folders.
54 public static function delete(string $dir): void
56 $files = new RecursiveIteratorIterator(
57 new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS),
58 RecursiveIteratorIterator::CHILD_FIRST
63 /** @var SplFileInfo $fileinfo */
64 foreach ($files as $fileinfo) {
65 $path = $fileinfo->getRealPath();
67 if ($fileinfo->isDir()) {
68 if ($fileinfo->isLink()) {
69 $links .= $fileinfo->getPath() . '::';
70 } else if (!str_contains($links, '::' . $path)) {
71 $result = rmdir($path);
73 } else if ($fileinfo->isFile()) {
74 $result = unlink($path);
77 if ($result === false) {
78 throw new \Exception("Failed to delete file or directory at {$path}");
82 if ($links === '::') {