Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.30% |
21 / 23 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
FileUtils | |
91.30% |
21 / 23 |
|
33.33% |
1 / 3 |
13.11 | |
0.00% |
0 / 1 |
getFiles | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
6 | |||
recursiveCopy | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
5.01 | |||
writeFile | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 |
1 | <?php |
2 | |
3 | namespace San\Crud\Utils; |
4 | |
5 | class FileUtils { |
6 | |
7 | public static function getFiles(string $path, array $filter = []) { |
8 | if (!is_dir($path)) return []; |
9 | |
10 | $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path)); |
11 | |
12 | /** @var \SplFileInfo $file */ |
13 | foreach ($iterator as $file) { |
14 | if (!$file->isFile()) continue; |
15 | if (count($filter) > 0 && !in_array($file->getExtension(), $filter)) continue; |
16 | |
17 | $files[] = $file->getRealPath(); |
18 | } |
19 | |
20 | return $files ?? []; |
21 | } |
22 | |
23 | public static function recursiveCopy($src, $dest, $force = FALSE) { |
24 | $files = self::getFiles($src); |
25 | $count = 0; |
26 | |
27 | foreach ($files as $file) { |
28 | $target = $dest . DIRECTORY_SEPARATOR . str_replace($src, '', $file); |
29 | |
30 | if (file_exists($target) && !$force) { |
31 | continue; |
32 | } else { |
33 | $dir = dirname($target); |
34 | if (!is_dir($dir)) { |
35 | mkdir($dir, 0755, TRUE); |
36 | } |
37 | |
38 | copy($file, $target); |
39 | $count++; |
40 | } |
41 | } |
42 | |
43 | return $count; |
44 | } |
45 | |
46 | public static function writeFile(string $dest, string $str) { |
47 | $dir = dirname($dest); |
48 | if (!is_dir($dir)) { |
49 | mkdir($dir, 0777, TRUE); |
50 | } |
51 | |
52 | return file_put_contents($dest, $str); |
53 | } |
54 | } |