Files
spa-api/laravel/response.php

347 lines
8.2 KiB
PHP
Raw Normal View History

2011-08-18 19:56:29 -05:00
<?php namespace Laravel;
2011-06-08 23:45:08 -05:00
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\LaravelResponse as FoundationResponse;
2012-03-28 17:11:55 -05:00
class Response {
/**
* The content of the response.
*
* @var mixed
2011-08-26 21:42:04 -05:00
*/
2011-09-22 00:43:42 -05:00
public $content;
2011-09-08 17:49:16 -05:00
/**
2012-03-28 17:11:55 -05:00
* The Symfony HttpFoundation Response instance.
2011-08-26 21:42:04 -05:00
*
2012-03-28 17:11:55 -05:00
* @var HttpFoundation\Response
*/
2012-03-28 17:11:55 -05:00
public $foundation;
/**
* Create a new response instance.
*
* @param mixed $content
* @param int $status
* @param array $headers
2011-08-26 21:42:04 -05:00
* @return void
*/
public function __construct($content, $status = 200, $headers = array())
2011-08-26 21:42:04 -05:00
{
$this->content = $content;
2012-03-28 17:11:55 -05:00
$this->foundation = new FoundationResponse('', $status, $headers);
2011-08-26 21:42:04 -05:00
}
/**
* Create a new response instance.
*
2011-09-22 00:25:48 -05:00
* <code>
* // Create a response instance with string content
* return Response::make(json_encode($user));
*
* // Create a response instance with a given status
* return Response::make('Not Found', 404);
*
* // Create a response with some custom headers
2012-01-16 13:59:24 -06:00
* return Response::make(json_encode($user), 200, array('header' => 'value'));
2011-09-22 00:25:48 -05:00
* </code>
*
2011-08-26 21:42:04 -05:00
* @param mixed $content
* @param int $status
2011-08-31 00:07:45 -05:00
* @param array $headers
2011-08-26 21:42:04 -05:00
* @return Response
*/
public static function make($content, $status = 200, $headers = array())
2011-08-26 21:42:04 -05:00
{
return new static($content, $status, $headers);
2011-08-26 21:42:04 -05:00
}
/**
* Create a new response instance containing a view.
2011-08-26 21:42:04 -05:00
*
2011-09-22 00:25:48 -05:00
* <code>
* // Create a response instance with a view
* return Response::view('home.index');
*
* // Create a response instance with a view and data
* return Response::view('home.index', array('name' => 'Taylor'));
* </code>
*
* @param string $view
* @param array $data
2011-08-26 21:42:04 -05:00
* @return Response
*/
public static function view($view, $data = array())
2011-08-26 21:42:04 -05:00
{
2011-10-05 18:32:48 -05:00
return new static(View::make($view, $data));
2011-08-26 21:42:04 -05:00
}
2012-05-02 08:35:26 -05:00
/**
* Create a new JSON response.
*
* <code>
2012-05-02 08:50:37 -05:00
* // Create a response instance with JSON
2012-05-02 08:35:26 -05:00
* return Response::json($data, 200, array('header' => 'value'));
* </code>
*
* @param mixed $data
* @param int $status
* @param array $headers
* @return Response
*/
public static function json($data, $status = 200, $headers = array())
{
2012-07-07 02:17:12 +03:00
$headers['Content-Type'] = 'application/json; charset=utf-8';
2012-05-02 08:35:26 -05:00
return new static(json_encode($data), $status, $headers);
}
2012-05-02 08:50:37 -05:00
/**
* Create a new response of JSON'd Eloquent models.
*
* <code>
* // Create a new response instance with Eloquent models
* return Response::eloquent($data, 200, array('header' => 'value'));
* </code>
*
2012-07-24 09:31:27 +00:00
* @param Eloquent|array $data
2012-05-02 08:50:37 -05:00
* @param int $status
* @param array $headers
* @return Response
*/
public static function eloquent($data, $status = 200, $headers = array())
{
2012-07-07 02:17:12 +03:00
$headers['Content-Type'] = 'application/json; charset=utf-8';
2012-05-02 08:50:37 -05:00
return new static(eloquent_to_json($data), $status, $headers);
}
2011-08-26 21:42:04 -05:00
/**
* Create a new error response instance.
*
* The response status code will be set using the specified code.
*
2012-01-16 13:59:24 -06:00
* The specified error should match a view in your views/error directory.
2011-08-26 21:42:04 -05:00
*
2011-09-22 00:25:48 -05:00
* <code>
* // Create a 404 response
* return Response::error('404');
*
* // Create a 404 response with data
* return Response::error('404', array('message' => 'Not Found'));
* </code>
*
2011-08-26 21:42:04 -05:00
* @param int $code
* @param array $data
2011-09-08 17:49:16 -05:00
* @return Response
2011-08-26 21:42:04 -05:00
*/
public static function error($code, $data = array())
2011-08-26 21:42:04 -05:00
{
return new static(View::make('error.'.$code, $data), $code);
2011-08-26 21:42:04 -05:00
}
2011-09-08 17:49:16 -05:00
/**
* Create a new download response instance.
*
2011-09-22 00:25:48 -05:00
* <code>
* // Create a download response to a given file
* return Response::download('path/to/file.jpg');
*
* // Create a download response with a given file name
* return Response::download('path/to/file.jpg', 'your_file.jpg');
* </code>
*
2011-09-08 17:49:16 -05:00
* @param string $path
* @param string $name
* @param array $headers
* @return Response
*/
public static function download($path, $name = null, $headers = array())
2011-09-08 17:49:16 -05:00
{
if (is_null($name)) $name = basename($path);
// We'll set some sensible default headers, but merge the array given to
// us so that the developer has the chance to override any of these
// default headers with header values of their own liking.
2011-09-08 17:49:16 -05:00
$headers = array_merge(array(
2012-03-28 17:11:55 -05:00
'Content-Description' => 'File Transfer',
'Content-Type' => File::mime(File::extension($path)),
'Content-Transfer-Encoding' => 'binary',
'Expires' => 0,
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Pragma' => 'public',
'Content-Length' => File::size($path),
2011-09-08 17:49:16 -05:00
), $headers);
// Once we create the response, we need to set the content disposition
// header on the response based on the file's name. We'll pass this
// off to the HttpFoundation and let it create the header text.
$response = new static(File::get($path), 200, $headers);
$d = $response->disposition($name);
return $response->header('Content-Disposition', $d);
}
/**
* Create the proper Content-Disposition header.
*
* @param string $file
* @return string
*/
public function disposition($file)
{
$type = ResponseHeaderBag::DISPOSITION_ATTACHMENT;
return $this->foundation->headers->makeDisposition($type, $file);
2011-09-08 17:49:16 -05:00
}
/**
2012-01-16 13:59:24 -06:00
* Prepare a response from the given value.
*
2012-01-16 13:59:24 -06:00
* @param mixed $response
* @return Response
*/
2012-01-16 13:59:24 -06:00
public static function prepare($response)
{
2012-04-04 11:19:52 -05:00
// We will need to force the response to be a string before closing
2012-04-01 15:33:35 -05:00
// the session since the developer may be utilizing the session
2012-03-28 17:11:55 -05:00
// within the view, and we can't age it until rendering.
2012-02-18 13:37:02 -06:00
if ( ! $response instanceof Response)
{
$response = new static($response);
}
2012-01-16 13:59:24 -06:00
2012-01-25 14:57:43 -06:00
return $response;
}
/**
* Send the headers and content of the response to the browser.
*
* @return void
*/
public function send()
{
$this->cookies();
$this->foundation->prepare(Request::foundation());
$this->foundation->send();
}
2012-01-25 14:57:43 -06:00
/**
* Convert the content of the Response to a string and return it.
*
* @return string
*/
public function render()
{
2012-03-28 17:11:55 -05:00
// If the content is a stringable object, we'll go ahead and call
// the toString method so that we can get the string content of
2012-03-28 17:11:55 -05:00
// the content object. Otherwise we'll just cast to string.
if (str_object($this->content))
{
2012-01-25 14:57:43 -06:00
$this->content = $this->content->__toString();
}
else
{
2012-01-25 14:57:43 -06:00
$this->content = (string) $this->content;
}
2012-01-16 13:59:24 -06:00
// Once we obtain the string content, we can set the content on
// the HttpFoundation's Response instance in preparation for
// sending it back to client browser when all is finished.
2012-03-28 17:11:55 -05:00
$this->foundation->setContent($this->content);
2012-01-25 14:57:43 -06:00
return $this->content;
}
2011-06-08 23:45:08 -05:00
/**
* Send all of the response headers to the browser.
*
2011-06-14 17:27:11 -05:00
* @return void
2011-06-08 23:45:08 -05:00
*/
2011-11-22 18:00:17 -06:00
public function send_headers()
2011-06-08 23:45:08 -05:00
{
$this->foundation->prepare(Request::foundation());
2012-01-16 13:59:24 -06:00
2012-03-28 17:11:55 -05:00
$this->foundation->sendHeaders();
2012-01-16 13:59:24 -06:00
}
/**
* Set the cookies on the HttpFoundation Response.
*
* @return void
*/
protected function cookies()
{
$ref = new \ReflectionClass('Symfony\Component\HttpFoundation\Cookie');
// All of the cookies for the response are actually stored on the
// Cookie class until we're ready to send the response back to
2012-04-23 13:36:25 -05:00
// the browser. This allows our cookies to be set easily.
foreach (Cookie::$jar as $name => $cookie)
{
$config = array_values($cookie);
$this->headers()->setCookie($ref->newInstanceArgs($config));
}
}
2012-01-16 13:59:24 -06:00
/**
* Add a header to the array of response headers.
2011-09-22 00:25:48 -05:00
*
2011-06-14 17:27:11 -05:00
* @param string $name
* @param string $value
2011-06-08 23:45:08 -05:00
* @return Response
*/
2011-06-14 17:27:11 -05:00
public function header($name, $value)
2011-06-08 23:45:08 -05:00
{
2012-03-28 17:11:55 -05:00
$this->foundation->headers->set($name, $value);
2011-06-08 23:45:08 -05:00
return $this;
}
/**
* Get the HttpFoundation Response headers.
*
* @return ResponseParameterBag
*/
public function headers()
{
return $this->foundation->headers;
}
/**
2012-04-23 13:36:25 -05:00
* Get / set the response status code.
*
2012-04-23 13:36:25 -05:00
* @param int $status
* @return mixed
*/
2012-04-23 13:36:25 -05:00
public function status($status = null)
{
2012-04-23 13:36:25 -05:00
if (is_null($status))
{
return $this->foundation->getStatusCode();
}
else
{
$this->foundation->setStatusCode($status);
2012-03-28 17:11:55 -05:00
2012-04-23 13:36:25 -05:00
return $this;
}
}
/**
* Render the response when cast to string
*
* @return string
*/
public function __toString()
{
return $this->render();
}
}