FileSystemFixes::removeDirOrFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 13
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Api;
4
5
use Sunnysideup\UpgradeToSilverstripe4\Traits\HelperInst;
6
7
class FileSystemFixes
8
{
9
    use HelperInst;
10
11
    public function mkDir(string $dir, string $baseFolder = ''): FileSystemFixes
12
    {
13
        if (! $baseFolder) {
14
            $baseFolder = $this->mu()->getWebRootDirLocation();
0 ignored issues
show
Bug introduced by
The method getWebRootDirLocation() does not exist on Sunnysideup\UpgradeToSilverstripe4\ModuleUpgrader. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

14
            $baseFolder = $this->mu()->/** @scrutinizer ignore-call */ getWebRootDirLocation();
Loading history...
15
        }
16
        $this->mu()->execMe(
17
            $baseFolder,
18
            'mkdir -vp ' . $dir,
19
            'Creating new folder: ' . $this->removeCommonStart($dir, $baseFolder),
20
            false
21
        );
22
        $this->test($dir);
23
24
        return $this;
25
    }
26
27
    public function removeDirOrFile(string $folderName, string $baseFolder = ''): FileSystemFixes
28
    {
29
        if (! $baseFolder) {
30
            $baseFolder = $this->mu()->getWebRootDirLocation();
31
        }
32
        $this->mu()->execMe(
33
            $baseFolder,
34
            'rm ' . $folderName . ' -rf',
35
            'removing ' . $folderName,
36
            false
37
        );
38
39
        return $this;
40
    }
41
42
    public function moveAllInFolder(string $oldDir, string $newDir): FileSystemFixes
43
    {
44
        $findFiles = new FindFiles();
45
        $list = $findFiles
46
            ->setSearchPath($oldDir)
47
            ->setRecursive(false)
48
            ->setFindAllExts(true)
49
            ->getFlatFileArray();
50
        if (is_array($list)) {
51
            $this->moveFoldersOrFilesWithin($oldDir, $newDir, $list);
52
        } else {
53
            $this->mu()->colourPrint($list);
54
        }
55
56
        return $this;
57
    }
58
59
    public function moveFoldersOrFilesWithin(string $oldDir, string $newDir, array $foldersOrFilesWithin): FileSystemFixes
60
    {
61
        if (count($foldersOrFilesWithin)) {
62
            foreach ($foldersOrFilesWithin as $folderOrFileWithin) {
63
                $folderOrFileWithin = basename($folderOrFileWithin);
64
                $oldDir .= '/' . $folderOrFileWithin . '/ ';
65
                $newDir .= '/' . $folderOrFileWithin . '/ ';
66
                $this->moveFolderOrFile($oldDir, $newDir);
67
            }
68
        }
69
70
        return $this;
71
    }
72
73
    public function moveFolderOrFile(string $oldPath, string $newPath, ?bool $isCopy = false): FileSystemFixes
74
    {
75
        $oldPath = trim($oldPath);
76
        $newPath = trim($newPath);
77
        $action = 'mv';
78
        $actionName = 'Moving';
79
        if ($isCopy) {
80
            $action = 'cp';
81
            $actionName = 'Copying';
82
        }
83
        if ($this->test($oldPath, false)) {
84
            $oldParentFolder = dirname($oldPath);
85
            $newParentFolder = dirname($newPath);
86
            $this->mu()->execMe(
87
                $oldParentFolder,
88
                'mkdir -p ' . $newParentFolder,
89
                'First we ensure new parent folder ' . $newParentFolder . ' exists',
90
                false
91
            );
92
            $this->mu()->execMe(
93
                $oldParentFolder,
94
                'if test -e ' . $oldPath . '; then ' . $action . ' -vn ' . $oldPath . ' ' . $newPath . '; fi;',
95
                $actionName . ' ' .
96
                $this->removeCommonStart($oldPath, $newPath) . ' to ' . $this->removeCommonStart($newPath, $oldPath) . '
97
                    if test -e ... True if the FILE exists and is a file, regardless of type (node, directory, socket, etc.).
98
                    -v ... verbose,
99
                    -n ... only if does not exists already.',
100
                false
101
            );
102
            $this->test($newPath);
103
        }
104
105
        return $this;
106
    }
107
108
    public function copyFolderOrFile(string $oldPath, string $newPath): FileSystemFixes
109
    {
110
        return $this->moveFolderOrFile($oldPath, $newPath, true);
111
    }
112
113
    protected function test(string $path, ?bool $showError = true): bool
114
    {
115
        clearstatcache();
116
        if (file_exists(trim($path))) {
117
            return true;
118
        }
119
        if ($showError) {
120
            user_error('Could not create, copy, or find "' . $path . '"', E_USER_NOTICE);
121
        }
122
123
        return false;
124
    }
125
126
    protected function removeCommonStart(string $path, string $other): string
127
    {
128
        $x = 0;
129
        $len = strlen($path);
130
        while ($x < $len && substr($path, 0, $x) === substr($other, 0, $x)) {
131
            $x++;
132
        }
133
134
        return substr($path, $x, $len - $x);
135
    }
136
}
137