1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HexMakina\LocalFS; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* FILEPATH: /var/www/dev.engine/krafto-cinergie/lib/LocalFS/FileSystem.php |
7
|
|
|
* |
8
|
|
|
* The FileSystem class provides a set of methods to interact with a directory and subdirectories. |
9
|
|
|
* The root is the starting point of the represented file system. |
10
|
|
|
* It allows to work with relative paths and provides methods to get absolute paths, list directories and files, |
11
|
|
|
* and ensure that a path is writable. |
12
|
|
|
* |
13
|
|
|
* @package HexMakina\LocalFS |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
class FileSystem |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
private $rootPath = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructs a new FileSystem object with the given root path. |
23
|
|
|
* |
24
|
|
|
* @param string $rootPath The starting point of the represented file system. |
25
|
|
|
* @throws \InvalidArgumentException If the root path is invalid. |
26
|
|
|
*/ |
27
|
|
|
function __construct(string $rootPath) |
28
|
|
|
{ |
29
|
|
|
$rootPath = realpath($rootPath); |
30
|
|
|
if (!$rootPath) |
31
|
|
|
throw new \InvalidArgumentException('INVALID_ROOT_PATH'); |
32
|
|
|
|
33
|
|
|
$this->rootPath = $rootPath; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function __toString() |
37
|
|
|
{ |
38
|
|
|
return $this->root(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns the starting point of a filesystem's abstraction, not the machine's filesystem root. |
43
|
|
|
* It can be a project's root, or any directory you may want to work with. |
44
|
|
|
* |
45
|
|
|
* @return string The root path of the file system. |
46
|
|
|
*/ |
47
|
|
|
public function root(): string |
48
|
|
|
{ |
49
|
|
|
return $this->rootPath; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the absolute path for a given relative path. |
54
|
|
|
* |
55
|
|
|
* @param string $relativePath The relative path to get the absolute path for. |
56
|
|
|
* @param bool $checkExistence Whether to check if the file exists or not. Default is false. |
57
|
|
|
* @return string The absolute path for the given relative path. |
58
|
|
|
* @throws \InvalidArgumentException If the relative path is empty or invalid. |
59
|
|
|
*/ |
60
|
|
|
public function absolutePathFor(string $relativePath, bool $checkExistence = false): string |
61
|
|
|
{ |
62
|
|
|
$absolute = sprintf('%s/%s', $this->root(), $relativePath); |
63
|
|
|
|
64
|
|
|
if ($checkExistence) { |
65
|
|
|
$absolute = realpath($absolute); |
66
|
|
|
if (!$absolute) { |
67
|
|
|
throw new \InvalidArgumentException('INVALID_PATH'); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $absolute; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Lists the contents of a directory. |
76
|
|
|
* |
77
|
|
|
* @param string $relativePath The relative path of the directory to list. |
78
|
|
|
* @return array An array of filenames in the directory. |
79
|
|
|
* @throws \InvalidArgumentException If the specified path is not a directory. |
80
|
|
|
*/ |
81
|
|
|
public function list(string $relativePath): array |
82
|
|
|
{ |
83
|
|
|
$absolutePath = $this->absolutePathFor($relativePath); |
84
|
|
|
|
85
|
|
|
if (!is_dir($absolutePath)) { |
86
|
|
|
throw new \InvalidArgumentException('RELATIVE_PATH_NOT_A_DIRECTORY'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return array_diff(scandir($absolutePath), ['.', '..']); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns an array of files in the specified directory. |
94
|
|
|
* |
95
|
|
|
* @param string $relativePath The relative path of the directory to search. |
96
|
|
|
* @return array An array of file names. |
97
|
|
|
*/ |
98
|
|
|
public function files(string $relativePath): array |
99
|
|
|
{ |
100
|
|
|
$absolutePath = $this->absolutePathFor($relativePath); |
101
|
|
|
// Filter the list of files to include only files (not directories). |
102
|
|
|
$files = array_filter($this->list($relativePath), function ($filename) use ($absolutePath) { |
103
|
|
|
return is_file($absolutePath . DIRECTORY_SEPARATOR . $filename); |
104
|
|
|
}); |
105
|
|
|
|
106
|
|
|
return $files; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Returns an array of directories in the specified relative path. |
111
|
|
|
* |
112
|
|
|
* @param string $relativePath The relative path to search for directories. |
113
|
|
|
* @return array An array of directories in the specified relative path. |
114
|
|
|
*/ |
115
|
|
|
public function directories(string $relativePath): array |
116
|
|
|
{ |
117
|
|
|
$absolutePath = $this->absolutePathFor($relativePath); |
118
|
|
|
|
119
|
|
|
// Filter the list of files to include only files (not directories). |
120
|
|
|
$files = array_filter($this->list($relativePath), function ($filename) use ($absolutePath) { |
121
|
|
|
return is_dir($absolutePath . DIRECTORY_SEPARATOR . $filename); |
122
|
|
|
}); |
123
|
|
|
|
124
|
|
|
return $files; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Ensures that the specified relative path is writable. |
129
|
|
|
* |
130
|
|
|
* @param string $relativePath The relative path to ensure is writable. |
131
|
|
|
* @throws \InvalidArgumentException If the path is not inside the root path, the target directory cannot be created, or the target directory is not writable. |
132
|
|
|
* @return bool True if the path is writable, false otherwise. |
133
|
|
|
*/ |
134
|
|
|
|
135
|
|
|
public function ensureWritablePath(string $absoluteDirectoryPath, string $filename=null): bool |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
if (strpos($absoluteDirectoryPath, $this->root()) !== 0) { |
138
|
|
|
throw new \InvalidArgumentException('PATH_NOT_INSIDE_ROOT_PATH'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$relativeDirectoryPath = substr($absoluteDirectoryPath, strlen($this->root()) + 1); |
142
|
|
|
$pathParts = explode('/', $relativeDirectoryPath); |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
$targetDir = $this->root(); |
146
|
|
|
foreach ($pathParts as $i => $part) { |
147
|
|
|
$targetDir .= '/' . $part; |
148
|
|
|
|
149
|
|
|
// Create the folder if it doesn't exist |
150
|
|
|
if (!file_exists($targetDir) && mkdir($targetDir, 0755, true) === false) { |
151
|
|
|
throw new \InvalidArgumentException('UNABLE_TO_CREATE_MISSING_TARGET_DIRECTORY'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (!is_dir($targetDir)) { |
155
|
|
|
throw new \InvalidArgumentException('TARGET_DIRECTORY_NOT_A_DIRECTORY'); |
156
|
|
|
} |
157
|
|
|
if (!is_writable($targetDir)) { |
158
|
|
|
throw new \InvalidArgumentException('TARGET_DIRECTORY_NOT_WRITABLE'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
return true; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function filenames($regex = null): array |
165
|
|
|
{ |
166
|
|
|
if (!file_exists($this->rootPath) && mkdir($this->rootPath) === false) { |
167
|
|
|
return []; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$filenames = self::preg_scandir($this->rootPath, $regex); // ID_SEQUENCENUMBER.ext |
171
|
|
|
if (!is_null($filenames)) { |
172
|
|
|
sort($filenames); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $filenames; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// previous implementation, to update |
179
|
|
|
public function filepathes($regex = null): array |
180
|
|
|
{ |
181
|
|
|
$filenames = $this->filenames($regex); |
182
|
|
|
$filepathes = []; |
183
|
|
|
foreach ($filenames as $filename) { |
184
|
|
|
$filepathes[] = $this->absolutePathFor($filename); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $filepathes; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Resolves a symbolic link to its target path. |
192
|
|
|
* |
193
|
|
|
* @param string $path The path to resolve. |
194
|
|
|
* @throws \Exception If `readlink` fails to resolve the symbolic link. |
195
|
|
|
* @return string The resolved path. |
196
|
|
|
*/ |
197
|
|
|
public static function resolve_symlink($path) |
198
|
|
|
{ |
199
|
|
|
if (is_link($path)) { |
200
|
|
|
if (($path = readlink($path)) === false) { |
201
|
|
|
throw new \Exception('Failed to resolve symbolic link'); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $path; |
206
|
|
|
} |
207
|
|
|
/** |
208
|
|
|
* Copies a file from the source path to the destination path. |
209
|
|
|
* |
210
|
|
|
* @param string $sourcePath The path of the source file. |
211
|
|
|
* @param string $destinationPath The path of the destination file. |
212
|
|
|
* @return bool Returns TRUE on success or FALSE on failure. |
213
|
|
|
*/ |
214
|
|
|
public static function copy($sourcePath, $destinationPath) |
215
|
|
|
{ |
216
|
|
|
if (file_exists($sourcePath) && is_file($sourcePath)) { |
217
|
|
|
$destination = new FilePath($destinationPath); |
218
|
|
|
if (file_exists($destination->dir()) && is_dir($destination->dir())) { |
219
|
|
|
return copy($sourcePath, $destinationPath); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
return false; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Moves a file from the source path to the destination path. |
227
|
|
|
* |
228
|
|
|
* @param string $sourcePath The path of the source file. |
229
|
|
|
* @param string $destinationPath The path of the destination file. |
230
|
|
|
* @return bool Returns TRUE on success or FALSE on failure. |
231
|
|
|
*/ |
232
|
|
|
public static function move($sourcePath, $destinationPath) |
233
|
|
|
{ |
234
|
|
|
if (file_exists($sourcePath) && is_file($sourcePath)) { |
235
|
|
|
$destination = new FilePath($destinationPath); |
236
|
|
|
if (file_exists($destination->dir()) && is_dir($destination->dir())) { |
237
|
|
|
return rename($sourcePath, $destinationPath); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return false; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Creates a directory with the specified path and permissions. |
246
|
|
|
* |
247
|
|
|
* @param string $directoryPath The path of the directory to create. |
248
|
|
|
* @param int $permission The permissions to set for the directory. |
249
|
|
|
* @param bool $recursive Whether to create parent directories if they don't exist. |
250
|
|
|
* @return bool Returns TRUE on success or FALSE on failure. |
251
|
|
|
*/ |
252
|
|
|
public static function makeDirectory($directoryPath, $permission = 0777, $recursive = true): bool |
253
|
|
|
{ |
254
|
|
|
return mkdir($directoryPath, $permission, $recursive); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Removes a file or directory with the specified path. |
259
|
|
|
* |
260
|
|
|
* @param string $sourcePath The path of the file or directory to remove. |
261
|
|
|
* @param bool $followLink Whether to follow symbolic links. |
262
|
|
|
* @return bool Returns TRUE on success or FALSE on failure. |
263
|
|
|
*/ |
264
|
|
|
public static function remove($sourcePath, $followLink = false): bool |
265
|
|
|
{ |
266
|
|
|
$success = false; |
267
|
|
|
if (file_exists($sourcePath)) { |
268
|
|
|
if (is_link($sourcePath) && $followLink === true) { |
269
|
|
|
$success = self::remove(readlink($sourcePath)); |
270
|
|
|
} elseif (is_file($sourcePath)) { |
271
|
|
|
$success = unlink($sourcePath); |
272
|
|
|
} elseif (is_dir($sourcePath)) { |
273
|
|
|
$success = rmdir($sourcePath); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
return $success; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Scans a directory for files and directories that match a regular expression. |
281
|
|
|
* |
282
|
|
|
* @param string $directoryPath The path of the directory to scan. |
283
|
|
|
* @param string|null $regex The regular expression to match against file and directory names. |
284
|
|
|
* @return array|null Returns an array of file and directory names that match the regular expression, or NULL if the directory doesn't exist. |
285
|
|
|
* @throws \Exception If the directory cannot be scanned. |
286
|
|
|
*/ |
287
|
|
|
public static function preg_scandir($directoryPath, $regex = null) |
288
|
|
|
{ |
289
|
|
|
if (!file_exists($directoryPath) || !is_dir($directoryPath)) { |
290
|
|
|
return null; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
if (($fileNames = scandir($directoryPath, SCANDIR_SORT_ASCENDING)) !== false) { |
294
|
|
|
return is_null($regex) ? $fileNames : preg_grep($regex, $fileNames); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
throw new \Exception("directory path '$directoryPath' cannot be scanned"); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.