-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathReadWriteHandle.php
118 lines (100 loc) · 3.62 KB
/
ReadWriteHandle.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
namespace Psl\File;
use Psl\DateTime\Duration;
use Psl\Filesystem;
use Psl\IO;
use Psl\Str;
final class ReadWriteHandle extends Internal\AbstractHandleWrapper implements WriteHandleInterface, ReadHandleInterface
{
use IO\ReadHandleConvenienceMethodsTrait;
use IO\WriteHandleConvenienceMethodsTrait;
private ReadHandleInterface&WriteHandleInterface $readWriteHandle;
/**
* @param non-empty-string $file
*
* @throws Exception\NotFileException If $file points to a non-file node on the filesystem.
* @throws Exception\AlreadyCreatedException If $file is already created, and $write_mode is {@see WriteMode::MUST_CREATE}.
* @throws Exception\NotFoundException If $file does not exist, and $write_mode is {@see WriteMode::TRUNCATE} or {@see WriteMode::APPEND}.
* @throws Exception\NotWritableException If $file exists, and is non-writable
* @throws Exception\NotReadableException If $file exists, and is non-readable.
* @throws Exception\RuntimeException If unable to create the $file if it does not exist.
*/
public function __construct(string $file, WriteMode $write_mode = WriteMode::OpenOrCreate)
{
$is_file = Filesystem\is_file($file);
if (!$is_file && Filesystem\exists($file)) {
throw Exception\NotFileException::for($file);
}
$must_create = $write_mode === WriteMode::MustCreate;
if ($must_create && $is_file) {
throw Exception\AlreadyCreatedException::for($file);
}
if ($is_file) {
if (!Filesystem\is_writable($file)) {
throw Exception\NotWritableException::for($file);
}
if (!Filesystem\is_readable($file)) {
throw Exception\NotReadableException::for($file);
}
}
if (!$is_file) {
try {
$directory = Filesystem\create_directory_for_file($file);
if (!Filesystem\is_writable($directory)) {
throw Exception\NotWritableException::for($file);
}
if (!Filesystem\is_readable($directory)) {
throw Exception\NotReadableException::for($file);
}
} catch (Filesystem\Exception\RuntimeException $previous) {
throw new Exception\RuntimeException(
Str\format('Failed to create the directory for file "%s".', $file),
previous: $previous,
);
}
}
$this->readWriteHandle = Internal\open($file, $write_mode->value . 'r+', read: true, write: true);
parent::__construct($this->readWriteHandle);
}
/**
* {@inheritDoc}
*/
#[\Override]
public function reachedEndOfDataSource(): bool
{
return $this->readWriteHandle->reachedEndOfDataSource();
}
/**
* {@inheritDoc}
*/
#[\Override]
public function tryRead(null|int $max_bytes = null): string
{
return $this->readWriteHandle->tryRead($max_bytes);
}
/**
* {@inheritDoc}
*/
#[\Override]
public function read(null|int $max_bytes = null, null|Duration $timeout = null): string
{
return $this->readWriteHandle->read($max_bytes, $timeout);
}
/**
* {@inheritDoc}
*/
#[\Override]
public function tryWrite(string $bytes): int
{
return $this->readWriteHandle->tryWrite($bytes);
}
/**
* {@inheritDoc}
*/
#[\Override]
public function write(string $bytes, null|Duration $timeout = null): int
{
return $this->readWriteHandle->write($bytes, $timeout);
}
}