]> BookStack Code Mirror - system-cli/blob - src/Services/Directories.php
Bumped version
[system-cli] / src / Services / Directories.php
1 <?php declare(strict_types=1);
2
3 namespace Cli\Services;
4 use FilesystemIterator;
5 use RecursiveDirectoryIterator;
6 use RecursiveIteratorIterator;
7 use SplFileInfo;
8
9 class Directories
10 {
11
12     public static function move(string $src, string $target): void
13     {
14         static::copy($src, $target);
15         static::delete($src);
16     }
17
18     public static function copy(string $src, string $target): void
19     {
20         if (!file_exists($target)) {
21             mkdir($target);
22         }
23
24         /** @var RecursiveDirectoryIterator $files */
25         $files = new RecursiveIteratorIterator(
26             new RecursiveDirectoryIterator($src, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS),
27             RecursiveIteratorIterator::SELF_FIRST
28         );
29
30         /** @var SplFileInfo $fileinfo */
31         foreach ($files as $fileinfo) {
32             $srcPath = $fileinfo->getRealPath();
33             $subPath = $files->getSubPathName();
34             $destPath = Paths::join($target, $subPath);
35             $result = true;
36             if ($fileinfo->isDir() && !file_exists($destPath)) {
37                 $result = mkdir($destPath);
38             } else if ($fileinfo->isFile()) {
39                 $result = copy($srcPath, $destPath);
40             }
41
42             if ($result === false) {
43                 throw new \Exception("Failed to copy file or directory from {$srcPath} to {$destPath}");
44             }
45         }
46     }
47
48     /**
49      * Delete the contents of the given directory.
50      * Will not delete symlinked folders to ensure that symlinks remain consistent,
51      * but will aim to delete contents of symlinked folders.
52      */
53     public static function delete(string $dir): void
54     {
55         $files = new RecursiveIteratorIterator(
56             new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS),
57             RecursiveIteratorIterator::CHILD_FIRST
58         );
59
60         $links = '::';
61
62         /** @var SplFileInfo $fileinfo */
63         foreach ($files as $fileinfo) {
64             $path = $fileinfo->getRealPath();
65             $result = true;
66             if ($fileinfo->isDir()) {
67                 if ($fileinfo->isLink()) {
68                     $links .= $fileinfo->getPath() . '::';
69                 } else if (!str_contains($links, '::' . $path)) {
70                     $result = rmdir($path);
71                 }
72             } else if ($fileinfo->isFile()) {
73                 $result = unlink($path);
74             }
75
76             if ($result === false) {
77                 throw new \Exception("Failed to delete file or directory at {$path}");
78             }
79         }
80
81         if ($links === '::') {
82             rmdir($dir);
83         }
84     }
85 }