/application/code/core/Wootook/Core/Controller/Response/Http.php

https://github.com/boermansjo/wootook · PHP · 158 lines · 127 code · 31 blank · 0 comment · 10 complexity · 8ddccfbbb4046dbe4b5b06be3ad8ab97 MD5 · raw file

  1. <?php
  2. class Wootook_Core_Controller_Response_Http
  3. extends Wootook_Object
  4. {
  5. const REDIRECT_MOVED_PERMANENTLY = 301;
  6. const REDIRECT_FOUND = 302;
  7. const REDIRECT_SEE_OTHER = 303;
  8. const REDIRECT_TEMPORARY = 307;
  9. protected static $_redirectCodes = array(
  10. self::REDIRECT_MOVED_PERMANENTLY => 'Moved Permanently',
  11. self::REDIRECT_FOUND => 'Found',
  12. self::REDIRECT_SEE_OTHER => 'See Other',
  13. self::REDIRECT_TEMPORARY => 'Temporary Redirect'
  14. );
  15. public function __construct()
  16. {
  17. parent::__construct(array());
  18. }
  19. public function setBody($data)
  20. {
  21. $this->clearBody();
  22. return $this->appendBody($data);
  23. }
  24. public function appendBody($data)
  25. {
  26. if (!isset($this->_data['body']) || !is_array($this->_data['body'])) {
  27. $this->clearBody();
  28. }
  29. $this->_data['body'][] = $data;
  30. return $this;
  31. }
  32. public function clearBody()
  33. {
  34. $this->_data['body'] = array();
  35. return $this;
  36. }
  37. public function sendBody()
  38. {
  39. return implode('', $this->_data['body']);
  40. }
  41. public function clearHeaders()
  42. {
  43. $this->_data['headers'] = array();
  44. return $this;
  45. }
  46. public function clearRawHeaders()
  47. {
  48. $this->_data['raw_headers'] = array();
  49. return $this;
  50. }
  51. public function clearAllHeaders()
  52. {
  53. $this->clearHeaders();
  54. $this->clearRawHeaders();
  55. return $this;
  56. }
  57. public function setHeader($name, $value)
  58. {
  59. if (!isset($this->_data['headers']) || !is_array($this->_data['headers'])) {
  60. $this->clearHeaders();
  61. }
  62. $this->_data['headers'][$name] = $value;
  63. return $this;
  64. }
  65. public function setRawHeader($name, $value)
  66. {
  67. if (!isset($this->_data['raw_headers']) || !is_array($this->_data['raw_headers'])) {
  68. $this->clearRawHeaders();
  69. }
  70. $this->_data['raw_headers'][] = $value;
  71. return $this;
  72. }
  73. public function sendHeaders()
  74. {
  75. if (isset($this->_data['raw_headers'])) {
  76. foreach ($this->_data['raw_headers'] as $header) {
  77. header($header);
  78. }
  79. }
  80. if (isset($this->_data['headers'])) {
  81. foreach ($this->_data['headers'] as $headerName => $headerValue) {
  82. header("{$headerName}: {$headerValue}");
  83. }
  84. }
  85. $this->clearAllHeaders();
  86. return $this;
  87. }
  88. public function setIsRedirect($value = null)
  89. {
  90. if (is_bool($value)) {
  91. $this->_data['is_redirect'] = $value;
  92. }
  93. return $this->_data['is_redirect'];
  94. }
  95. public function setIsDispatched($dispatched = true)
  96. {
  97. return $this->setData('id_dispatched', $dispatched);
  98. }
  99. public function getIsDispatched()
  100. {
  101. return $this->getData('id_dispatched');
  102. }
  103. public function setCookie($name, $value, $lifetime = null, $path = null, $domain = null)
  104. {
  105. return $this->setRawCookie($name, serialize($value), $lifetime, $path, $domain);
  106. }
  107. public function unsetCookie($name, $path = null, $domain = null)
  108. {
  109. return $this->setRawCookie($name, null, 0, $path, $domain);
  110. }
  111. public function setRawCookie($name, $value, $lifetime = null, $path = null, $domain = null)
  112. {
  113. setcookie($name, $value, time() + $lifetime, $path, $domain);
  114. return $this;
  115. }
  116. public function setRedirect($url, $code = self::REDIRECT_FOUND)
  117. {
  118. if (!in_array($code, self::$_redirectCodes)) {
  119. $code = self::REDIRECT_FOUND;
  120. }
  121. $statusText = self::$_redirectCodes[$code];
  122. $this->setRawHeader("HTTP/1.1 {$code} {$statusText}")
  123. ->setHeader('Location', $url)
  124. ->setIsRedirect(true);
  125. return $this;
  126. }
  127. }