From 89766bef84c82737c416b6a26dc6c5b26ea3d2aa Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 28 Mar 2012 17:11:55 -0500 Subject: [PATCH] converting to httpfoundation response. --- laravel/core.php | 2 +- laravel/helpers.php | 11 +++ laravel/request.php | 12 ++-- laravel/response.php | 163 ++++++++++--------------------------------- 4 files changed, 53 insertions(+), 135 deletions(-) diff --git a/laravel/core.php b/laravel/core.php index 8cff1b7c..b78e47d8 100644 --- a/laravel/core.php +++ b/laravel/core.php @@ -176,4 +176,4 @@ use Symfony\Component\HttpFoundation\Request as FoundationRequest; -Request::$request = FoundationRequest::createFromGlobals(); \ No newline at end of file +Request::$foundation = FoundationRequest::createFromGlobals(); \ No newline at end of file diff --git a/laravel/helpers.php b/laravel/helpers.php index b04a8c49..9caf55ff 100644 --- a/laravel/helpers.php +++ b/laravel/helpers.php @@ -361,6 +361,17 @@ function str_finish($value, $cap) return rtrim($value, $cap).$cap; } +/** + * Determine if the given object has a toString method. + * + * @param object $value + * @return bool + */ +function str_object($value) +{ + return is_object($value) and method_exists($value, '__toString'); +} + /** * Get the root namespace of a given class. * diff --git a/laravel/request.php b/laravel/request.php index 0b527fef..81c617b9 100644 --- a/laravel/request.php +++ b/laravel/request.php @@ -7,7 +7,7 @@ class Request { * * @var HttpFoundation\Request */ - public static $request; + public static $foundation; /** * All of the route instances handling the request. @@ -78,7 +78,7 @@ public static function spoofed() */ public static function ip($default = '0.0.0.0') { - return value(static::$request->getClientIp(), $default); + return value(static::$foundation->getClientIp(), $default); } /** @@ -98,7 +98,7 @@ public static function protocol() */ public static function accept() { - return static::$request->getAcceptableContentTypes(); + return static::$foundation->getAcceptableContentTypes(); } /** @@ -118,7 +118,7 @@ public static function accepts($type) */ public static function secure() { - return static::$request->isSecure(); + return static::$foundation->isSecure(); } /** @@ -140,7 +140,7 @@ public static function forged() */ public static function ajax() { - return static::$request->isXmlHttpRequest(); + return static::$foundation->isXmlHttpRequest(); } /** @@ -203,7 +203,7 @@ public static function route() */ public static function __callStatic($method, $parameters) { - return call_user_func_array(array(static::$request, $method), $parameters); + return call_user_func_array(array(static::$foundation, $method), $parameters); } } \ No newline at end of file diff --git a/laravel/response.php b/laravel/response.php index da3ba84f..18953485 100644 --- a/laravel/response.php +++ b/laravel/response.php @@ -1,5 +1,7 @@ 'Continue', - 101 => 'Switching Protocols', - 200 => 'OK', - 201 => 'Created', - 202 => 'Accepted', - 203 => 'Non-Authoritative Information', - 204 => 'No Content', - 205 => 'Reset Content', - 206 => 'Partial Content', - 207 => 'Multi-Status', - 300 => 'Multiple Choices', - 301 => 'Moved Permanently', - 302 => 'Found', - 303 => 'See Other', - 304 => 'Not Modified', - 305 => 'Use Proxy', - 307 => 'Temporary Redirect', - 400 => 'Bad Request', - 401 => 'Unauthorized', - 402 => 'Payment Required', - 403 => 'Forbidden', - 404 => 'Not Found', - 405 => 'Method Not Allowed', - 406 => 'Not Acceptable', - 407 => 'Proxy Authentication Required', - 408 => 'Request Timeout', - 409 => 'Conflict', - 410 => 'Gone', - 411 => 'Length Required', - 412 => 'Precondition Failed', - 413 => 'Request Entity Too Large', - 414 => 'Request-URI Too Long', - 415 => 'Unsupported Media Type', - 416 => 'Requested Range Not Satisfiable', - 417 => 'Expectation Failed', - 422 => 'Unprocessable Entity', - 423 => 'Locked', - 424 => 'Failed Dependency', - 500 => 'Internal Server Error', - 501 => 'Not Implemented', - 502 => 'Bad Gateway', - 503 => 'Service Unavailable', - 504 => 'Gateway Timeout', - 505 => 'HTTP Version Not Supported', - 507 => 'Insufficient Storage', - 509 => 'Bandwidth Limit Exceeded' - ); + public $foundation; /** * Create a new response instance. @@ -87,9 +28,9 @@ class Response { */ public function __construct($content, $status = 200, $headers = array()) { - $this->status = $status; $this->content = $content; - $this->headers = $headers; + + $this->foundation = new FoundationResponse('', $status, $headers); } /** @@ -181,14 +122,14 @@ public static function download($path, $name = null, $headers = array()) if (is_null($name)) $name = basename($path); $headers = array_merge(array( - 'content-description' => 'File Transfer', - 'content-type' => File::mime(File::extension($path)), - 'content-disposition' => 'attachment; filename="'.$name.'"', - 'content-transfer-encoding' => 'binary', - 'expires' => 0, - 'cache-control' => 'must-revalidate, post-check=0, pre-check=0', - 'pragma' => 'public', - 'content-length' => File::size($path), + 'Content-Description' => 'File Transfer', + 'Content-Type' => File::mime(File::extension($path)), + 'Content-Disposition' => 'attachment; filename="'.$name.'"', + 'Content-Transfer-Encoding' => 'binary', + 'Expires' => 0, + 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', + 'Pragma' => 'public', + 'Content-Length' => File::size($path), ), $headers); return new static(File::get($path), 200, $headers); @@ -205,18 +146,14 @@ public static function download($path, $name = null, $headers = array()) */ public static function prepare($response) { + // We'll need to force the response to be a string before closing + // the session, since the developer may be utilizing the session + // within the view, and we can't age it until rendering. if ( ! $response instanceof Response) { $response = new static($response); } - // We'll need to force the response to be a string before closing the session, - // since the developer may be using the session within a view, and we can't - // age the flash data until the view is rendered. - // - // Since this method is used by both the Route and Controller classes, it is - // a convenient spot to cast the application response to a string before it - // is returned to the main request handler. $response->render(); return $response; @@ -229,7 +166,10 @@ public static function prepare($response) */ public function render() { - if (is_object($this->content) and method_exists($this->content, '__toString')) + // If the content is a stringable object, we'll go ahead and call + // to toString method so that we can get the string content of + // the content object. Otherwise we'll just cast to string. + if (str_object($this->content)) { $this->content = $this->content->__toString(); } @@ -238,6 +178,11 @@ public function render() $this->content = (string) $this->content; } + // Once we have the string content, we can set the content on + // the HttpFoundation Response instance in preparation for + // sending it back to client browser when all is done. + $this->foundation->setContent($this->content); + return $this->content; } @@ -248,9 +193,9 @@ public function render() */ public function send() { - if ( ! headers_sent()) $this->send_headers(); + $this->foundation->prepare(Request::$foundation); - echo (string) $this->content; + $this->foundation->send(); } /** @@ -260,49 +205,9 @@ public function send() */ public function send_headers() { - // If the server is using FastCGI, we need to send a slightly different - // protocol and status header than we normally would. Otherwise it will - // not call any custom scripts setup to handle 404 responses. - // - // The status header will contain both the code and the status message, - // such as "OK" or "Not Found". For typical servers, the HTTP protocol - // will also be included with the status. - if (isset($_SERVER['FCGI_SERVER_VERSION'])) - { - header('Status: '.$this->status.' '.$this->message()); - } - else - { - header(Request::protocol().' '.$this->status.' '.$this->message()); - } + $this->foundation->prepare(Request::$foundation); - // If the content type was not set by the developer, we will set the - // header to a default value that indicates to the browser that the - // response is HTML and that it uses the default encoding. - if ( ! isset($this->headers['content-type'])) - { - $encoding = Config::get('application.encoding'); - - $this->header('content-type', 'text/html; charset='.$encoding); - } - - // Once the framework controlled headers have been sentm, we can - // simply iterate over the developer's headers and send each one - // back to the browser for the response. - foreach ($this->headers as $name => $value) - { - header("{$name}: {$value}", true); - } - } - - /** - * Get the status code message for the response. - * - * @return string - */ - public function message() - { - return static::$statuses[$this->status]; + $this->foundation->sendHeaders(); } /** @@ -314,7 +219,8 @@ public function message() */ public function header($name, $value) { - $this->headers[strtolower($name)] = $value; + $this->foundation->headers->set($name, $value); + return $this; } @@ -326,7 +232,8 @@ public function header($name, $value) */ public function status($status) { - $this->status = $status; + $this->foundation->setStatusCode($status); + return $this; }