PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Nano/library/Nano/Response.php

http://mvh-source.googlecode.com/
PHP | 120 lines | 93 code | 20 blank | 7 comment | 5 complexity | b2ad1857d108640c1af26f15e9a5ed4a MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Response object; what comes after request.
  4. *
  5. * shoud be be renamed to http response
  6. */
  7. class Nano_Response{
  8. private $_content = array();
  9. private $_headers = array();
  10. public function __construct( array $args = array() ){
  11. foreach( $args as $key => $value ){
  12. $this->__set( $key, $value );
  13. }
  14. }
  15. public function __set( $key, $value ){
  16. if( ( $method = 'set' . ucfirst($key) ) && method_exists( $this, $method ) ){
  17. $this->$method( $value );
  18. }
  19. else if( ($member = "_$key") && property_exists( $this, $member ) ){
  20. $this->$member = $value;
  21. }
  22. }
  23. public function __toString(){
  24. return join( "\n", $this->_content );
  25. }
  26. public function out(){
  27. $content = join( "\n", $this->_content );
  28. $this->addHeaders(
  29. //array( 'Expires: ' . date( 'r', strtotime('+1 Month', $inserted ) ) ),
  30. array( 'Cache-Control: max-age=36000, must-revalidate' ),
  31. array( 'Content-Length: ' . strlen($content), True ),
  32. array( 'Pragma: cache' )
  33. );
  34. foreach( $this->_headers as $header ){
  35. list( $string, $replace, $code ) = array_pad( (array) $header, 3, null );
  36. header( $string, $replace, $code );
  37. //call_user_func_array( 'header', $header );
  38. }
  39. echo $content;
  40. }
  41. public function redirect( $where, $how = 303 ){
  42. header( sprintf( 'Location: %s', $where, $how ));
  43. exit(1);
  44. }
  45. public function unshiftContent( $content ){
  46. array_unshift( $this->_content, $content );
  47. return $this;
  48. }
  49. public function push( $content ){
  50. $this->pushContent( $content );
  51. }
  52. public function pushContent( $content ){
  53. $this->_content[] = $content;
  54. return $this;
  55. }
  56. public function popContent( $content ){
  57. array_pop( $this->_content[] );
  58. return $content;
  59. }
  60. public function setStatus( $code = 200 ){
  61. $this->_status = $code;
  62. return $this;
  63. }
  64. public function getStatus(){
  65. return $this->_status;
  66. }
  67. public function addHeaders( array $headers = array() ){
  68. foreach( $headers as $header ){
  69. list($string, $replace, $status) = array_pad( (array) $header, 3, null );
  70. $this->addHeader( $string, $replace, $status );
  71. }
  72. return $this;
  73. }
  74. public function addHeader( $string, $replace = True, $status = null ){
  75. $this->_headers[] = array($string, $replace, $status);
  76. return $this;
  77. }
  78. public function setHeaders( array $header ){
  79. $this->clearHeaders();
  80. foreach( $headers as $header ){
  81. $this->_headers[] = $header;
  82. }
  83. return $this;
  84. }
  85. public function setContent( array $content ){
  86. $this->clear();
  87. foreach( $content as $value ){
  88. $this->_content[] = $value;
  89. }
  90. return $this;
  91. }
  92. public function clear(){
  93. $this->_content = array();
  94. return $this;
  95. }
  96. public function clearHeaders(){
  97. $this->_outputHeaders = array();
  98. return $this;
  99. }
  100. }