Commit 734d5738 authored by Victor's avatar Victor

[Application] Add the ability to send files

parent 22a8fea7
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
namespace Silex; namespace Silex;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpKernel\HttpKernel; use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface; use Symfony\Component\HttpKernel\TerminableInterface;
...@@ -423,6 +424,27 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte ...@@ -423,6 +424,27 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
return new JsonResponse($data, $status, $headers); return new JsonResponse($data, $status, $headers);
} }
/**
* Sends a file.
*
* @param \SplFileInfo|string $file The file to stream
* @param integer $status The response status code
* @param array $headers An array of response headers
* @param null|string $contentDisposition The type of Content-Disposition to set automatically with the filename
*
* @return BinaryFileResponse
*
* @throws \RuntimeException When the feature is not supported, before http-foundation v2.2
*/
public function sendFile($file, $status = 200, $headers = array(), $contentDisposition = null)
{
if (!class_exists('Symfony\Component\HttpFoundation\BinaryFileResponse')) {
throw new \RuntimeException('This feature is only supported as of Http Foundation 2.2.');
}
return new BinaryFileResponse($file, $status, $headers, true, $contentDisposition);
}
/** /**
* Mounts controllers under the given route prefix. * Mounts controllers under the given route prefix.
* *
......
...@@ -487,6 +487,19 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase ...@@ -487,6 +487,19 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$this->assertSame($app, $app->mount('/hello', $mounted)); $this->assertSame($app, $app->mount('/hello', $mounted));
} }
public function testSendFile()
{
$app = new Application();
try {
$response = $app->sendFile(__FILE__, 200, array('Content-Type: application/php'), null);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\BinaryFileResponse', $response);
$this->assertEquals(__FILE__, (string) $response->getFile());
} catch (\RuntimeException $e) {
$this->assertFalse(class_exists('Symfony\Component\HttpFoundation\BinaryFileResponse'));
}
}
} }
class FooController class FooController
......
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