ServerSentEvent.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\HttpClient\Chunk;
  11. use Symfony\Component\HttpClient\Exception\JsonException;
  12. use Symfony\Contracts\HttpClient\ChunkInterface;
  13. /**
  14. * @author Antoine Bluchet <soyuka@gmail.com>
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. final class ServerSentEvent extends DataChunk implements ChunkInterface
  18. {
  19. /**
  20. * @var string
  21. */
  22. private $data = '';
  23. /**
  24. * @var string
  25. */
  26. private $id = '';
  27. /**
  28. * @var string
  29. */
  30. private $type = 'message';
  31. /**
  32. * @var float
  33. */
  34. private $retry = 0;
  35. /**
  36. * @var mixed[]|null
  37. */
  38. private $jsonData;
  39. /**
  40. * @param string $content
  41. */
  42. public function __construct($content)
  43. {
  44. parent::__construct(-1, $content);
  45. // remove BOM
  46. if (strncmp($content, "\xEF\xBB\xBF", strlen("\xEF\xBB\xBF")) === 0) {
  47. $content = substr($content, 3);
  48. }
  49. foreach (preg_split("/(?:\r\n|[\r\n])/", $content) as $line) {
  50. if (0 === $i = strpos($line, ':')) {
  51. continue;
  52. }
  53. $i = false === $i ? \strlen($line) : $i;
  54. $field = substr($line, 0, $i);
  55. $i += 1 + (' ' === ($line[1 + $i] ?? ''));
  56. switch ($field) {
  57. case 'id': $this->id = substr($line, $i); break;
  58. case 'event': $this->type = substr($line, $i); break;
  59. case 'data': $this->data .= ('' === $this->data ? '' : "\n").substr($line, $i); break;
  60. case 'retry':
  61. $retry = substr($line, $i);
  62. if ('' !== $retry && \strlen($retry) === strspn($retry, '0123456789')) {
  63. $this->retry = $retry / 1000.0;
  64. }
  65. break;
  66. }
  67. }
  68. }
  69. public function getId()
  70. {
  71. return $this->id;
  72. }
  73. public function getType()
  74. {
  75. return $this->type;
  76. }
  77. public function getData()
  78. {
  79. return $this->data;
  80. }
  81. public function getRetry()
  82. {
  83. return $this->retry;
  84. }
  85. /**
  86. * Gets the SSE data decoded as an array when it's a JSON payload.
  87. */
  88. public function getArrayData()
  89. {
  90. if (null !== $this->jsonData) {
  91. return $this->jsonData;
  92. }
  93. if ('' === $this->data) {
  94. throw new JsonException(sprintf('Server-Sent Event%s data is empty.', '' !== $this->id ? sprintf(' "%s"', $this->id) : ''));
  95. }
  96. try {
  97. $jsonData = json_decode($this->data, true, 512, \JSON_BIGINT_AS_STRING);
  98. } catch (\JsonException $e) {
  99. throw new JsonException(sprintf('Decoding Server-Sent Event%s failed: ', '' !== $this->id ? sprintf(' "%s"', $this->id) : '').$e->getMessage(), $e->getCode());
  100. }
  101. if (!\is_array($jsonData)) {
  102. throw new JsonException(sprintf('JSON content was expected to decode to an array, "%s" returned in Server-Sent Event%s.', get_debug_type($jsonData), '' !== $this->id ? sprintf(' "%s"', $this->id) : ''));
  103. }
  104. return $this->jsonData = $jsonData;
  105. }
  106. }