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