refactoring redirect and response classes.

This commit is contained in:
Taylor Otwell 2011-08-29 01:03:45 -05:00
parent f79dd1ba5d
commit 15cde60794
3 changed files with 73 additions and 45 deletions

View File

@ -16,6 +16,12 @@
}), }),
'laravel.download' => array('singleton' => true, 'resolver' => function($container)
{
return new Download($container->resolve('laravel.file'));
}),
'laravel.file' => array('singleton' => true, 'resolver' => function($container) 'laravel.file' => array('singleton' => true, 'resolver' => function($container)
{ {
return new File($container->resolve('laravel.config')->get('mimes')); return new File($container->resolve('laravel.config')->get('mimes'));
@ -57,6 +63,14 @@
}), }),
'laravel.response' => array('singleton' => true, 'resolver' => function($container)
{
require_once SYS_PATH.'response'.EXT;
return new Response_Factory($container->resolve('laravel.view'));
}),
'laravel.router' => array('singleton' => true, 'resolver' => function($container) 'laravel.router' => array('singleton' => true, 'resolver' => function($container)
{ {
return new Routing\Router($container->resolve('laravel.request'), require APP_PATH.'routes'.EXT, CONTROLLER_PATH); return new Routing\Router($container->resolve('laravel.request'), require APP_PATH.'routes'.EXT, CONTROLLER_PATH);

41
laravel/download.php Normal file
View File

@ -0,0 +1,41 @@
<?php namespace Laravel;
class Download extends Response {
/**
* Create a new download generator instance.
*
* @param File $file
* @return void
*/
public function __construct(File $file)
{
$this->file = $file;
}
/**
* Create a new download response instance.
*
* @param string $path
* @param string $name
* @return Response
*/
public function of($path, $name = null)
{
if (is_null($name)) $name = basename($path);
$response = parent::__construct($this->file->get($path));
$response->header('Content-Description', 'File Transfer');
$response->header('Content-Type', $this->file->mime($this->file->extension($path)));
$response->header('Content-Disposition', 'attachment; filename="'.$name.'"');
$response->header('Content-Transfer-Encoding', 'binary');
$response->header('Expires', 0);
$response->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
$response->header('Pragma', 'public');
$response->header('Content-Length', $this->file->size($path));
return $response;
}
}

View File

@ -9,23 +9,15 @@ class Response_Factory {
*/ */
private $view; private $view;
/**
* The file manager instance.
*
* @var File
*/
private $file;
/** /**
* Create a new response factory instance. * Create a new response factory instance.
* *
* @param File $file * @param File $file
* @return void * @return void
*/ */
public function __construct(View_Factory $view, File $file) public function __construct(View_Factory $view)
{ {
$this->view = $view; $this->view = $view;
$this->file = $file;
} }
/** /**
@ -41,36 +33,15 @@ public function make($content, $status = 200)
} }
/** /**
* Create a new download response instance. * Create a new response instance containing a view.
* *
* <code> * @param string $view
* // Return a download response for a given file * @param array $data
* return new Download('path/to/image.jpg');
*
* // Return a download response for a given file and assign a name
* return new Download('path/to/image.jpg', 'you.jpg');
* </code>
*
* @param string $path
* @param string $name
* @return Response * @return Response
*/ */
public function download($path, $name = null) public function view($view, $data = array())
{ {
if (is_null($name)) $name = basename($path); return new Response($this->view->make($view, $data));
$response = new Response($this->file->get($path));
$response->header('Content-Description', 'File Transfer');
$response->header('Content-Type', $this->file->mime($this->file->extension($path)));
$response->header('Content-Disposition', 'attachment; filename="'.$name.'"');
$response->header('Content-Transfer-Encoding', 'binary');
$response->header('Expires', 0);
$response->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
$response->header('Pragma', 'public');
$response->header('Content-Length', $this->file->size($path));
return $response;
} }
/** /**
@ -80,11 +51,6 @@ public function download($path, $name = null)
* *
* Note: The specified error code should correspond to a view in your views/error directory. * Note: The specified error code should correspond to a view in your views/error directory.
* *
* <code>
* // Return a 404 error response
* return new Error('404');
* </code>
*
* @param int $code * @param int $code
* @param array $data * @param array $data
* @return void * @return void
@ -233,11 +199,6 @@ public function send_headers()
/** /**
* Add a header to the response. * Add a header to the response.
* *
* <code>
* // Add a "location" header to a response
* $response->header('Location', 'http://google.com');
* </code>
*
* @param string $name * @param string $name
* @param string $value * @param string $value
* @return Response * @return Response
@ -248,4 +209,16 @@ public function header($name, $value)
return $this; return $this;
} }
/**
* Set the response status code.
*
* @param int $status
* @return Response
*/
public function status($status)
{
$this->status = $status;
return $this;
}
} }