response.php 8.86 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel;
2

3
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
4
use Symfony\Component\HttpFoundation\LaravelResponse as FoundationResponse;
5

6
class Response {
7 8

	/**
9
	 * The content of the response.
10
	 *
11
	 * @var mixed
12
	 */
13
	public $content;
14 15

	/**
16
	 * The Symfony HttpFoundation Response instance.
17
	 *
18
	 * @var HttpFoundation\Response
19
	 */
20
	public $foundation;
21 22 23 24 25 26 27

	/**
	 * Create a new response instance.
	 *
	 * @param  mixed  $content
	 * @param  int    $status
	 * @param  array  $headers
28 29
	 * @return void
	 */
30
	public function __construct($content, $status = 200, $headers = array())
31
	{
32
		$this->content = $content;
33 34

		$this->foundation = new FoundationResponse('', $status, $headers);
35 36 37 38 39
	}

	/**
	 * Create a new response instance.
	 *
40 41 42 43 44 45 46 47
	 * <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
48
	 *		return Response::make(json_encode($user), 200, array('header' => 'value'));
49 50
	 * </code>
	 *
51 52
	 * @param  mixed     $content
	 * @param  int       $status
53
	 * @param  array     $headers
54 55
	 * @return Response
	 */
56
	public static function make($content, $status = 200, $headers = array())
57
	{
58
		return new static($content, $status, $headers);
59 60 61
	}

	/**
62
	 * Create a new response instance containing a view.
63
	 *
64 65 66 67 68 69 70 71
	 * <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>
	 *
72 73
	 * @param  string    $view
	 * @param  array     $data
74
	 * @return Response
75
	 */
76
	public static function view($view, $data = array())
77
	{
78
		return new static(View::make($view, $data));
79 80 81
	}

	/**
82 83 84
	 * Create a new JSON response.
	 *
	 * <code>
85
	 *		// Create a response instance with JSON
86 87 88 89 90 91
	 *		return Response::json($data, 200, array('header' => 'value'));
	 * </code>
	 *
	 * @param  mixed     $data
	 * @param  int       $status
	 * @param  array     $headers
92
   * @param  int       $json_options
93 94
	 * @return Response
	 */
95
	public static function json($data, $status = 200, $headers = array(), $json_options = 0)
96
	{
97
		$headers['Content-Type'] = 'application/json; charset=utf-8';
98

99
		return new static(json_encode($data, $json_options), $status, $headers);
100
	}
101
	
102 103

	/**
104 105 106 107
	 * Create a new JSONP response.
	 *
	 * <code>
	 *		// Create a response instance with JSONP
108
	 *		return Response::jsonp('myFunctionCall', $data, 200, array('header' => 'value'));
109 110 111 112 113 114 115
	 * </code>
	 *
	 * @param  mixed     $data
	 * @param  int       $status
	 * @param  array     $headers
	 * @return Response
	 */
116
	public static function jsonp($callback, $data, $status = 200, $headers = array())
117 118 119
	{
		$headers['Content-Type'] = 'application/javascript; charset=utf-8';

120
		return new static($callback.'('.json_encode($data).');', $status, $headers);
121 122 123
	}

	/**
124 125 126 127 128 129 130
	 * 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>
	 *
Pascal Borreli committed
131
	 * @param  Eloquent|array   $data
132 133 134 135 136 137
	 * @param  int              $status
	 * @param  array            $headers
	 * @return Response
	 */
	public static function eloquent($data, $status = 200, $headers = array())
	{
138
		$headers['Content-Type'] = 'application/json; charset=utf-8';
139 140 141 142 143

		return new static(eloquent_to_json($data), $status, $headers);
	}

	/**
144 145 146 147
	 * Create a new error response instance.
	 *
	 * The response status code will be set using the specified code.
	 *
148
	 * The specified error should match a view in your views/error directory.
149
	 *
150 151 152 153 154 155 156 157
	 * <code>
	 *		// Create a 404 response
	 *		return Response::error('404');
	 *
	 *		// Create a 404 response with data
	 *		return Response::error('404', array('message' => 'Not Found'));
	 * </code>
	 *
158 159
	 * @param  int       $code
	 * @param  array     $data
160
	 * @return Response
161
	 */
162
	public static function error($code, $data = array())
163
	{
164
		return new static(View::make('error.'.$code, $data), $code);
165
	}
166

167 168 169
	/**
	 * Create a new download response instance.
	 *
170 171 172 173 174 175 176 177
	 * <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>
	 *
178 179 180 181 182
	 * @param  string    $path
	 * @param  string    $name
	 * @param  array     $headers
	 * @return Response
	 */
183
	public static function download($path, $name = null, $headers = array())
184 185 186
	{
		if (is_null($name)) $name = basename($path);

187 188 189
		// 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.
190
		$headers = array_merge(array(
191 192 193 194 195 196 197
			'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),
198 199
		), $headers);

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
		// 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);
221 222
	}

223
	/**
224
	 * Prepare a response from the given value.
225
	 *
226 227
	 * @param  mixed     $response
	 * @return Response
228
	 */
229
	public static function prepare($response)
230
	{
Taylor Otwell committed
231
		// We will need to force the response to be a string before closing
232
		// the session since the developer may be utilizing the session
233
		// within the view, and we can't age it until rendering.
Taylor Otwell committed
234 235 236 237
		if ( ! $response instanceof Response)
		{
			$response = new static($response);
		}
238

239 240 241 242
		return $response;
	}

	/**
243 244 245 246 247 248 249 250 251 252 253 254 255 256
	 * 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();
	}

	/**
257 258 259 260 261 262
	 * Convert the content of the Response to a string and return it.
	 *
	 * @return string
	 */
	public function render()
	{
263
		// If the content is a stringable object, we'll go ahead and call
Chris Berthe committed
264
		// the toString method so that we can get the string content of
265 266
		// the content object. Otherwise we'll just cast to string.
		if (str_object($this->content))
267
		{
268
			$this->content = $this->content->__toString();
269 270 271
		}
		else
		{
272
			$this->content = (string) $this->content;
273
		}
274

275 276 277
		// 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.
278 279
		$this->foundation->setContent($this->content);

280
		return $this->content;
281 282 283
	}

	/**
284 285
	 * Send all of the response headers to the browser.
	 *
286
	 * @return void
287
	 */
288
	public function send_headers()
289
	{
290
		$this->foundation->prepare(Request::foundation());
291

292
		$this->foundation->sendHeaders();
293 294 295
	}

	/**
296 297 298 299 300 301 302 303 304 305
	 * 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
306
		// the browser. This allows our cookies to be set easily.
307 308 309 310 311 312 313 314 315
		foreach (Cookie::$jar as $name => $cookie)
		{
			$config = array_values($cookie);

			$this->headers()->setCookie($ref->newInstanceArgs($config));
		}
	}

	/**
316
	 * Add a header to the array of response headers.
317
	 *
318 319
	 * @param  string    $name
	 * @param  string    $value
320 321
	 * @return Response
	 */
322
	public function header($name, $value)
323
	{
324 325
		$this->foundation->headers->set($name, $value);

326 327 328
		return $this;
	}

329
	/**
330 331 332 333 334 335 336 337 338 339
	 * Get the HttpFoundation Response headers.
	 *
	 * @return ResponseParameterBag
	 */
	public function headers()
	{
		return $this->foundation->headers;
	}

	/**
340
	 * Get / set the response status code.
341
	 *
342 343
	 * @param  int    $status
	 * @return mixed
344
	 */
345
	public function status($status = null)
346
	{
347 348 349 350 351 352 353
		if (is_null($status))
		{
			return $this->foundation->getStatusCode();
		}
		else
		{
			$this->foundation->setStatusCode($status);
354

355 356
			return $this;
		}
357 358
	}

359 360 361 362 363 364 365 366 367 368
	/**
	 * Render the response when cast to string
	 *
	 * @return string
	 */
	public function __toString()
	{
		return $this->render();
	}

369
}