Skip to content

Commit 1e7b039

Browse files
authored
Create dir_scan_recursive() (#6)
Co-authored-by: onairmarc <[email protected]>
1 parent 435fc20 commit 1e7b039

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

.git-blame-ignore-revs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ a709fafb67d0f1ee38b072390385ec486416aa3e
33
00a48bf6a894d24c9ed4c82bee6297ed24769e34
44
c0c6bf6fb1c3ad80ff54b7420853ecc9096ee576
55
d637c653da4a72e3b2b1132c1f0a4af3e0135387
6+
de3cddf99b38b4504132eb7449c2b7a20aee5cc3

src/ObjectHelpers/dir_helpers.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ function dir_scan(string $dir): array
2424
return Directory::scan($dir);
2525
}
2626
}
27+
28+
if (!function_exists('dir_scan_recursive')) {
29+
/** @experimental */
30+
function dir_scan_recursive(string $path): array
31+
{
32+
return Directory::scanRecursive($path);
33+
}
34+
}

src/Objects/Directory.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use EncoreDigitalGroup\StdLib\Exceptions\DirectoryNotFoundException;
66
use EncoreDigitalGroup\StdLib\Exceptions\ImproperBooleanReturnedException;
7+
use RecursiveDirectoryIterator;
8+
use RecursiveIteratorIterator;
79

810
/**
911
* @api
@@ -79,4 +81,26 @@ public static function scan(string $dir, array &$results = []): array
7981

8082
return $results;
8183
}
84+
85+
/**
86+
* List all files in a directory recursively
87+
*
88+
* @codeCoverageIgnore
89+
*/
90+
public static function scanRecursive(string $path): array
91+
{
92+
$directoryFiles = new RecursiveIteratorIterator(
93+
new RecursiveDirectoryIterator($path),
94+
RecursiveIteratorIterator::SELF_FIRST
95+
);
96+
97+
$files = [];
98+
foreach ($directoryFiles as $file) {
99+
if ($file->isFile()) {
100+
$files[] = $file->getRealpath();
101+
}
102+
}
103+
104+
return $files;
105+
}
82106
}

0 commit comments

Comments
 (0)