|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\utils\base; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\utils\base\traits\UFileSystemWriter; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* File system utilities |
|
9
|
|
|
* Ubiquity\utils\base$UFileSystem |
|
10
|
|
|
* This class is part of Ubiquity |
|
11
|
|
|
* |
|
12
|
|
|
* @author jcheron <[email protected]> |
|
13
|
|
|
* @version 1.0.4 |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
class UFileSystem { |
|
17
|
|
|
use UFileSystemWriter; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Find recursively pathnames matching a pattern |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $pattern |
|
23
|
|
|
* @param integer $flags |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
57 |
|
public static function glob_recursive($pattern, $flags = 0) { |
|
27
|
57 |
|
$files = \glob ( $pattern, $flags ); |
|
28
|
57 |
|
foreach ( \glob ( \dirname ( $pattern ) . '/*', GLOB_ONLYDIR | GLOB_NOSORT ) as $dir ) { |
|
29
|
55 |
|
$files = \array_merge ( $files, self::glob_recursive ( $dir . '/' . \basename ( $pattern ), $flags ) ); |
|
30
|
|
|
} |
|
31
|
57 |
|
return $files; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Deletes all files from a folder (not in subfolders) |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $folder |
|
38
|
|
|
* @param string $mask |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public static function deleteAllFilesFromFolder($folder, $mask = '*') { |
|
41
|
2 |
|
$files = \glob ( $folder . \DS . $mask ); |
|
42
|
2 |
|
foreach ( $files as $file ) { |
|
43
|
2 |
|
if (\is_file ( $file )) |
|
44
|
2 |
|
\unlink ( $file ); |
|
45
|
|
|
} |
|
46
|
2 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Deletes a file, in safe mode |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $filename |
|
52
|
|
|
* @return boolean |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public static function deleteFile($filename) { |
|
55
|
1 |
|
if (\file_exists ( $filename )) |
|
56
|
1 |
|
return \unlink ( $filename ); |
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Tests the existance and eventually creates a directory |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $dir |
|
64
|
|
|
* @param int $mode |
|
65
|
|
|
* @param boolean $recursive |
|
66
|
|
|
* @return boolean |
|
67
|
|
|
*/ |
|
68
|
58 |
|
public static function safeMkdir($dir, $mode = 0777, $recursive = true) { |
|
69
|
58 |
|
if (! \is_dir ( $dir )) |
|
70
|
14 |
|
return \mkdir ( $dir, $mode, $recursive ); |
|
71
|
53 |
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Cleans a directory path by removing double backslashes or slashes and using DIRECTORY_SEPARATOR |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $path |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
15 |
|
public static function cleanPathname($path) { |
|
81
|
15 |
|
if (UString::isNotNull ( $path )) { |
|
82
|
15 |
|
if (\DS === "/") |
|
83
|
15 |
|
$path = \str_replace ( "\\", \DS, $path ); |
|
84
|
|
|
else |
|
85
|
|
|
$path = \str_replace ( "/", \DS, $path ); |
|
86
|
15 |
|
$path = \str_replace ( \DS . \DS, \DS, $path ); |
|
87
|
15 |
|
if (! UString::endswith ( $path, \DS )) { |
|
88
|
5 |
|
$path = $path . \DS; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
15 |
|
return $path; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Cleans a file path by removing double backslashes or slashes and using DIRECTORY_SEPARATOR |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $path |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
16 |
|
public static function cleanFilePathname($path) { |
|
101
|
16 |
|
if (UString::isNotNull ( $path )) { |
|
102
|
16 |
|
if (\DS === "/") |
|
103
|
16 |
|
$path = \str_replace ( "\\", \DS, $path ); |
|
104
|
|
|
else |
|
105
|
|
|
$path = \str_replace ( "/", \DS, $path ); |
|
106
|
16 |
|
$path = \str_replace ( \DS . \DS, \DS, $path ); |
|
107
|
|
|
} |
|
108
|
16 |
|
return $path; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Try to require a file, in safe mode |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $file |
|
115
|
|
|
* @return boolean |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function tryToRequire($file) { |
|
118
|
|
|
if (\file_exists ( $file )) { |
|
119
|
|
|
require_once ($file); |
|
120
|
|
|
return true; |
|
121
|
|
|
} |
|
122
|
|
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Gets file modification time |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $filename |
|
129
|
|
|
* @return number |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public static function lastModified($filename) { |
|
132
|
1 |
|
return \filemtime ( $filename ); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Reads entire file into a string in safe mode |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $filename |
|
139
|
|
|
* @return string|boolean |
|
140
|
|
|
*/ |
|
141
|
2 |
|
public static function load($filename) { |
|
142
|
2 |
|
if (\file_exists ( $filename )) { |
|
143
|
2 |
|
return \file_get_contents ( $filename ); |
|
144
|
|
|
} |
|
145
|
|
|
return false; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Returns the directory base on ROOT, corresponding to a namespace |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $ns |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
4 |
|
public static function getDirFromNamespace($ns) { |
|
155
|
4 |
|
return \ROOT . \DS . \str_replace ( "\\", \DS, $ns ); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Deletes recursivly a folder and its content |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $dir |
|
162
|
|
|
* @return boolean |
|
163
|
|
|
*/ |
|
164
|
2 |
|
public static function delTree($dir) { |
|
165
|
2 |
|
$files = \array_diff ( scandir ( $dir ), array ('.','..' ) ); |
|
166
|
2 |
|
foreach ( $files as $file ) { |
|
167
|
2 |
|
(\is_dir ( "$dir/$file" )) ? self::delTree ( "$dir/$file" ) : \unlink ( "$dir/$file" ); |
|
168
|
|
|
} |
|
169
|
2 |
|
return \rmdir ( $dir ); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Returns the lines of a file in an array |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $filename |
|
176
|
|
|
* @param boolean $reverse |
|
177
|
|
|
* @param null|int $maxLines |
|
178
|
|
|
* @param callback $lineCallback |
|
179
|
|
|
* @return array |
|
180
|
|
|
*/ |
|
181
|
2 |
|
public static function getLines($filename, $reverse = false, $maxLines = null, $lineCallback = null) { |
|
182
|
2 |
|
if (\file_exists ( $filename )) { |
|
183
|
2 |
|
if ($reverse && isset ( $maxLines )) { |
|
184
|
2 |
|
$result = [ ]; |
|
185
|
2 |
|
$fl = \fopen ( $filename, "r" ); |
|
186
|
2 |
|
for($x_pos = 0, $ln = 0, $lines = [ ]; \fseek ( $fl, $x_pos, SEEK_END ) !== - 1; $x_pos --) { |
|
187
|
2 |
|
$char = \fgetc ( $fl ); |
|
188
|
2 |
|
if ($char === "\n") { |
|
189
|
2 |
|
if (\is_callable ( $lineCallback )) { |
|
190
|
2 |
|
$lineCallback ( $result, $lines [$ln] ); |
|
191
|
|
|
} else { |
|
192
|
|
|
$result [] = $lines [$ln]; |
|
193
|
|
|
} |
|
194
|
2 |
|
if (isset ( $maxLines ) && \sizeof ( $result ) >= $maxLines) { |
|
195
|
1 |
|
\fclose ( $fl ); |
|
196
|
1 |
|
return $result; |
|
197
|
|
|
} |
|
198
|
2 |
|
$ln ++; |
|
199
|
2 |
|
continue; |
|
200
|
|
|
} |
|
201
|
2 |
|
$lines [$ln] = $char . ($lines [$ln] ?? ''); |
|
202
|
|
|
} |
|
203
|
1 |
|
\fclose ( $fl ); |
|
204
|
1 |
|
return $result; |
|
205
|
|
|
} else { |
|
206
|
2 |
|
return self::getLinesByLine ( $filename, $reverse, $maxLines, $lineCallback ); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
return [ ]; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Returns relative path between two sources |
|
214
|
|
|
* |
|
215
|
|
|
* @param $from |
|
216
|
|
|
* @param $to |
|
217
|
|
|
* @param string $separator |
|
218
|
|
|
* @return string |
|
219
|
|
|
*/ |
|
220
|
1 |
|
public static function relativePath($from, $to, $separator = DIRECTORY_SEPARATOR) { |
|
221
|
1 |
|
$from = self::cleanPathname ( $from ); |
|
222
|
1 |
|
$to = self::cleanPathname ( $to ); |
|
223
|
|
|
|
|
224
|
1 |
|
$arFrom = \explode ( $separator, \rtrim ( $from, $separator ) ); |
|
225
|
1 |
|
$arTo = \explode ( $separator, \rtrim ( $to, $separator ) ); |
|
226
|
1 |
|
while ( \count ( $arFrom ) && \count ( $arTo ) && ($arFrom [0] == $arTo [0]) ) { |
|
227
|
1 |
|
\array_shift ( $arFrom ); |
|
228
|
1 |
|
\array_shift ( $arTo ); |
|
229
|
|
|
} |
|
230
|
1 |
|
return str_pad ( "", \count ( $arTo ) * 3, '..' . $separator ) . \implode ( $separator, $arFrom ); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
2 |
|
protected static function getLinesByLine($filename, $reverse, $maxLines, $lineCallback) { |
|
234
|
2 |
|
$result = [ ]; |
|
235
|
2 |
|
$handle = \fopen ( $filename, "r" ); |
|
236
|
2 |
|
if ($handle) { |
|
237
|
2 |
|
while ( ($line = \fgets ( $handle )) !== false ) { |
|
238
|
2 |
|
if (\is_callable ( $lineCallback )) { |
|
239
|
2 |
|
$lineCallback ( $result, $line ); |
|
240
|
|
|
} else { |
|
241
|
|
|
$result [] = $line; |
|
242
|
|
|
} |
|
243
|
2 |
|
if (isset ( $maxLines ) && \sizeof ( $result ) >= $maxLines) { |
|
244
|
1 |
|
\fclose ( $handle ); |
|
245
|
1 |
|
if (is_array ( $result ) && $reverse) { |
|
246
|
|
|
$result = \array_reverse ( $result ); |
|
247
|
|
|
} |
|
248
|
1 |
|
return $result; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
2 |
|
\fclose ( $handle ); |
|
252
|
|
|
} else { |
|
253
|
|
|
// error opening the file. |
|
254
|
|
|
} |
|
255
|
2 |
|
if ($reverse) { |
|
256
|
1 |
|
$result = \array_reverse ( $result ); |
|
257
|
|
|
} |
|
258
|
2 |
|
return $result; |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|