StreamedResponse.php 2.9 KB

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