|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Compolomus\FSHelper; |
|
6
|
|
|
|
|
7
|
|
|
use CallbackFilterIterator; |
|
8
|
|
|
use Compolomus\FSHelper\Exceptions\PathNotFoundException; |
|
9
|
|
|
use FilesystemIterator; |
|
10
|
|
|
use RecursiveDirectoryIterator; |
|
11
|
|
|
use RecursiveIteratorIterator; |
|
12
|
|
|
|
|
13
|
|
|
class FSHelper |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Return all object with information about all files and folder by provided path |
|
17
|
|
|
* |
|
18
|
|
|
* @param string $directoryPath Absolute or relative path |
|
19
|
|
|
* @throws PathNotFoundException |
|
20
|
|
|
*/ |
|
21
|
8 |
|
public static function getAll(string $directoryPath): RecursiveIteratorIterator |
|
22
|
|
|
{ |
|
23
|
8 |
|
$directoryPathReal = realpath($directoryPath); |
|
24
|
|
|
|
|
25
|
|
|
// Throw an exception if the path does not exist |
|
26
|
8 |
|
if (!$directoryPathReal) { |
|
27
|
1 |
|
throw new PathNotFoundException($directoryPath); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
7 |
|
return new RecursiveIteratorIterator( |
|
31
|
7 |
|
new RecursiveDirectoryIterator( |
|
32
|
7 |
|
$directoryPathReal, |
|
33
|
7 |
|
FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS |
|
34
|
|
|
), |
|
35
|
7 |
|
RecursiveIteratorIterator::CHILD_FIRST |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Return objects of all directories by provided path |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $directoryPath Absolute or relative path |
|
43
|
|
|
* @throws PathNotFoundException |
|
44
|
|
|
*/ |
|
45
|
4 |
|
public static function getDirectories(string $directoryPath): CallbackFilterIterator |
|
46
|
|
|
{ |
|
47
|
4 |
|
$all = self::getAll($directoryPath); |
|
48
|
|
|
|
|
49
|
4 |
|
return new CallbackFilterIterator($all, static fn($iterator) => $iterator->isDir()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Return objects of all files by provided path |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $directoryPath Absolute or relative path |
|
56
|
|
|
* @throws PathNotFoundException |
|
57
|
|
|
*/ |
|
58
|
5 |
|
public static function getFiles(string $directoryPath): CallbackFilterIterator |
|
59
|
|
|
{ |
|
60
|
5 |
|
$all = self::getAll($directoryPath); |
|
61
|
|
|
|
|
62
|
5 |
|
return new CallbackFilterIterator($all, static fn($iterator) => $iterator->isFile()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get total size of all files by provided path (in bytes) |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $directoryPath Absolute or relative path |
|
69
|
|
|
* @throws PathNotFoundException |
|
70
|
|
|
*/ |
|
71
|
2 |
|
public static function getFilesSize(string $directoryPath): int |
|
72
|
|
|
{ |
|
73
|
2 |
|
$size = 0; |
|
74
|
2 |
|
foreach (self::getFiles($directoryPath) as $fileInfo) { |
|
75
|
2 |
|
$size += $fileInfo->getSize(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
return $size; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Return meta information about files and directories |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $directoryPath Absolute or relative path |
|
85
|
|
|
* @return array<string, mixed> |
|
86
|
|
|
* @throws PathNotFoundException |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public static function getInfoDirectory(string $directoryPath): array |
|
89
|
|
|
{ |
|
90
|
|
|
return [ |
|
91
|
1 |
|
'directories' => iterator_count(self::getDirectories($directoryPath)), |
|
92
|
1 |
|
'files' => iterator_count(self::getFiles($directoryPath)), |
|
93
|
1 |
|
'size' => self::getFilesSize($directoryPath), |
|
94
|
|
|
]; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|