|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rico\Slib; |
|
6
|
|
|
|
|
7
|
|
|
abstract class FilesystemUtils |
|
8
|
|
|
{ |
|
9
|
|
|
const LIST_DIRECTORY_FILE_ONLY = 1; |
|
10
|
|
|
const LIST_DIRECTORY_DIR_ONLY = 2; |
|
11
|
|
|
const LIST_DIRECTORY_BOTH = 3; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Creates the completer $path with all missing intermediates directories. |
|
15
|
|
|
* |
|
16
|
|
|
* @param string $path |
|
17
|
|
|
* |
|
18
|
|
|
* @return bool |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function createPath(string $path): bool |
|
21
|
|
|
{ |
|
22
|
|
|
if (file_exists($path)) { |
|
23
|
|
|
return false; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
mkdir($path, 0755, true); |
|
27
|
|
|
|
|
28
|
|
|
return true; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Creates a symbolic $link pointing to $file. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $link |
|
35
|
|
|
* @param string $file |
|
36
|
|
|
* |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function createSymlink(string $link, string $file): bool |
|
40
|
|
|
{ |
|
41
|
|
|
if (!file_exists($link) && !file_exists(dirname($file).'/'.$link)) { |
|
42
|
|
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (file_exists($file)) { |
|
46
|
|
|
unlink($file); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return symlink($link, $file); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Gets filenames and folders names (according to $option) inside a $path. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $path |
|
56
|
|
|
* @param int $option |
|
57
|
|
|
* |
|
58
|
|
|
* @return string[]|null |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function listDirectory(string $path, int $option = self::LIST_DIRECTORY_BOTH): ?array |
|
61
|
|
|
{ |
|
62
|
|
|
if (!file_exists($path) || !is_dir($path)) { |
|
63
|
|
|
return null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$aResult = []; |
|
67
|
|
|
|
|
68
|
|
|
$resourceDir = opendir($path); |
|
69
|
|
|
|
|
70
|
|
|
while (false !== ($strFile = readdir($resourceDir))) { |
|
|
|
|
|
|
71
|
|
|
if (in_array($strFile, ['.', '..'])) { |
|
72
|
|
|
continue; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$strCompleteFile = $path.'/'.$strFile; |
|
76
|
|
|
switch ($option) { |
|
77
|
|
|
case self::LIST_DIRECTORY_FILE_ONLY: |
|
78
|
|
|
if (!is_dir($strCompleteFile)) { |
|
79
|
|
|
$aResult[] = $strFile; |
|
80
|
|
|
} |
|
81
|
|
|
break; |
|
82
|
|
|
case self::LIST_DIRECTORY_DIR_ONLY: |
|
83
|
|
|
if (is_dir($strCompleteFile)) { |
|
84
|
|
|
$aResult[] = $strFile; |
|
85
|
|
|
} |
|
86
|
|
|
break; |
|
87
|
|
|
case self::LIST_DIRECTORY_BOTH: |
|
88
|
|
|
$aResult[] = $strFile; |
|
89
|
|
|
break; |
|
90
|
|
|
default: |
|
91
|
|
|
return null; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
closedir($resourceDir); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
return $aResult; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|