1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PierInfor\GeoLite; |
4
|
|
|
|
5
|
|
|
use PierInfor\GeoLite\Downloader; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* FileManager class to manage files |
9
|
|
|
*/ |
10
|
|
|
class FileManager implements Interfaces\FileManagerInterface |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Downloader instance |
15
|
|
|
* |
16
|
|
|
* @var Downloader |
17
|
|
|
*/ |
18
|
|
|
private $downloader; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Instanciate |
22
|
|
|
*/ |
23
|
15 |
|
public function __construct() |
24
|
|
|
{ |
25
|
15 |
|
$this->downloader = new Downloader(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* returns downloader instance |
30
|
|
|
* |
31
|
|
|
* @return Downloader |
32
|
|
|
*/ |
33
|
1 |
|
public function getDownloader(): Downloader |
34
|
|
|
{ |
35
|
1 |
|
return $this->downloader; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* download a file for a given url and filename |
40
|
|
|
* |
41
|
|
|
* @param string $url |
42
|
|
|
* @param string $toFilename |
43
|
|
|
* @return FileManager |
44
|
|
|
*/ |
45
|
1 |
|
public function download(string $url, string $toFilename): FileManager |
46
|
|
|
{ |
47
|
1 |
|
$this->downloader->download($url, $toFilename); |
48
|
1 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* uncompress a tar gz file to a tar archive and return true if ok |
53
|
|
|
* |
54
|
|
|
* @param string $tgzFilename |
55
|
|
|
* @return boolean |
56
|
|
|
* @throws \Exception |
57
|
|
|
*/ |
58
|
4 |
|
public function ungz(string $tgzFilename): bool |
59
|
|
|
{ |
60
|
4 |
|
$result = false; |
61
|
4 |
|
if (empty($tgzFilename) || !file_exists($tgzFilename)) { |
62
|
1 |
|
return $result; |
63
|
|
|
} |
64
|
|
|
try { |
65
|
3 |
|
(new \PharData($tgzFilename))->decompress(); |
66
|
1 |
|
} catch (\Exception $e) { |
67
|
1 |
|
if ($e instanceof \UnexpectedValueException) { |
68
|
1 |
|
throw new \Exception('Malformed gzip'); |
69
|
|
|
} |
70
|
|
|
} |
71
|
2 |
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* extract a tar archive to a target folder |
76
|
|
|
* |
77
|
|
|
* @param string $tarFilename |
78
|
|
|
* @param string $targetFolder |
79
|
|
|
* @return boolean |
80
|
|
|
*/ |
81
|
1 |
|
public function untar(string $tarFilename, string $targetFolder): bool |
82
|
|
|
{ |
83
|
1 |
|
if (empty($tarFilename) |
84
|
1 |
|
|| empty($targetFolder) |
85
|
1 |
|
|| !file_exists($tarFilename) |
86
|
|
|
) { |
87
|
1 |
|
return false; |
88
|
|
|
} |
89
|
1 |
|
(new \PharData($tarFilename))->extractTo($targetFolder); |
90
|
1 |
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* copy file from to and returns operation status |
95
|
|
|
* |
96
|
|
|
* @param string $from |
97
|
|
|
* @param string $to |
98
|
|
|
* @return boolean |
99
|
|
|
*/ |
100
|
5 |
|
public function copyFile(string $from, string $to): bool |
101
|
|
|
{ |
102
|
5 |
|
return @copy($from, $to); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* returns folders list for a given path |
107
|
|
|
* |
108
|
|
|
* @param string $path |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
15 |
|
public function folderList(string $path): array |
112
|
|
|
{ |
113
|
15 |
|
return @glob($path . '*', GLOB_ONLYDIR); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* returns file list for a given path |
118
|
|
|
* |
119
|
|
|
* @param string $path |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
1 |
|
public function fileList(string $path): array |
123
|
|
|
{ |
124
|
1 |
|
return @glob(rtrim($path, '/') . '/*.{*}', GLOB_BRACE); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* returns Y-m-d modification file date for a $path |
129
|
|
|
* |
130
|
|
|
* @param string $path |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
2 |
|
public function fileDate(string $path): string |
134
|
|
|
{ |
135
|
2 |
|
clearstatcache(); |
136
|
2 |
|
if (!file_exists($path)) { |
137
|
1 |
|
return '1971-01-14'; |
138
|
|
|
} |
139
|
2 |
|
return date('Y-m-d', filemtime($path)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* returns true if modified file date is today |
144
|
|
|
* |
145
|
|
|
* @param string $path |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
1 |
|
public function isFileDateToday(string $path): bool |
149
|
|
|
{ |
150
|
1 |
|
return (date('Y-m-d') == $this->fileDate($path)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* unlink multiples files from a path and a mask |
155
|
|
|
* |
156
|
|
|
* @param string $mask |
157
|
|
|
* @return boolean |
158
|
|
|
*/ |
159
|
15 |
|
public function unlinkFiles(string $mask): bool |
160
|
|
|
{ |
161
|
15 |
|
$toDelete = @glob($mask); |
162
|
15 |
|
if (false === $toDelete) { |
163
|
|
|
return $toDelete; |
164
|
|
|
} |
165
|
15 |
|
$toDeleteCount = count($toDelete); |
166
|
15 |
|
$opStatus = []; |
167
|
15 |
|
for ($c = 0; $c < $toDeleteCount; $c++) { |
168
|
4 |
|
$opStatus[] = (int) @unlink($toDelete[$c]); |
169
|
|
|
} |
170
|
15 |
|
$status = array_reduce($opStatus, function ($car, $ite) { |
171
|
4 |
|
return $car += $ite; |
172
|
15 |
|
}); |
173
|
15 |
|
return ($status > 0); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* unlink multiples folders even if not empty from a path |
178
|
|
|
* |
179
|
|
|
* @param string $mask |
180
|
|
|
* @return boolean |
181
|
|
|
*/ |
182
|
15 |
|
public function unlinkFolders(string $path): bool |
183
|
|
|
{ |
184
|
15 |
|
$toDelete = $this->folderList($path); |
185
|
15 |
|
$toDeleteCount = count($toDelete); |
186
|
15 |
|
$opStatus = []; |
187
|
15 |
|
for ($c = 0; $c < $toDeleteCount; $c++) { |
188
|
2 |
|
$opStatus[] = (int) $this->deleteFolder($toDelete[$c]); |
189
|
|
|
} |
190
|
15 |
|
$status = array_reduce($opStatus, function ($car, $ite) { |
191
|
2 |
|
return $car += $ite; |
192
|
15 |
|
}); |
193
|
15 |
|
return ($status > 0); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* delete a not empty folder |
198
|
|
|
* |
199
|
|
|
* @param string $path |
200
|
|
|
* @return boolean |
201
|
|
|
*/ |
202
|
3 |
|
public function deleteFolder(string $path): bool |
203
|
|
|
{ |
204
|
3 |
|
$opStatus = []; |
205
|
3 |
|
$fsItNoDots = \FilesystemIterator::SKIP_DOTS; |
206
|
3 |
|
$rdiT = new \RecursiveDirectoryIterator($path, $fsItNoDots); |
207
|
3 |
|
$riiFirstChild = \RecursiveIteratorIterator::CHILD_FIRST; |
208
|
3 |
|
$rii = new \RecursiveIteratorIterator($rdiT, $riiFirstChild); |
209
|
3 |
|
foreach ($rii as $file) { |
210
|
3 |
|
if ($file->isDir()) { |
211
|
2 |
|
$opStatus[] = (int) @rmdir($file->getPathname()); |
212
|
|
|
} else { |
213
|
3 |
|
$opStatus[] = (int) @unlink($file->getPathname()); |
214
|
|
|
} |
215
|
|
|
} |
216
|
3 |
|
$opStatus[] = (int) @rmdir($path); |
217
|
3 |
|
$status = array_reduce($opStatus, function ($car, $ite) { |
218
|
3 |
|
return $car += $ite; |
219
|
3 |
|
}); |
220
|
3 |
|
return $status; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|