-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathWritableBuffer.php
More file actions
74 lines (56 loc) · 1.46 KB
/
WritableBuffer.php
File metadata and controls
74 lines (56 loc) · 1.46 KB
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
<?php declare(strict_types=1);
namespace Amp\ByteStream;
use Amp\DeferredFuture;
use Amp\ForbidCloning;
use Amp\ForbidSerialization;
final class WritableBuffer implements WritableStream
{
use ForbidCloning;
use ForbidSerialization;
private readonly DeferredFuture $deferredFuture;
private string $contents = '';
private bool $closed = false;
public function __construct()
{
$this->deferredFuture = new DeferredFuture;
}
public function write(string $bytes): void
{
if ($this->closed) {
throw new ClosedException("The stream has already been closed");
}
$this->contents .= $bytes;
}
public function end(): void
{
if ($this->closed) {
throw new ClosedException("The stream has already been closed");
}
$this->close();
}
public function isWritable(): bool
{
return !$this->closed;
}
public function buffer(): string
{
return $this->deferredFuture->getFuture()->await();
}
public function close(): void
{
if ($this->closed) {
return;
}
$this->closed = true;
$this->deferredFuture->complete($this->contents);
$this->contents = '';
}
public function isClosed(): bool
{
return $this->closed;
}
public function onClose(\Closure $onClose): void
{
$this->deferredFuture->getFuture()->finally($onClose);
}
}