StreamedResponse.php 2.6 KB

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