/Response.php

https://github.com/tlhunter/sleekmvc · PHP · 167 lines · 93 code · 19 blank · 55 comment · 8 complexity · 6761db3011aa23e01447f5952e77e8af MD5 · raw file

  1. <?php
  2. namespace Sleek;
  3. class Response {
  4. /**
  5. * @var Response The singleton instance of our response class
  6. */
  7. static private $_instance = NULL;
  8. /**
  9. * @var array A listing of HTTP status codes
  10. * @author http://coreymaynard.com/blog/creating-a-restful-api-with-php/
  11. */
  12. static public $status = array(
  13. 100 => 'Continue',
  14. 101 => 'Switching Protocols',
  15. 200 => 'OK',
  16. 201 => 'Created',
  17. 202 => 'Accepted',
  18. 203 => 'Non-Authoritative Information',
  19. 204 => 'No Content',
  20. 205 => 'Reset Content',
  21. 206 => 'Partial Content',
  22. 300 => 'Multiple Choices',
  23. 301 => 'Moved Permanently',
  24. 302 => 'Found',
  25. 303 => 'See Other',
  26. 304 => 'Not Modified',
  27. 305 => 'Use Proxy',
  28. 306 => '(Unused)',
  29. 307 => 'Temporary Redirect',
  30. 400 => 'Bad Request',
  31. 401 => 'Unauthorized',
  32. 402 => 'Payment Required',
  33. 403 => 'Forbidden',
  34. 404 => 'Not Found',
  35. 405 => 'Method Not Allowed',
  36. 406 => 'Not Acceptable',
  37. 407 => 'Proxy Authentication Required',
  38. 408 => 'Request Timeout',
  39. 409 => 'Conflict',
  40. 410 => 'Gone',
  41. 411 => 'Length Required',
  42. 412 => 'Precondition Failed',
  43. 413 => 'Request Entity Too Large',
  44. 414 => 'Request-URI Too Long',
  45. 415 => 'Unsupported Media Type',
  46. 416 => 'Requested Range Not Satisfiable',
  47. 417 => 'Expectation Failed',
  48. 500 => 'Internal Server Error',
  49. 501 => 'Not Implemented',
  50. 502 => 'Bad Gateway',
  51. 503 => 'Service Unavailable',
  52. 504 => 'Gateway Timeout',
  53. 505 => 'HTTP Version Not Supported'
  54. );
  55. private function __construct() { }
  56. /**
  57. * Prevents the class from being cloned
  58. * @return null
  59. */
  60. private function __clone() { }
  61. /**
  62. * Returns the singleton instance of the Database class
  63. * @return Response
  64. */
  65. public static function getInstance() {
  66. if (!self::$_instance) {
  67. self::$_instance = new Response();
  68. }
  69. return self::$_instance;
  70. }
  71. /**
  72. * Redirects the user to the specified page
  73. * @static
  74. * @param string $url URL to redirect user to
  75. * @param bool $permanent If true, sends a 301 permanent
  76. * @return bool Whether or not the header was set properly
  77. */
  78. public static function redirect($url, $permanent = FALSE) {
  79. if (headers_sent()) {
  80. return FALSE;
  81. }
  82. if ($permanent) {
  83. header("HTTP/1.1 301 Moved Permanently");
  84. }
  85. header("Location: $url");
  86. return TRUE;
  87. }
  88. /**
  89. * Sends an HTTP header to the client
  90. * @static
  91. * @param string $name The name of the HTTP header
  92. * @param string $value The value of the HTTP header
  93. * @return bool Whether or not the header was set
  94. */
  95. public static function header($name, $value = NULL) {
  96. if (headers_sent()) {
  97. return FALSE;
  98. }
  99. $header = $name;
  100. if ($value) {
  101. $header .= ": $value";
  102. }
  103. header($header);
  104. return TRUE;
  105. }
  106. /**
  107. * Sets a status code by taking the number and automatically adding the status text for the developer
  108. * @link http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
  109. * @todo List needs to contain more entries... All of them?
  110. * @param int $code
  111. * @return bool
  112. */
  113. public static function status($code) {
  114. if (headers_sent() || !isset(self::$status[$code])) {
  115. return FALSE;
  116. }
  117. header("HTTP/1.1 $code " . self::$status[$code]);
  118. return TRUE;
  119. }
  120. /**
  121. * Renders a view and sends it to the browser. Can't be used to return strings.
  122. * To return a string, use \Sleek\View::render($file, $data, TRUE) instead.
  123. * @param string $file Path to view file to render, minus the extension
  124. * @param array $data Data array, ran through extract()
  125. * @return void
  126. */
  127. public function view($file, $data = array()) {
  128. \Sleek\View::render($file, $data);
  129. }
  130. /**
  131. * This function sets a cookie. To read a cookie, use Request::cookie();
  132. * @static
  133. * @param string $name Name of the cookie to use
  134. * @param mixed $value The value of the cookie being set
  135. * @param int $expire Expiration date
  136. * @param null $path Cookie Path
  137. * @param null $domain Cookie Domain
  138. * @param null $secure Cookie Secure
  139. * @param null $httponly Cookie HTTPOnly
  140. * @return bool Success or failure
  141. */
  142. public static function cookie($name, $value, $expire = 0, $path = NULL, $domain = NULL, $secure = NULL, $httponly = NULL) {
  143. if (headers_sent()) {
  144. return FALSE;
  145. }
  146. return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
  147. }
  148. }