StreamedResponse.php 2.9 KB

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