| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class ServerEvent implements \IteratorAggregate |
| 23 |
{ |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public function __construct( |
| 33 |
private string|iterable $data, |
| 34 |
private ?string $type = null, |
| 35 |
private ?int $retry = null, |
| 36 |
private ?string $id = null, |
| 37 |
private ?string $comment = null, |
| 38 |
) { |
| 39 |
} |
| 40 |
|
| 41 |
public function getData(): iterable|string |
| 42 |
{ |
| 43 |
return $this->data; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function setData(iterable|string $data): static |
| 50 |
{ |
| 51 |
$this->data = $data; |
| 52 |
|
| 53 |
return $this; |
| 54 |
} |
| 55 |
|
| 56 |
public function getType(): ?string |
| 57 |
{ |
| 58 |
return $this->type; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function setType(string $type): static |
| 65 |
{ |
| 66 |
$this->type = $type; |
| 67 |
|
| 68 |
return $this; |
| 69 |
} |
| 70 |
|
| 71 |
public function getRetry(): ?int |
| 72 |
{ |
| 73 |
return $this->retry; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function setRetry(?int $retry): static |
| 80 |
{ |
| 81 |
$this->retry = $retry; |
| 82 |
|
| 83 |
return $this; |
| 84 |
} |
| 85 |
|
| 86 |
public function getId(): ?string |
| 87 |
{ |
| 88 |
return $this->id; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function setId(string $id): static |
| 95 |
{ |
| 96 |
$this->id = $id; |
| 97 |
|
| 98 |
return $this; |
| 99 |
} |
| 100 |
|
| 101 |
public function getComment(): ?string |
| 102 |
{ |
| 103 |
return $this->comment; |
| 104 |
} |
| 105 |
|
| 106 |
public function setComment(string $comment): static |
| 107 |
{ |
| 108 |
$this->comment = $comment; |
| 109 |
|
| 110 |
return $this; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function getIterator(): \Traversable |
| 117 |
{ |
| 118 |
static $lastRetry = null; |
| 119 |
|
| 120 |
$head = ''; |
| 121 |
if ($this->comment) { |
| 122 |
$head .= \sprintf(': %s', $this->comment)."\n"; |
| 123 |
} |
| 124 |
if ($this->id) { |
| 125 |
$head .= \sprintf('id: %s', $this->id)."\n"; |
| 126 |
} |
| 127 |
if ($this->retry > 0 && $this->retry !== $lastRetry) { |
| 128 |
$head .= \sprintf('retry: %s', $lastRetry = $this->retry)."\n"; |
| 129 |
} |
| 130 |
if ($this->type) { |
| 131 |
$head .= \sprintf('event: %s', $this->type)."\n"; |
| 132 |
} |
| 133 |
yield $head; |
| 134 |
|
| 135 |
if (is_iterable($this->data)) { |
| 136 |
foreach ($this->data as $data) { |
| 137 |
yield \sprintf('data: %s', $data)."\n"; |
| 138 |
} |
| 139 |
} elseif ('' !== $this->data) { |
| 140 |
yield \sprintf('data: %s', $this->data)."\n"; |
| 141 |
} |
| 142 |
|
| 143 |
yield "\n"; |
| 144 |
} |
| 145 |
} |