1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Repositories; |
4
|
|
|
|
5
|
|
|
class Filesystem |
6
|
|
|
{ |
7
|
|
|
private string $basePath; |
8
|
|
|
const DS = DIRECTORY_SEPARATOR; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Instância um novo arquivo |
12
|
|
|
* @param string $basePath |
13
|
|
|
*/ |
14
|
|
|
public function __construct($basePath = '') |
15
|
|
|
{ |
16
|
|
|
$this->basePath = BASE_PATH . $basePath . '/'; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Retorna array com arquivos e diretórios |
21
|
|
|
* @param string $path |
22
|
|
|
* @return string[] |
23
|
|
|
*/ |
24
|
|
|
public function children($path = '') |
25
|
|
|
{ |
26
|
|
|
return array_diff((array) scandir($this->basePath . $path), ['..', '.']); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Retorna total de items |
31
|
|
|
* @param string $path |
32
|
|
|
* @return int |
33
|
|
|
*/ |
34
|
|
|
public function count($path) |
35
|
|
|
{ |
36
|
|
|
return count($this->children($path)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Cria o diretório |
41
|
|
|
* @param string $folderPath |
42
|
|
|
* @param int $chmod Permissão (base 8) |
43
|
|
|
*/ |
44
|
|
|
public function create($folderPath, $chmod = 0755) |
45
|
|
|
{ |
46
|
|
|
$path = $this->basePath . $folderPath; |
47
|
|
|
if (!is_dir($path)) { |
48
|
|
|
$mask = umask(0); |
49
|
|
|
mkdir($path, $chmod, true); |
50
|
|
|
umask($mask); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Renomeia o arquivo |
56
|
|
|
* @param string $newName |
57
|
|
|
* @param string $newExtension |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function rename($filePath = null, $newFilePath) |
61
|
|
|
{ |
62
|
|
|
return rename($this->basePath . $filePath, $this->basePath . $newFilePath); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function move($filePath = null, $newFolder) |
66
|
|
|
{ |
67
|
|
|
$currentName = ''; |
68
|
|
|
|
69
|
|
|
return rename($this->basePath . $filePath, $this->basePath . $newFolder . $currentName); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Exclui o arquivo/diretório |
74
|
|
|
* @param string $path Caminho do arquivo/diretório |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function delete($path) |
78
|
|
|
{ |
79
|
|
|
$fullPath = $this->basePath . $path; |
80
|
|
|
if (is_dir($fullPath) && !is_link($fullPath)) { |
81
|
|
|
foreach ($this->children($path) as $child) { |
82
|
|
|
$this->delete("$path/$child"); |
83
|
|
|
} |
84
|
|
|
rmdir($fullPath); |
85
|
|
|
} elseif (is_file($fullPath)) { |
86
|
|
|
unlink($fullPath); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Salva o conteúdo no arquivo |
92
|
|
|
* @param string $content |
93
|
|
|
* @param string $mode |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function write($filePath, $content, $mode = 'w') |
97
|
|
|
{ |
98
|
|
|
$dir = pathinfo($filePath, PATHINFO_DIRNAME); |
99
|
|
|
$file = pathinfo($filePath, PATHINFO_BASENAME); |
100
|
|
|
$return = false; |
101
|
|
|
|
102
|
|
|
if ($dir) { |
103
|
|
|
$this->create($dir, 0777); |
104
|
|
|
$fp = fopen("{$this->basePath}{$dir}/{$file}", $mode); |
105
|
|
|
if (false !== $fp) { |
106
|
|
|
fwrite($fp, $content); |
107
|
|
|
$return = fclose($fp); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Retorna o conteúdo do arquivo |
116
|
|
|
* @return string|false |
117
|
|
|
*/ |
118
|
|
|
public function read($filePath) |
119
|
|
|
{ |
120
|
|
|
$content = false; |
121
|
|
|
if ($this->exists($filePath)) { |
122
|
|
|
$content = file_get_contents($this->basePath . $filePath); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $content; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Retorna TRUE se o arquivo existe |
130
|
|
|
* @param string $filePath |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function exists($filePath) |
134
|
|
|
{ |
135
|
|
|
$filePath = ($this->basePath) . $filePath; |
136
|
|
|
|
137
|
|
|
return is_file($filePath) or is_dir($filePath); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|