-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathread_directory.php
39 lines (32 loc) · 1.05 KB
/
read_directory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
declare(strict_types=1);
namespace Psl\Filesystem;
use FilesystemIterator;
use Psl\Vec;
/**
* Return a vec of files and directories inside the specified directory.
*
* @param non-empty-string $directory
*
* @throws Exception\NotFoundException If $directory is not found.
* @throws Exception\NotDirectoryException If $directory is not a directory.
* @throws Exception\NotReadableException If $directory is not readable.
*
* @return list<non-empty-string>
*/
function read_directory(string $directory): array
{
if (!namespace\exists($directory)) {
throw Exception\NotFoundException::forDirectory($directory);
}
if (!namespace\is_directory($directory)) {
throw Exception\NotDirectoryException::for($directory);
}
if (!namespace\is_readable($directory)) {
throw Exception\NotReadableException::forDirectory($directory);
}
/** @var list<non-empty-string> */
return Vec\values(
new FilesystemIterator($directory, FilesystemIterator::CURRENT_AS_PATHNAME | FilesystemIterator::SKIP_DOTS),
);
}