-
-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathServerEvent.php
More file actions
145 lines (123 loc) · 3.3 KB
/
ServerEvent.php
File metadata and controls
145 lines (123 loc) · 3.3 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation;
/**
* An event generated on the server intended for streaming to the client
* as part of the SSE streaming technique.
*
* @implements \IteratorAggregate<string>
*
* @author Yonel Ceruto <open@yceruto.dev>
*/
class ServerEvent implements \IteratorAggregate
{
/**
* @param string|iterable<string> $data The event data field for the message
* @param string|null $type The event type
* @param int|null $retry The number of milliseconds the client should wait
* before reconnecting in case of network failure
* @param string|null $id The event ID to set the EventSource object's last event ID value
* @param string|null $comment The event comment
*/
public function __construct(
private string|iterable $data,
private ?string $type = null,
private ?int $retry = null,
private ?string $id = null,
private ?string $comment = null,
) {
}
public function getData(): iterable|string
{
return $this->data;
}
/**
* @return $this
*/
public function setData(iterable|string $data): static
{
$this->data = $data;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
/**
* @return $this
*/
public function setType(string $type): static
{
$this->type = $type;
return $this;
}
public function getRetry(): ?int
{
return $this->retry;
}
/**
* @return $this
*/
public function setRetry(?int $retry): static
{
$this->retry = $retry;
return $this;
}
public function getId(): ?string
{
return $this->id;
}
/**
* @return $this
*/
public function setId(string $id): static
{
$this->id = $id;
return $this;
}
public function getComment(): ?string
{
return $this->comment;
}
public function setComment(string $comment): static
{
$this->comment = $comment;
return $this;
}
/**
* @return \Traversable<string>
*/
public function getIterator(): \Traversable
{
static $lastRetry = null;
$head = '';
if ($this->comment) {
$head .= \sprintf(': %s', $this->comment)."\n";
}
if ($this->id) {
$head .= \sprintf('id: %s', $this->id)."\n";
}
if ($this->retry > 0 && $this->retry !== $lastRetry) {
$head .= \sprintf('retry: %s', $lastRetry = $this->retry)."\n";
}
if ($this->type) {
$head .= \sprintf('event: %s', $this->type)."\n";
}
yield $head;
if (is_iterable($this->data)) {
foreach ($this->data as $data) {
yield \sprintf('data: %s', $data)."\n";
}
} elseif ('' !== $this->data) {
yield \sprintf('data: %s', $this->data)."\n";
}
yield "\n";
}
}