1 | <?php |
||
2 | |||
3 | namespace Startwind\Inventorio\Exec; |
||
4 | |||
5 | class File |
||
6 | { |
||
7 | private static ?File $instance = null; |
||
8 | |||
9 | static public function getInstance(): File |
||
10 | { |
||
11 | if (self::$instance === null) { |
||
12 | self::$instance = new self(); |
||
13 | } |
||
14 | |||
15 | return self::$instance; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
16 | } |
||
17 | |||
18 | public function isDir($directory): bool |
||
19 | { |
||
20 | $cmd = '[ -d ' . escapeshellarg($directory) . ' ] && echo "1" || echo "0"'; |
||
21 | return trim(Runner::getInstance()->run($cmd)->getOutput()) === '1'; |
||
22 | } |
||
23 | |||
24 | public function isFile($filename): bool |
||
25 | { |
||
26 | $cmd = '[ -f ' . escapeshellarg($filename) . ' ] && echo "1" || echo "0"'; |
||
27 | return trim(Runner::getInstance()->run($cmd)->getOutput()) === '1'; |
||
28 | } |
||
29 | |||
30 | public function isLink($filename): bool |
||
31 | { |
||
32 | $cmd = '[ -L ' . escapeshellarg($filename) . ' ] && echo "1" || echo "0"'; |
||
33 | return trim(Runner::getInstance()->run($cmd)->getOutput()) === '1'; |
||
34 | } |
||
35 | |||
36 | public function isReadable($filename): bool |
||
37 | { |
||
38 | $cmd = '[ -r ' . escapeshellarg($filename) . ' ] && echo "1" || echo "0"'; |
||
39 | return trim(Runner::getInstance()->run($cmd)->getOutput()) === '1'; |
||
40 | } |
||
41 | |||
42 | public function getFilesize($filename): int |
||
43 | { |
||
44 | $escaped = escapeshellarg($filename); |
||
45 | $platform = System::getInstance()->getPlatform(); |
||
46 | |||
47 | if ($platform === 'darwin' || str_contains($platform, 'bsd')) { |
||
48 | $cmd = 'stat -f%z ' . $escaped; |
||
49 | } else { |
||
50 | $cmd = 'stat -c%s ' . $escaped; |
||
51 | } |
||
52 | |||
53 | $output =trim(Runner::getInstance()->run($cmd)->getOutput()); |
||
54 | return is_numeric($output) ? (int)$output : 0; |
||
55 | } |
||
56 | |||
57 | public function fileExists(string $path): bool |
||
58 | { |
||
59 | $cmd = '[ -e ' . escapeshellarg($path) . ' ] && echo "1" || echo "0"'; |
||
60 | return trim(Runner::getInstance()->run($cmd)->getOutput()) === '1'; |
||
61 | } |
||
62 | |||
63 | public function getContents(string $path, bool $asArray = false): string|false|array |
||
64 | { |
||
65 | $cmd = 'cat ' . escapeshellarg($path) . ' 2>/dev/null'; |
||
66 | $output = Runner::getInstance()->run($cmd)->getOutput(); |
||
67 | |||
68 | if ($output === null) { |
||
0 ignored issues
–
show
|
|||
69 | return false; |
||
70 | } |
||
71 | |||
72 | return $asArray ? explode("\n", rtrim($output)) : $output; |
||
73 | } |
||
74 | |||
75 | public function realPath(string $path): string |
||
76 | { |
||
77 | return trim(Runner::getInstance()->run("realpath " . escapeshellarg(trim($path)))->getOutput()); |
||
78 | } |
||
79 | |||
80 | public function scanDir($path): array |
||
81 | { |
||
82 | $cmd = 'ls -1A ' . escapeshellarg($path) . ' 2>/dev/null'; |
||
83 | $output = Runner::getInstance()->run($cmd)->getOutput(); |
||
84 | return $output !== null ? explode("\n", trim($output)) : []; |
||
0 ignored issues
–
show
|
|||
85 | } |
||
86 | } |
||
87 |