forked from azjezz/psl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriteHandle.php
66 lines (53 loc) · 2.12 KB
/
WriteHandle.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
<?php
declare(strict_types=1);
namespace Psl\File;
use Psl\Filesystem;
use Psl\IO;
final class WriteHandle extends Internal\AbstractHandleWrapper implements WriteHandleInterface
{
use IO\WriteHandleConvenienceMethodsTrait;
private WriteHandleInterface $writeHandle;
/**
* @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.
*/
public function __construct(string $file, WriteMode $write_mode = WriteMode::OPEN_OR_CREATE)
{
$is_file = Filesystem\is_file($file);
if (!$is_file && Filesystem\exists($file)) {
throw Exception\NotFileException::for($file);
}
$open_or_create = $write_mode === WriteMode::OPEN_OR_CREATE || $write_mode === WriteMode::TRUNCATE;
$must_create = $write_mode === WriteMode::MUST_CREATE;
if ($must_create && $is_file) {
throw Exception\AlreadyCreatedException::for($file);
}
$creating = $open_or_create || $must_create;
if (!$creating && !$is_file) {
throw Exception\NotFoundException::for($file);
}
if ((!$creating || ($open_or_create && $is_file)) && !Filesystem\is_writable($file)) {
throw Exception\NotWritableException::for($file);
}
$this->writeHandle = Internal\open($file, $write_mode->value, read: false, write: true);
parent::__construct($this->writeHandle);
}
/**
* {@inheritDoc}
*/
public function tryWrite(string $bytes): int
{
return $this->writeHandle->tryWrite($bytes);
}
/**
* {@inheritDoc}
*/
public function write(string $bytes, ?float $timeout = null): int
{
return $this->writeHandle->write($bytes, $timeout);
}
}