StreamedResponse.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. * StreamedResponse represents a streamed HTTP response.
  13. *
  14. * A StreamedResponse uses a callback or an iterable of strings for its content.
  15. *
  16. * The callback should use the standard PHP functions like echo
  17. * to stream the response back to the client. The flush() function
  18. * can also be used if needed.
  19. *
  20. * @see flush()
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. */
  24. class StreamedResponse extends Response
  25. {
  26. protected ?\Closure $callback = null;
  27. protected bool $streamed = false;
  28. private bool $headersSent = false;
  29. /**
  30. * @param callable|iterable<string>|null $callbackOrChunks
  31. * @param int $status The HTTP status code (200 "OK" by default)
  32. */
  33. public function __construct(callable|iterable|null $callbackOrChunks = null, int $status = 200, array $headers = [])
  34. {
  35. parent::__construct(null, $status, $headers);
  36. if (\is_callable($callbackOrChunks)) {
  37. $this->setCallback($callbackOrChunks);
  38. } elseif ($callbackOrChunks) {
  39. $this->setChunks($callbackOrChunks);
  40. }
  41. $this->streamed = false;
  42. $this->headersSent = false;
  43. }
  44. /**
  45. * @param iterable<string> $chunks
  46. */
  47. public function setChunks(iterable $chunks): static
  48. {
  49. $this->callback = static function () use ($chunks): void {
  50. foreach ($chunks as $chunk) {
  51. echo $chunk;
  52. @ob_flush();
  53. flush();
  54. }
  55. };
  56. return $this;
  57. }
  58. /**
  59. * Sets the PHP callback associated with this Response.
  60. *
  61. * @return $this
  62. */
  63. public function setCallback(callable $callback): static
  64. {
  65. $this->callback = $callback(...);
  66. return $this;
  67. }
  68. public function getCallback(): ?\Closure
  69. {
  70. if (!isset($this->callback)) {
  71. return null;
  72. }
  73. return ($this->callback)(...);
  74. }
  75. /**
  76. * This method only sends the headers once.
  77. *
  78. * @param positive-int|null $statusCode The status code to use, override the statusCode property if set and not null
  79. *
  80. * @return $this
  81. */
  82. public function sendHeaders(?int $statusCode = null): static
  83. {
  84. if ($this->headersSent) {
  85. return $this;
  86. }
  87. if ($statusCode < 100 || $statusCode >= 200) {
  88. $this->headersSent = true;
  89. }
  90. return parent::sendHeaders($statusCode);
  91. }
  92. /**
  93. * This method only sends the content once.
  94. *
  95. * @return $this
  96. */
  97. public function sendContent(): static
  98. {
  99. if ($this->streamed) {
  100. return $this;
  101. }
  102. $this->streamed = true;
  103. if (!isset($this->callback)) {
  104. throw new \LogicException('The Response callback must be set.');
  105. }
  106. ($this->callback)();
  107. return $this;
  108. }
  109. /**
  110. * @return $this
  111. *
  112. * @throws \LogicException when the content is not null
  113. */
  114. public function setContent(?string $content): static
  115. {
  116. if (null !== $content) {
  117. throw new \LogicException('The content cannot be set on a StreamedResponse instance.');
  118. }
  119. $this->streamed = true;
  120. return $this;
  121. }
  122. public function getContent(): string|false
  123. {
  124. return false;
  125. }
  126. }