AbstractResult.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Controller;
  7. use Magento\Framework\App\ResponseInterface;
  8. use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
  9. abstract class AbstractResult implements ResultInterface
  10. {
  11. /**
  12. * @var int
  13. */
  14. protected $httpResponseCode;
  15. /**
  16. * @var array
  17. */
  18. protected $headers = [];
  19. /**
  20. * @var string
  21. */
  22. protected $statusHeaderCode;
  23. /**
  24. * @var string
  25. */
  26. protected $statusHeaderVersion;
  27. /**
  28. * @var string
  29. */
  30. protected $statusHeaderPhrase;
  31. /**
  32. * Set response code to result
  33. *
  34. * @param int $httpCode
  35. * @return $this
  36. */
  37. public function setHttpResponseCode($httpCode)
  38. {
  39. $this->httpResponseCode = $httpCode;
  40. return $this;
  41. }
  42. /**
  43. * Set a header
  44. *
  45. * If $replace is true, replaces any headers already defined with that
  46. * $name.
  47. *
  48. * @param string $name
  49. * @param string $value
  50. * @param boolean $replace
  51. * @return $this
  52. */
  53. public function setHeader($name, $value, $replace = false)
  54. {
  55. $this->headers[] = [
  56. 'name' => $name,
  57. 'value' => $value,
  58. 'replace' => $replace,
  59. ];
  60. return $this;
  61. }
  62. /**
  63. * @param int|string $httpCode
  64. * @param null|int|string $version
  65. * @param null|string $phrase
  66. * @return $this
  67. */
  68. public function setStatusHeader($httpCode, $version = null, $phrase = null)
  69. {
  70. $this->statusHeaderCode = $httpCode;
  71. $this->statusHeaderVersion = $version;
  72. $this->statusHeaderPhrase = $phrase;
  73. return $this;
  74. }
  75. /**
  76. * @param HttpResponseInterface $response
  77. * @return $this
  78. */
  79. protected function applyHttpHeaders(HttpResponseInterface $response)
  80. {
  81. if (!empty($this->httpResponseCode)) {
  82. $response->setHttpResponseCode($this->httpResponseCode);
  83. }
  84. if ($this->statusHeaderCode) {
  85. $response->setStatusHeader(
  86. $this->statusHeaderCode,
  87. $this->statusHeaderVersion,
  88. $this->statusHeaderPhrase
  89. );
  90. }
  91. if (!empty($this->headers)) {
  92. foreach ($this->headers as $headerData) {
  93. $response->setHeader($headerData['name'], $headerData['value'], $headerData['replace']);
  94. }
  95. }
  96. return $this;
  97. }
  98. /**
  99. * @param HttpResponseInterface $response
  100. * @return $this
  101. */
  102. abstract protected function render(HttpResponseInterface $response);
  103. /**
  104. * Render content
  105. *
  106. * @param HttpResponseInterface|ResponseInterface $response
  107. * @return $this
  108. */
  109. public function renderResult(ResponseInterface $response)
  110. {
  111. $this->applyHttpHeaders($response);
  112. return $this->render($response);
  113. }
  114. }