Drupal investigation

StreamedResponse.php 3.2KB

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