31
31
use const DIRECTORY_SEPARATOR ;
32
32
33
33
/**
34
+ * A class for working with the local file system.
35
+ *
34
36
* @author Maksim Spirkov <[email protected] >
35
37
*
36
38
* @phpstan-type WriteOptionalParams array{directoryPermissions?: int, filePermissions?: int}
@@ -51,7 +53,7 @@ class LocalFilesystem
51
53
* directory?: int,
52
54
* file?: int
53
55
* }
54
- * } $optionalParams
56
+ * } $optionalParams Optional parameters. *Note: permissions MUST be set in octal mode*.
55
57
*/
56
58
public function __construct (string $ location , array $ optionalParams = [])
57
59
{
@@ -61,43 +63,103 @@ public function __construct(string $location, array $optionalParams = [])
61
63
}
62
64
63
65
/**
66
+ * Preparing full path by relative path.
67
+ *
68
+ * Example:
69
+ * ```php
70
+ * // File
71
+ * $result = $filesystem->prepareFullPath('file.txt');
72
+ *
73
+ * // Directory
74
+ * $result = $filesystem->prepareFullPath('directory');
75
+ * ```
76
+ *
64
77
* @param string $path relative path
78
+ *
79
+ * @return string full path
65
80
*/
66
81
public function prepareFullPath (string $ path ): string
67
82
{
68
83
return LocalFilesystemHelper::normalizePath ($ this ->location . DIRECTORY_SEPARATOR . $ path );
69
84
}
70
85
71
86
/**
87
+ * Checking the existence of a file.
88
+ *
89
+ * Example:
90
+ * ```php
91
+ * $result = $filesystem->fileExists('file.txt');
92
+ * ```
93
+ *
72
94
* @param string $path relative path
95
+ *
96
+ * @see [is_file](https://www.php.net/manual/en/function.is-file.php) for more information
73
97
*/
74
98
public function fileExists (string $ path ): bool
75
99
{
76
100
return is_file ($ this ->prepareFullPath ($ path ));
77
101
}
78
102
79
103
/**
104
+ * Checking the existence of a directory.
105
+ *
106
+ * Example:
107
+ * ```php
108
+ * $result = $filesystem->directoryExists('directory');
109
+ * ```
110
+ *
80
111
* @param string $path relative path
112
+ *
113
+ * @see [is_dir](https://www.php.net/manual/en/function.is-dir.php) for more information
81
114
*/
82
115
public function directoryExists (string $ path ): bool
83
116
{
84
117
return is_dir ($ this ->prepareFullPath ($ path ));
85
118
}
86
119
87
120
/**
121
+ * Setting up permissions.
122
+ *
123
+ * Example:
124
+ * ```php
125
+ * // File
126
+ * $filesystem->setPermissions('file.txt', 0644);
127
+ *
128
+ * // Directory
129
+ * $filesystem->setPermissions('directory', 0755);
130
+ * ```
131
+ *
88
132
* @param string $path relative path
133
+ * @param int $permissions octal mode permissions
89
134
*
90
135
* @throws LocalFilesystemException
136
+ *
137
+ * @see [chmod](https://www.php.net/manual/en/function.chmod.php) for more information
91
138
*/
92
139
public function setPermissions (string $ path , int $ permissions ): void
93
140
{
94
141
$ this ->setPermissionsByFullPath ($ this ->prepareFullPath ($ path ), $ permissions );
95
142
}
96
143
97
144
/**
145
+ * Getting permissions.
146
+ *
147
+ * Example:
148
+ * ```php
149
+ * // File
150
+ * $result = $filesystem->getPermissions('file.txt');
151
+ *
152
+ * // Directory
153
+ * $result = $filesystem->getPermissions('directory');
154
+ * ```
155
+ *
98
156
* @param string $path relative path
99
157
*
100
158
* @throws LocalFilesystemException
159
+ *
160
+ * @return int numeric mode permissions (example: `0100644`)
161
+ *
162
+ * @see [fileperms](https://www.php.net/manual/en/function.fileperms.php) for more information
101
163
*/
102
164
public function getPermissions (string $ path ): int
103
165
{
@@ -111,12 +173,21 @@ public function getPermissions(string $path): int
111
173
}
112
174
113
175
/**
176
+ * Pathnames listing.
177
+ *
178
+ * Example:
179
+ * ```php
180
+ * $result = filesystem->listPathnames('*');
181
+ * ```
182
+ *
114
183
* @param string $pattern relative pattern
115
184
*
116
185
* @throws LocalFilesystemException
117
186
*
118
187
* @return string[] an array containing the matched files/directories, an empty array
119
188
* if no file matched
189
+ *
190
+ * @see [glob](https://www.php.net/manual/en/function.glob.php) for more information
120
191
*/
121
192
public function listPathnames (string $ pattern , int $ flags = 0 ): array
122
193
{
@@ -132,11 +203,30 @@ public function listPathnames(string $pattern, int $flags = 0): array
132
203
/**
133
204
* Writing content to a file with the creation of a file and a directory for it.
134
205
*
206
+ * Example:
207
+ * ```php
208
+ * $filesystem->writeToFile('file.txt', 'Test');
209
+ * ```
210
+ *
211
+ * This method also allows you to set permissions for directories and file:
212
+ * ```php
213
+ * $filesystem->writeToFile('file.txt', 'Test', [
214
+ * 'directoryPermissions' => 0777,
215
+ * 'filePermissions' => 0666,
216
+ * ]);
217
+ * ```
218
+ *
219
+ * You can also use this method to write a stream to a file.
220
+ * To do this, simply replace `'Test'` with a stream.
221
+ *
135
222
* @param string $path relative path
136
223
* @param mixed $content
137
224
* @param WriteOptionalParams|array{flags?: int} $optionalParams
138
225
*
139
226
* @throws LocalFilesystemException
227
+ *
228
+ * @see [file_put_contents](https://www.php.net/manual/en/function.file-put-contents.php) for more information
229
+ * about `flags`
140
230
*/
141
231
public function writeToFile (string $ path , $ content , array $ optionalParams = []): void
142
232
{
@@ -152,11 +242,20 @@ public function writeToFile(string $path, $content, array $optionalParams = []):
152
242
}
153
243
154
244
/**
245
+ * Reading a file.
246
+ *
247
+ * Example:
248
+ * ```php
249
+ * $result = $filesystem->readFile('file.txt');
250
+ * ```
251
+ *
155
252
* @param string $path relative path
156
253
*
157
254
* @throws LocalFilesystemException
158
255
*
159
256
* @return string file content
257
+ *
258
+ * @see [file_get_contents](https://www.php.net/manual/en/function.file-get-contents.php) for more information
160
259
*/
161
260
public function readFile (string $ path ): string
162
261
{
@@ -170,11 +269,20 @@ public function readFile(string $path): string
170
269
}
171
270
172
271
/**
272
+ * Streaming file reading.
273
+ *
274
+ * Example:
275
+ * ```php
276
+ * $result = $filesystem->readFileAsStream('file.txt');
277
+ * ```
278
+ *
173
279
* @param string $path relative path
174
280
*
175
281
* @throws LocalFilesystemException
176
282
*
177
283
* @return resource stream for reading file content
284
+ *
285
+ * @see [fopen](https://www.php.net/manual/en/function.fopen.php) for more information
178
286
*/
179
287
public function readFileAsStream (string $ path )
180
288
{
@@ -188,11 +296,20 @@ public function readFileAsStream(string $path)
188
296
}
189
297
190
298
/**
299
+ * Getting the file size.
300
+ *
301
+ * Example:
302
+ * ```php
303
+ * $result = $filesystem->getFileSize('file.txt');
304
+ * ```
305
+ *
191
306
* @param string $path relative path
192
307
*
193
308
* @throws LocalFilesystemException
194
309
*
195
310
* @return int the size of the file in bytes
311
+ *
312
+ * @see [filesize](https://www.php.net/manual/en/function.filesize.php) for more information
196
313
*/
197
314
public function getFileSize (string $ path ): int
198
315
{
@@ -206,11 +323,20 @@ public function getFileSize(string $path): int
206
323
}
207
324
208
325
/**
326
+ * Detect MIME Content-type for a file.
327
+ *
328
+ * Example:
329
+ * ```php
330
+ * $result = $filesystem->getFileMimeType('file.txt');
331
+ * ```
332
+ *
209
333
* @param string $path relative path
210
334
*
211
335
* @throws LocalFilesystemException
212
336
*
213
337
* @return string the content type in MIME format, like text/plain or application/octet-stream.
338
+ *
339
+ * @see [mime_content_type](https://www.php.net/manual/en/function.mime-content-type.php) for more information
214
340
*/
215
341
public function getFileMimeType (string $ path ): string
216
342
{
@@ -224,12 +350,21 @@ public function getFileMimeType(string $path): string
224
350
}
225
351
226
352
/**
353
+ * Getting the file modification time.
354
+ *
355
+ * Example:
356
+ * ```php
357
+ * $result = $filesystem->getFileLastModifiedTime('file.txt');
358
+ * ```
359
+ *
227
360
* @param string $path relative path
228
361
*
229
362
* @throws LocalFilesystemException
230
363
*
231
364
* @return int the time the file was last modified. The time is returned as a Unix timestamp,
232
365
* which is suitable for the date function.
366
+ *
367
+ * @see [filemtime](https://www.php.net/manual/en/function.filemtime.php) for more information
233
368
*/
234
369
public function getFileLastModifiedTime (string $ path ): int
235
370
{
@@ -243,6 +378,13 @@ public function getFileLastModifiedTime(string $path): int
243
378
}
244
379
245
380
/**
381
+ * Deleting a file.
382
+ *
383
+ * Example:
384
+ * ```php
385
+ * $filesystem->deleteFile('file.txt');
386
+ * ```
387
+ *
246
388
* @param string $path relative path
247
389
*
248
390
* @throws LocalFilesystemException
@@ -255,8 +397,21 @@ public function deleteFile(string $path): void
255
397
/**
256
398
* Copying a file with creating a directory for it.
257
399
*
258
- * @param string $oldPath relative oldPath
259
- * @param string $newPath relative newPath
400
+ * Example:
401
+ * ```php
402
+ * $filesystem->copyFile('file.txt', 'directory/file.txt');
403
+ * ```
404
+ *
405
+ * This method also allows you to set permissions for directories and file:
406
+ * ```php
407
+ * $filesystem->copyFile('file.txt', 'directory/file.txt', [
408
+ * 'directoryPermissions' => 0777,
409
+ * 'filePermissions' => 0666,
410
+ * ]);
411
+ * ```
412
+ *
413
+ * @param string $oldPath relative path to the source file
414
+ * @param string $newPath relative destination path
260
415
* @param WriteOptionalParams $optionalParams
261
416
*
262
417
* @throws LocalFilesystemException
@@ -277,8 +432,21 @@ public function copyFile(string $oldPath, string $newPath, array $optionalParams
277
432
/**
278
433
* Moving a file with creating a directory for it.
279
434
*
280
- * @param string $oldPath relative oldPath
281
- * @param string $newPath relative newPath
435
+ * Example:
436
+ * ```php
437
+ * $filesystem->moveFile('file.txt', 'directory/file.txt');
438
+ * ```
439
+ *
440
+ * This method also allows you to set permissions for directories and file:
441
+ * ```php
442
+ * $filesystem->moveFile('file.txt', 'directory/file.txt', [
443
+ * 'directoryPermissions' => 0777,
444
+ * 'filePermissions' => 0666,
445
+ * ]);
446
+ * ```
447
+ *
448
+ * @param string $oldPath old relative path
449
+ * @param string $newPath new relative path
282
450
* @param WriteOptionalParams $optionalParams
283
451
*
284
452
* @throws LocalFilesystemException
@@ -297,7 +465,17 @@ public function moveFile(string $oldPath, string $newPath, array $optionalParams
297
465
}
298
466
299
467
/**
300
- * Recursive creating a directory.
468
+ * Recursively creating a directory.
469
+ *
470
+ * Example:
471
+ * ```php
472
+ * $filesystem->createDirectory('directory');
473
+ * ```
474
+ *
475
+ * It also allows you to set permissions for the created directories:
476
+ * ```php
477
+ * $filesystem->createDirectory('directory', 0777);
478
+ * ```
301
479
*
302
480
* @param string $path relative path
303
481
*
@@ -314,6 +492,11 @@ public function createDirectory(string $path, ?int $permissions = null): void
314
492
/**
315
493
* Recursively deleting a directory along with the contained files and directories.
316
494
*
495
+ * Example:
496
+ * ```php
497
+ * $filesystem->deleteDirectory('directory');
498
+ * ```
499
+ *
317
500
* @param string $path relative path
318
501
*
319
502
* @throws LocalFilesystemException
0 commit comments