PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/includes/classes/response.php

https://bitbucket.org/slogsdon/unamed
PHP | 200 lines | 108 code | 12 blank | 80 comment | 7 complexity | f17bebec43d57fa47f45fab57da01db1 MD5 | raw file
  1. <?php
  2. /**
  3. * Unamed - a WordPress replacement
  4. *
  5. * @category CMS
  6. * @package Unamed
  7. * @author Shane Logsdon <shane.a.logsdon@gmail.com>
  8. * @license MIT http://mit.edu/
  9. * @link http://bitbucket.org/slogsdon/unamed
  10. */
  11. namespace Unamed\FrontController;
  12. use Unamed\Interfaces;
  13. {
  14. /**
  15. * Response implementation
  16. *
  17. * @category Class
  18. * @package Unamed
  19. * @author Shane Logsdon <shane.a.logsdon@gmail.com>
  20. * @license MIT http://mit.edu/
  21. * @link http://bitbucket.org/slogsdon/unamed
  22. * @since 1.0
  23. */
  24. class Response implements Interfaces\Response
  25. {
  26. /* Constants */
  27. const DEFAULT_STATUS = 200;
  28. const HTTP_10 = 'HTTP/1.0';
  29. const HTTP_11 = 'HTTP/1.1';
  30. /* Properties */
  31. protected $headers = array();
  32. protected $options = array();
  33. protected $status = self::DEFAULT_STATUS;
  34. protected $use10 = false;
  35. /* Methods */
  36. /**
  37. * __construct
  38. */
  39. public function __construct()
  40. {
  41. }
  42. /**
  43. * addHeader
  44. *
  45. * @param string $name -
  46. * @param string $value -
  47. * @param bool $replace -
  48. *
  49. * @return object(Response)
  50. */
  51. public function addHeader($name, $value, $replace = true)
  52. {
  53. $this->headers[] = array(
  54. 'name' => $name,
  55. 'value' => $value,
  56. 'replace' => $replace,
  57. );
  58. return $this;
  59. }
  60. /**
  61. * addHeaders
  62. *
  63. * @param bool $headers -
  64. *
  65. * @return object(Response)
  66. */
  67. public function addHeaders(array $headers)
  68. {
  69. foreach ($headers as $header) {
  70. $this->addHeader(
  71. $header['name'],
  72. $header['value'],
  73. isset($header['replace']) ? $header['replace'] : true
  74. );
  75. }
  76. return $this;
  77. }
  78. /**
  79. * deliver
  80. *
  81. * @param string $request -
  82. * @param callback|string $data -
  83. *
  84. * @return nothing
  85. */
  86. public function deliver($request, $data)
  87. {
  88. // set up
  89. if ($this->use10) {
  90. header(self::HTTP_10 . ' ' . $this->status);
  91. } else {
  92. header(self::HTTP_11 . ' ' . $this->status);
  93. }
  94. foreach ($this->headers as $header) {
  95. header(
  96. $header['name'] . ': ' .
  97. $header['value'], $header['replace']
  98. );
  99. }
  100. // deliver
  101. if (is_callable($data)) {
  102. call_user_func($data);
  103. } else {
  104. echo $data;
  105. }
  106. return;
  107. }
  108. /**
  109. * noCache
  110. *
  111. * @return object(Response)
  112. */
  113. public function noCache()
  114. {
  115. $this->addHeaders(
  116. array(
  117. array(
  118. 'name' => 'Cache-Control',
  119. 'value' => 'no-store, no-cache, must-revalidate'
  120. ),
  121. array(
  122. 'name' => 'Cache-Control',
  123. 'value' => 'post-check=0, pre-check=0',
  124. 'replace' => false
  125. ),
  126. array(
  127. 'name' => 'Pragma',
  128. 'value' => 'no-cache'
  129. ),
  130. )
  131. );
  132. return $this;
  133. }
  134. /**
  135. * setOptions
  136. *
  137. * @param string $options -
  138. *
  139. * @return object(Response)
  140. */
  141. public function setOptions(array $options)
  142. {
  143. $this->options = $options;
  144. return $this;
  145. }
  146. /**
  147. * setStatus
  148. *
  149. * @param int $status -
  150. *
  151. * @return object(Response)
  152. */
  153. public function setStatus($status)
  154. {
  155. if (is_integer($status))
  156. $this->status = $status;
  157. return $this;
  158. }
  159. /**
  160. * useHttp10
  161. *
  162. * @return object(Response)
  163. */
  164. public function useHttp10()
  165. {
  166. if (function_exists('apache_setenv')) {
  167. apache_setenv('downgrade-1.0', 'true');
  168. apache_setenv('force-response-1.0', 'true');
  169. }
  170. $this->use10 = true;
  171. return $this;
  172. }
  173. /**
  174. * useHttp11
  175. *
  176. * @return object(Response)
  177. */
  178. public function useHttp11()
  179. {
  180. if (function_exists('apache_setenv')) {
  181. apache_setenv('downgrade-1.0', 'false');
  182. apache_setenv('force-response-1.0', 'false');
  183. }
  184. $this->use10 = false;
  185. return $this;
  186. }
  187. };
  188. }