Response.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Console;
  7. /**
  8. * @SuppressWarnings(PHPMD.ExitExpression)
  9. */
  10. class Response implements \Magento\Framework\App\ResponseInterface
  11. {
  12. /**
  13. * Status code
  14. * Possible values:
  15. * 0 (successfully)
  16. * 1-255 (error)
  17. * -1 (error)
  18. *
  19. * @var int
  20. */
  21. protected $code = 0;
  22. /**
  23. * Success code
  24. */
  25. const SUCCESS = 0;
  26. /**
  27. * Error code
  28. */
  29. const ERROR = 255;
  30. /**
  31. * Text to output on send response
  32. *
  33. * @var string
  34. */
  35. private $body;
  36. /**
  37. * Set whether to terminate process on send or not
  38. *
  39. * @var bool
  40. */
  41. protected $terminateOnSend = true;
  42. /**
  43. * Send response to client
  44. *
  45. * @return int
  46. */
  47. public function sendResponse()
  48. {
  49. if (!empty($this->body)) {
  50. echo $this->body;
  51. }
  52. if ($this->terminateOnSend) {
  53. exit($this->code);
  54. }
  55. return $this->code;
  56. }
  57. /**
  58. * Get body
  59. *
  60. * @return string
  61. */
  62. public function getBody()
  63. {
  64. return $this->body;
  65. }
  66. /**
  67. * Set body
  68. *
  69. * @param string $body
  70. * @return void
  71. */
  72. public function setBody($body)
  73. {
  74. $this->body = $body;
  75. }
  76. /**
  77. * Set exit code
  78. *
  79. * @param int $code
  80. * @return void
  81. */
  82. public function setCode($code)
  83. {
  84. if ($code > 255) {
  85. $code = 255;
  86. }
  87. $this->code = $code;
  88. }
  89. /**
  90. * Set whether to terminate process on send or not
  91. *
  92. * @param bool $terminate
  93. * @return void
  94. */
  95. public function terminateOnSend($terminate)
  96. {
  97. $this->terminateOnSend = $terminate;
  98. }
  99. }