Test Failed
Push — master ( ac3303...b3b99f )
by 世昌
02:03
created

DirectoryHelper::move()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\application\helper;
3
4
use RecursiveIteratorIterator;
5
use nebula\component\loader\Path;
6
use nebula\component\loader\PathTrait;
7
use nebula\application\helper\FileHelper;
8
9
/**
10
 * 文件夹辅助
11
 */
12
class DirectoryHelper
13
{
14
    /**
15
     * 递归创建目录
16
     *
17
     * @param string $path
18
     * @param integer $mode
19
     * @return boolean
20
     */
21
    public static function makes(string $path, int $mode = 0777):bool
22
    {
23
        if (!is_dir($path)) {
24
            if (!static::makes(dirname($path), $mode)) {
25
                return false;
26
            }
27
            if (!static::make($path, $mode)) {
28
                return false;
29
            }
30
        }
31
        return true;
32
    }
33
34
    /**
35
     * 创建目录
36
     *
37
     * @param string $path
38
     * @param integer $mode
39
     * @return boolean
40
     */
41
    public static function make(string $path, int $mode):bool
42
    {
43
        if (\is_dir(\dirname($path)) && is_writable(dirname($path))) {
44
            $mk = mkdir($path, $mode);
45
            if ($mk) {
46
                chmod($path, $mode);
47
            }
48
            return $mk;
49
        }
50
        return false;
51
    }
52
53
    /**
54
     * 删除空目录
55
     *
56
     * @param string $path
57
     * @return boolean
58
     */
59
    public static function rm(string $path):bool
60
    {
61
        if (!is_writable($path)) {
62
            return false;
63
        }
64
        return rmdir($path);
65
    }
66
67
    /**
68
     * 删除非空目录
69
     *
70
     * @param string $path
71
     * @param string|null $regex
72
     * @return boolean
73
     */
74
    public static function rmdirs(string $path, ?string $regex = null):bool
75
    {
76
        foreach (static::read($path, false, $regex, true) as $subpath) {
77
            if (\is_dir($subpath)) {
78
                static::rmdirs($subpath, $regex);
79
            } elseif (is_file($subpath)) {
80
                FileHelper::delete($subpath);
81
            }
82
        }
83
        static::rm($path);
84
        return true;
85
    }
86
87
88
    /**
89
     * 读目录下文件
90
     *
91
     * @param string $path
92
     * @param boolean $recursive
93
     * @param string|null $regex
94
     * @param boolean $full
95
     * @return \Iterator
96
     */
97
    public static function readFiles(string $path, bool $recursive=false, ?string $regex=null, bool $full=true, int $mode = RecursiveIteratorIterator::LEAVES_ONLY) : \Iterator
98
    {
99
        $parent = Path::format($path);
100
        foreach (static::read($path, $recursive, $regex, false, $mode) as $subpath) {
101
            $path = $full?$subpath:$parent.DIRECTORY_SEPARATOR.$subpath;
102
            if (is_file($path)) {
103
                yield $subpath;
104
            }
105
        }
106
    }
107
108
    /**
109
     * 读目录下文件夹
110
     *
111
     * @param string $path
112
     * @param boolean $recursive
113
     * @param string|null $regex
114
     * @param boolean $full
115
     * @return \Iterator
116
     */
117
    public static function readDirs(string $path, bool $recursive=false, ?string $regex=null, bool $full=false, int $mode = RecursiveIteratorIterator::LEAVES_ONLY): \Iterator
118
    {
119
        $parent = Path::format($path);
120
        foreach (static::read($path, $recursive, $regex, false, $mode) as $subpath) {
121
            $path = $full?$subpath:$parent.DIRECTORY_SEPARATOR.$subpath;
122
            if (is_dir($path)) {
123
                yield $subpath;
124
            }
125
        }
126
    }
127
128
    /**
129
     * 读目录,包括文件,文件夹
130
     *
131
     * @param string $path
132
     * @param boolean $recursive
133
     * @param string|null $regex
134
     * @param boolean $full
135
     * @return \Iterator
136
     */
137
    public static function read(string $path, bool $recursive=false, ?string $regex=null, bool $full=true, int $mode = RecursiveIteratorIterator::LEAVES_ONLY): \Iterator
138
    {
139
        $directory = Path::format($path);
140
        if ($directory && \is_dir($directory)) {
141
            $it = new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS);
142
            if ($regex !== null) {
143
                $it = new \RecursiveRegexIterator($it, $regex);
144
            }
145
            if ($recursive) {
146
                $it = new \RecursiveIteratorIterator($it, $mode);
147
            }
148
            foreach ($it as $key => $item) {
149
                if ($full) {
150
                    yield $key;
151
                } else {
152
                    yield static::cut($key, $directory);
153
                }
154
            }
155
        }
156
    }
157
    
158
    /**
159
     * 截断部分目录
160
     *
161
     * @param string $path
162
     * @param string $basepath
163
     * @return string
164
     */
165
    public static function cut(string $path, string $basepath):string
166
    {
167
        $path =  PathTrait::toAbsolutePath($path);
168
        $basepath = PathTrait::toAbsolutePath($basepath);
169
        if (strpos($path, $basepath.DIRECTORY_SEPARATOR) === 0) {
170
            return substr($path, \strlen($basepath) + 1);
171
        }
172
        return $path;
173
    }
174
175
    /**
176
     * 复制文件
177
     *
178
     * @param string $path
179
     * @param string $toPath
180
     * @param string|null $regex
181
     * @param boolean $move
182
     * @return boolean
183
     */
184
    public static function copy(string $path, string $toPath, ?string $regex=null, bool $move = false):bool
185
    {
186
        $directory = Path::format($path);
187
        static::makes($toPath);
188
        if ($directory && \is_writable($toPath)) {
189
            foreach (static::read($directory, true, $regex, false, RecursiveIteratorIterator::CHILD_FIRST) as $subpath) {
190
                $srcpath = $directory.DIRECTORY_SEPARATOR.$subpath;
191
                $destpath = $toPath.DIRECTORY_SEPARATOR.$subpath;
192
                if (is_dir($srcpath)) {
193
                    static::makes($destpath);
194
                    if ($move) {
195
                        static::rm($srcpath);
196
                    }
197
                } else {
198
                    static::makes(dirname($destpath));
199
                    FileHelper::copy($srcpath, $destpath);
200
                    if ($move) {
201
                        FileHelper::delete($srcpath);
202
                    }
203
                }
204
            }
205
            return true;
206
        }
207
        return false;
208
    }
209
210
211
    /**
212
     * 移动文件
213
     *
214
     * @param string $path
215
     * @param string $toPath
216
     * @param string|null $regex
217
     * @return boolean
218
     */
219
    public static function move(string $path, string $toPath, ?string $regex=null):bool
220
    {
221
        return static::copy($path, $toPath, $regex, true);
222
    }
223
}
224