Skip to content
This repository was archived by the owner on Jun 18, 2025. It is now read-only.

Commit 3caf430

Browse files
authored
feat: added more detailed descriptions to the methods (#47)
1 parent 129ed6c commit 3caf430

File tree

2 files changed

+213
-6
lines changed

2 files changed

+213
-6
lines changed

src/LocalFilesystem.php

Lines changed: 189 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
use const DIRECTORY_SEPARATOR;
3232

3333
/**
34+
* A class for working with the local file system.
35+
*
3436
* @author Maksim Spirkov <[email protected]>
3537
*
3638
* @phpstan-type WriteOptionalParams array{directoryPermissions?: int, filePermissions?: int}
@@ -51,7 +53,7 @@ class LocalFilesystem
5153
* directory?: int,
5254
* file?: int
5355
* }
54-
* } $optionalParams
56+
* } $optionalParams Optional parameters. *Note: permissions MUST be set in octal mode*.
5557
*/
5658
public function __construct(string $location, array $optionalParams = [])
5759
{
@@ -61,43 +63,103 @@ public function __construct(string $location, array $optionalParams = [])
6163
}
6264

6365
/**
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+
*
6477
* @param string $path relative path
78+
*
79+
* @return string full path
6580
*/
6681
public function prepareFullPath(string $path): string
6782
{
6883
return LocalFilesystemHelper::normalizePath($this->location . DIRECTORY_SEPARATOR . $path);
6984
}
7085

7186
/**
87+
* Checking the existence of a file.
88+
*
89+
* Example:
90+
* ```php
91+
* $result = $filesystem->fileExists('file.txt');
92+
* ```
93+
*
7294
* @param string $path relative path
95+
*
96+
* @see [is_file](https://www.php.net/manual/en/function.is-file.php) for more information
7397
*/
7498
public function fileExists(string $path): bool
7599
{
76100
return is_file($this->prepareFullPath($path));
77101
}
78102

79103
/**
104+
* Checking the existence of a directory.
105+
*
106+
* Example:
107+
* ```php
108+
* $result = $filesystem->directoryExists('directory');
109+
* ```
110+
*
80111
* @param string $path relative path
112+
*
113+
* @see [is_dir](https://www.php.net/manual/en/function.is-dir.php) for more information
81114
*/
82115
public function directoryExists(string $path): bool
83116
{
84117
return is_dir($this->prepareFullPath($path));
85118
}
86119

87120
/**
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+
*
88132
* @param string $path relative path
133+
* @param int $permissions octal mode permissions
89134
*
90135
* @throws LocalFilesystemException
136+
*
137+
* @see [chmod](https://www.php.net/manual/en/function.chmod.php) for more information
91138
*/
92139
public function setPermissions(string $path, int $permissions): void
93140
{
94141
$this->setPermissionsByFullPath($this->prepareFullPath($path), $permissions);
95142
}
96143

97144
/**
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+
*
98156
* @param string $path relative path
99157
*
100158
* @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
101163
*/
102164
public function getPermissions(string $path): int
103165
{
@@ -111,12 +173,21 @@ public function getPermissions(string $path): int
111173
}
112174

113175
/**
176+
* Pathnames listing.
177+
*
178+
* Example:
179+
* ```php
180+
* $result = filesystem->listPathnames('*');
181+
* ```
182+
*
114183
* @param string $pattern relative pattern
115184
*
116185
* @throws LocalFilesystemException
117186
*
118187
* @return string[] an array containing the matched files/directories, an empty array
119188
* if no file matched
189+
*
190+
* @see [glob](https://www.php.net/manual/en/function.glob.php) for more information
120191
*/
121192
public function listPathnames(string $pattern, int $flags = 0): array
122193
{
@@ -132,11 +203,30 @@ public function listPathnames(string $pattern, int $flags = 0): array
132203
/**
133204
* Writing content to a file with the creation of a file and a directory for it.
134205
*
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+
*
135222
* @param string $path relative path
136223
* @param mixed $content
137224
* @param WriteOptionalParams|array{flags?: int} $optionalParams
138225
*
139226
* @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`
140230
*/
141231
public function writeToFile(string $path, $content, array $optionalParams = []): void
142232
{
@@ -152,11 +242,20 @@ public function writeToFile(string $path, $content, array $optionalParams = []):
152242
}
153243

154244
/**
245+
* Reading a file.
246+
*
247+
* Example:
248+
* ```php
249+
* $result = $filesystem->readFile('file.txt');
250+
* ```
251+
*
155252
* @param string $path relative path
156253
*
157254
* @throws LocalFilesystemException
158255
*
159256
* @return string file content
257+
*
258+
* @see [file_get_contents](https://www.php.net/manual/en/function.file-get-contents.php) for more information
160259
*/
161260
public function readFile(string $path): string
162261
{
@@ -170,11 +269,20 @@ public function readFile(string $path): string
170269
}
171270

172271
/**
272+
* Streaming file reading.
273+
*
274+
* Example:
275+
* ```php
276+
* $result = $filesystem->readFileAsStream('file.txt');
277+
* ```
278+
*
173279
* @param string $path relative path
174280
*
175281
* @throws LocalFilesystemException
176282
*
177283
* @return resource stream for reading file content
284+
*
285+
* @see [fopen](https://www.php.net/manual/en/function.fopen.php) for more information
178286
*/
179287
public function readFileAsStream(string $path)
180288
{
@@ -188,11 +296,20 @@ public function readFileAsStream(string $path)
188296
}
189297

190298
/**
299+
* Getting the file size.
300+
*
301+
* Example:
302+
* ```php
303+
* $result = $filesystem->getFileSize('file.txt');
304+
* ```
305+
*
191306
* @param string $path relative path
192307
*
193308
* @throws LocalFilesystemException
194309
*
195310
* @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
196313
*/
197314
public function getFileSize(string $path): int
198315
{
@@ -206,11 +323,20 @@ public function getFileSize(string $path): int
206323
}
207324

208325
/**
326+
* Detect MIME Content-type for a file.
327+
*
328+
* Example:
329+
* ```php
330+
* $result = $filesystem->getFileMimeType('file.txt');
331+
* ```
332+
*
209333
* @param string $path relative path
210334
*
211335
* @throws LocalFilesystemException
212336
*
213337
* @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
214340
*/
215341
public function getFileMimeType(string $path): string
216342
{
@@ -224,12 +350,21 @@ public function getFileMimeType(string $path): string
224350
}
225351

226352
/**
353+
* Getting the file modification time.
354+
*
355+
* Example:
356+
* ```php
357+
* $result = $filesystem->getFileLastModifiedTime('file.txt');
358+
* ```
359+
*
227360
* @param string $path relative path
228361
*
229362
* @throws LocalFilesystemException
230363
*
231364
* @return int the time the file was last modified. The time is returned as a Unix timestamp,
232365
* which is suitable for the date function.
366+
*
367+
* @see [filemtime](https://www.php.net/manual/en/function.filemtime.php) for more information
233368
*/
234369
public function getFileLastModifiedTime(string $path): int
235370
{
@@ -243,6 +378,13 @@ public function getFileLastModifiedTime(string $path): int
243378
}
244379

245380
/**
381+
* Deleting a file.
382+
*
383+
* Example:
384+
* ```php
385+
* $filesystem->deleteFile('file.txt');
386+
* ```
387+
*
246388
* @param string $path relative path
247389
*
248390
* @throws LocalFilesystemException
@@ -255,8 +397,21 @@ public function deleteFile(string $path): void
255397
/**
256398
* Copying a file with creating a directory for it.
257399
*
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
260415
* @param WriteOptionalParams $optionalParams
261416
*
262417
* @throws LocalFilesystemException
@@ -277,8 +432,21 @@ public function copyFile(string $oldPath, string $newPath, array $optionalParams
277432
/**
278433
* Moving a file with creating a directory for it.
279434
*
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
282450
* @param WriteOptionalParams $optionalParams
283451
*
284452
* @throws LocalFilesystemException
@@ -297,7 +465,17 @@ public function moveFile(string $oldPath, string $newPath, array $optionalParams
297465
}
298466

299467
/**
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+
* ```
301479
*
302480
* @param string $path relative path
303481
*
@@ -314,6 +492,11 @@ public function createDirectory(string $path, ?int $permissions = null): void
314492
/**
315493
* Recursively deleting a directory along with the contained files and directories.
316494
*
495+
* Example:
496+
* ```php
497+
* $filesystem->deleteDirectory('directory');
498+
* ```
499+
*
317500
* @param string $path relative path
318501
*
319502
* @throws LocalFilesystemException

0 commit comments

Comments
 (0)