ServerEvent.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation;
  11. /**
  12. * An event generated on the server intended for streaming to the client
  13. * as part of the SSE streaming technique.
  14. *
  15. * @implements \IteratorAggregate<string>
  16. *
  17. * @author Yonel Ceruto <open@yceruto.dev>
  18. */
  19. class ServerEvent implements \IteratorAggregate
  20. {
  21. /**
  22. * @param string|iterable<string> $data The event data field for the message
  23. * @param string|null $type The event type
  24. * @param int|null $retry The number of milliseconds the client should wait
  25. * before reconnecting in case of network failure
  26. * @param string|null $id The event ID to set the EventSource object's last event ID value
  27. * @param string|null $comment The event comment
  28. */
  29. public function __construct(
  30. private string|iterable $data,
  31. private ?string $type = null,
  32. private ?int $retry = null,
  33. private ?string $id = null,
  34. private ?string $comment = null,
  35. ) {
  36. }
  37. public function getData(): iterable|string
  38. {
  39. return $this->data;
  40. }
  41. /**
  42. * @return $this
  43. */
  44. public function setData(iterable|string $data): static
  45. {
  46. $this->data = $data;
  47. return $this;
  48. }
  49. public function getType(): ?string
  50. {
  51. return $this->type;
  52. }
  53. /**
  54. * @return $this
  55. */
  56. public function setType(string $type): static
  57. {
  58. $this->type = $type;
  59. return $this;
  60. }
  61. public function getRetry(): ?int
  62. {
  63. return $this->retry;
  64. }
  65. /**
  66. * @return $this
  67. */
  68. public function setRetry(?int $retry): static
  69. {
  70. $this->retry = $retry;
  71. return $this;
  72. }
  73. public function getId(): ?string
  74. {
  75. return $this->id;
  76. }
  77. /**
  78. * @return $this
  79. */
  80. public function setId(string $id): static
  81. {
  82. $this->id = $id;
  83. return $this;
  84. }
  85. public function getComment(): ?string
  86. {
  87. return $this->comment;
  88. }
  89. public function setComment(string $comment): static
  90. {
  91. $this->comment = $comment;
  92. return $this;
  93. }
  94. /**
  95. * @return \Traversable<string>
  96. */
  97. public function getIterator(): \Traversable
  98. {
  99. static $lastRetry = null;
  100. $head = '';
  101. if ($this->comment) {
  102. $head .= \sprintf(': %s', $this->comment)."\n";
  103. }
  104. if ($this->id) {
  105. $head .= \sprintf('id: %s', $this->id)."\n";
  106. }
  107. if ($this->retry > 0 && $this->retry !== $lastRetry) {
  108. $head .= \sprintf('retry: %s', $lastRetry = $this->retry)."\n";
  109. }
  110. if ($this->type) {
  111. $head .= \sprintf('event: %s', $this->type)."\n";
  112. }
  113. yield $head;
  114. if (is_iterable($this->data)) {
  115. foreach ($this->data as $data) {
  116. yield \sprintf('data: %s', $data)."\n";
  117. }
  118. } elseif ('' !== $this->data) {
  119. yield \sprintf('data: %s', $this->data)."\n";
  120. }
  121. yield "\n";
  122. }
  123. }