-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathread_file.php
51 lines (43 loc) · 1.46 KB
/
read_file.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
40
41
42
43
44
45
46
47
48
49
50
51
<?php
declare(strict_types=1);
namespace Psl\Filesystem;
use Psl;
use Psl\Internal;
use Psl\Str;
use function file_get_contents;
/**
* Reads entire file into a string.
*
* @param int $offset The offset where the reading starts.
* @param null|int $length Maximum length of data read. The default is to read
* until end of file is reached.
*
* @throws Psl\Exception\InvariantViolationException If the file specified by
* $file does not exist, or is not readable.
* @throws Exception\RuntimeException If an error
*/
function read_file(string $file, int $offset = 0, ?int $length = null): string
{
Psl\invariant(exists($file), '$file does not exist.');
Psl\invariant(is_file($file), '$file is not a file.');
Psl\invariant(is_readable($file), '$file is not readable.');
if (null === $length) {
[$content, $error] = Internal\box(
static fn() => file_get_contents($file, false, null, $offset)
);
} else {
[$content, $error] = Internal\box(
static fn() => file_get_contents($file, false, null, $offset, $length)
);
}
// @codeCoverageIgnoreStart
if (false === $content || null !== $error) {
throw new Exception\RuntimeException(Str\format(
'Failed to read file "%s": %s.',
$file,
$error ?? 'internal error',
));
}
// @codeCoverageIgnoreEnd
return $content;
}