StreamedResponse.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 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 $callback;
  27. protected $streamed;
  28. private bool $headersSent;
  29. /**
  30. * @param int $status The HTTP status code (200 "OK" by default)
  31. */
  32. public function __construct(?callable $callback = null, int $status = 200, array $headers = [])
  33. {
  34. parent::__construct(null, $status, $headers);
  35. if (null !== $callback) {
  36. $this->setCallback($callback);
  37. }
  38. $this->streamed = false;
  39. $this->headersSent = false;
  40. }
  41. /**
  42. * Sets the PHP callback associated with this Response.
  43. *
  44. * @return $this
  45. */
  46. public function setCallback(callable $callback): static
  47. {
  48. $this->callback = $callback(...);
  49. return $this;
  50. }
  51. public function getCallback(): ?\Closure
  52. {
  53. if (!isset($this->callback)) {
  54. return null;
  55. }
  56. return ($this->callback)(...);
  57. }
  58. /**
  59. * This method only sends the headers once.
  60. *
  61. * @param positive-int|null $statusCode The status code to use, override the statusCode property if set and not null
  62. *
  63. * @return $this
  64. */
  65. public function sendHeaders(/* int $statusCode = null */): static
  66. {
  67. if ($this->headersSent) {
  68. return $this;
  69. }
  70. $statusCode = \func_num_args() > 0 ? func_get_arg(0) : null;
  71. if ($statusCode < 100 || $statusCode >= 200) {
  72. $this->headersSent = true;
  73. }
  74. return parent::sendHeaders($statusCode);
  75. }
  76. /**
  77. * This method only sends the content once.
  78. *
  79. * @return $this
  80. */
  81. public function sendContent(): static
  82. {
  83. if ($this->streamed) {
  84. return $this;
  85. }
  86. $this->streamed = true;
  87. if (!isset($this->callback)) {
  88. throw new \LogicException('The Response callback must be set.');
  89. }
  90. ($this->callback)();
  91. return $this;
  92. }
  93. /**
  94. * @return $this
  95. *
  96. * @throws \LogicException when the content is not null
  97. */
  98. public function setContent(?string $content): static
  99. {
  100. if (null !== $content) {
  101. throw new \LogicException('The content cannot be set on a StreamedResponse instance.');
  102. }
  103. $this->streamed = true;
  104. return $this;
  105. }
  106. public function getContent(): string|false
  107. {
  108. return false;
  109. }
  110. }