Commit 89766bef authored by Taylor Otwell's avatar Taylor Otwell

converting to httpfoundation response.

parent 573725ad
...@@ -176,4 +176,4 @@ foreach ($bundles as $bundle => $config) ...@@ -176,4 +176,4 @@ foreach ($bundles as $bundle => $config)
use Symfony\Component\HttpFoundation\Request as FoundationRequest; use Symfony\Component\HttpFoundation\Request as FoundationRequest;
Request::$request = FoundationRequest::createFromGlobals(); Request::$foundation = FoundationRequest::createFromGlobals();
\ No newline at end of file \ No newline at end of file
...@@ -361,6 +361,17 @@ function str_finish($value, $cap) ...@@ -361,6 +361,17 @@ function str_finish($value, $cap)
return rtrim($value, $cap).$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. * Get the root namespace of a given class.
* *
......
...@@ -7,7 +7,7 @@ class Request { ...@@ -7,7 +7,7 @@ class Request {
* *
* @var HttpFoundation\Request * @var HttpFoundation\Request
*/ */
public static $request; public static $foundation;
/** /**
* All of the route instances handling the request. * All of the route instances handling the request.
...@@ -78,7 +78,7 @@ class Request { ...@@ -78,7 +78,7 @@ class Request {
*/ */
public static function ip($default = '0.0.0.0') 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 @@ class Request { ...@@ -98,7 +98,7 @@ class Request {
*/ */
public static function accept() public static function accept()
{ {
return static::$request->getAcceptableContentTypes(); return static::$foundation->getAcceptableContentTypes();
} }
/** /**
...@@ -118,7 +118,7 @@ class Request { ...@@ -118,7 +118,7 @@ class Request {
*/ */
public static function secure() public static function secure()
{ {
return static::$request->isSecure(); return static::$foundation->isSecure();
} }
/** /**
...@@ -140,7 +140,7 @@ class Request { ...@@ -140,7 +140,7 @@ class Request {
*/ */
public static function ajax() public static function ajax()
{ {
return static::$request->isXmlHttpRequest(); return static::$foundation->isXmlHttpRequest();
} }
/** /**
...@@ -203,7 +203,7 @@ class Request { ...@@ -203,7 +203,7 @@ class Request {
*/ */
public static function __callStatic($method, $parameters) 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
<?php namespace Laravel; <?php namespace Laravel;
use Symfony\Component\HttpFoundation\Response as FoundationResponse;
class Response { class Response {
/** /**
...@@ -10,72 +12,11 @@ class Response { ...@@ -10,72 +12,11 @@ class Response {
public $content; public $content;
/** /**
* The HTTP status code of the response. * The Symfony HttpFoundation Response instance.
*
* @var int
*/
public $status = 200;
/**
* The response headers.
*
* @var array
*/
public $headers = array();
/**
* HTTP status codes.
* *
* @var array * @var HttpFoundation\Response
*/ */
public static $statuses = array( public $foundation;
100 => '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'
);
/** /**
* Create a new response instance. * Create a new response instance.
...@@ -87,9 +28,9 @@ class Response { ...@@ -87,9 +28,9 @@ class Response {
*/ */
public function __construct($content, $status = 200, $headers = array()) public function __construct($content, $status = 200, $headers = array())
{ {
$this->status = $status;
$this->content = $content; $this->content = $content;
$this->headers = $headers;
$this->foundation = new FoundationResponse('', $status, $headers);
} }
/** /**
...@@ -181,14 +122,14 @@ class Response { ...@@ -181,14 +122,14 @@ class Response {
if (is_null($name)) $name = basename($path); if (is_null($name)) $name = basename($path);
$headers = array_merge(array( $headers = array_merge(array(
'content-description' => 'File Transfer', 'Content-Description' => 'File Transfer',
'content-type' => File::mime(File::extension($path)), 'Content-Type' => File::mime(File::extension($path)),
'content-disposition' => 'attachment; filename="'.$name.'"', 'Content-Disposition' => 'attachment; filename="'.$name.'"',
'content-transfer-encoding' => 'binary', 'Content-Transfer-Encoding' => 'binary',
'expires' => 0, 'Expires' => 0,
'cache-control' => 'must-revalidate, post-check=0, pre-check=0', 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'pragma' => 'public', 'Pragma' => 'public',
'content-length' => File::size($path), 'Content-Length' => File::size($path),
), $headers); ), $headers);
return new static(File::get($path), 200, $headers); return new static(File::get($path), 200, $headers);
...@@ -205,18 +146,14 @@ class Response { ...@@ -205,18 +146,14 @@ class Response {
*/ */
public static function prepare($response) 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) if ( ! $response instanceof Response)
{ {
$response = new static($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(); $response->render();
return $response; return $response;
...@@ -229,7 +166,10 @@ class Response { ...@@ -229,7 +166,10 @@ class Response {
*/ */
public function render() 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(); $this->content = $this->content->__toString();
} }
...@@ -238,6 +178,11 @@ class Response { ...@@ -238,6 +178,11 @@ class Response {
$this->content = (string) $this->content; $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; return $this->content;
} }
...@@ -248,9 +193,9 @@ class Response { ...@@ -248,9 +193,9 @@ class Response {
*/ */
public function send() 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 @@ class Response { ...@@ -260,49 +205,9 @@ class Response {
*/ */
public function send_headers() public function send_headers()
{ {
// If the server is using FastCGI, we need to send a slightly different $this->foundation->prepare(Request::$foundation);
// 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());
}
// If the content type was not set by the developer, we will set the $this->foundation->sendHeaders();
// 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];
} }
/** /**
...@@ -314,7 +219,8 @@ class Response { ...@@ -314,7 +219,8 @@ class Response {
*/ */
public function header($name, $value) public function header($name, $value)
{ {
$this->headers[strtolower($name)] = $value; $this->foundation->headers->set($name, $value);
return $this; return $this;
} }
...@@ -326,7 +232,8 @@ class Response { ...@@ -326,7 +232,8 @@ class Response {
*/ */
public function status($status) public function status($status)
{ {
$this->status = $status; $this->foundation->setStatusCode($status);
return $this; return $this;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment