diff --git a/laravel/config/container.php b/laravel/config/container.php index a36f16e5..426f4be4 100644 --- a/laravel/config/container.php +++ b/laravel/config/container.php @@ -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) { 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) { return new Routing\Router($container->resolve('laravel.request'), require APP_PATH.'routes'.EXT, CONTROLLER_PATH); diff --git a/laravel/download.php b/laravel/download.php new file mode 100644 index 00000000..0f0a6de9 --- /dev/null +++ b/laravel/download.php @@ -0,0 +1,41 @@ +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; + } + +} \ No newline at end of file diff --git a/laravel/response.php b/laravel/response.php index 683a27dc..7f43b2f5 100644 --- a/laravel/response.php +++ b/laravel/response.php @@ -9,23 +9,15 @@ class Response_Factory { */ private $view; - /** - * The file manager instance. - * - * @var File - */ - private $file; - /** * Create a new response factory instance. * * @param File $file * @return void */ - public function __construct(View_Factory $view, File $file) + public function __construct(View_Factory $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. * - * - * // Return a download response for a given file - * 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'); - * - * - * @param string $path - * @param string $name + * @param string $view + * @param array $data * @return Response */ - public function download($path, $name = null) + public function view($view, $data = array()) { - if (is_null($name)) $name = basename($path); - - $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; + return new Response($this->view->make($view, $data)); } /** @@ -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. * - * - * // Return a 404 error response - * return new Error('404'); - * - * * @param int $code * @param array $data * @return void @@ -233,11 +199,6 @@ public function send_headers() /** * Add a header to the response. * - * - * // Add a "location" header to a response - * $response->header('Location', 'http://google.com'); - * - * * @param string $name * @param string $value * @return Response @@ -248,4 +209,16 @@ public function header($name, $value) return $this; } + /** + * Set the response status code. + * + * @param int $status + * @return Response + */ + public function status($status) + { + $this->status = $status; + return $this; + } + } \ No newline at end of file