1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Api; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Psr\Http\Message\StreamInterface; |
9
|
|
|
|
10
|
|
|
final class ServerSentEventsStream implements StreamInterface, \Stringable |
11
|
|
|
{ |
12
|
|
|
public array $buffer = []; |
13
|
|
|
private bool $eof = false; |
14
|
|
|
|
15
|
|
|
public function __construct( |
16
|
|
|
private Closure $stream, |
17
|
|
|
) { |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function __toString(): string |
21
|
|
|
{ |
22
|
|
|
return ''; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function close(): void |
26
|
|
|
{ |
27
|
|
|
$this->eof = true; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function detach(): void |
31
|
|
|
{ |
32
|
|
|
$this->eof = true; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getSize() |
36
|
|
|
{ |
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function tell() |
41
|
|
|
{ |
42
|
|
|
// TODO: Implement tell() method. |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function eof() |
46
|
|
|
{ |
47
|
|
|
return $this->eof; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function isSeekable(): bool |
51
|
|
|
{ |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function seek($offset, $whence = SEEK_SET) |
56
|
|
|
{ |
57
|
|
|
throw new \RuntimeException('Stream is not seekable'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function rewind() |
61
|
|
|
{ |
62
|
|
|
throw new \RuntimeException('Stream is not seekable'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function isWritable(): bool |
66
|
|
|
{ |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function write($string) |
71
|
|
|
{ |
72
|
|
|
throw new \RuntimeException('Stream is not writable'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function isReadable(): bool |
76
|
|
|
{ |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function read($length): string |
81
|
|
|
{ |
82
|
|
|
$continue = ($this->stream)($this->buffer); |
83
|
|
|
|
84
|
|
|
if (!$continue) { |
85
|
|
|
$this->eof = true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$output = ''; |
89
|
|
|
foreach ($this->buffer as $key => $value) { |
90
|
|
|
unset($this->buffer[$key]); |
91
|
|
|
$output .= sprintf("data: %s\n", $value); |
92
|
|
|
} |
93
|
|
|
$output .= "\n"; |
94
|
|
|
return $output; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getContents() |
98
|
|
|
{ |
99
|
|
|
// TODO: Implement getContents() method. |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getMetadata($key = null): array |
103
|
|
|
{ |
104
|
|
|
return []; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|