Commit 73fe0b17 authored by Igor Wiedler's avatar Igor Wiedler

[stream] Add a nice API for streaming responses

parent 18292b7f
......@@ -464,14 +464,47 @@ Redirects
You can redirect to another page by returning a redirect response, which
you can create by calling the ``redirect`` method::
use Silex\Application;
$app->get('/', function (Silex\Application $app) {
$app->get('/', function () use ($app) {
return $app->redirect('/hello');
});
This will redirect from ``/`` to ``/hello``.
Streaming
---------
It's possible to create a streaming response, which is important in cases
when you cannot buffer the data being sent.
code-block:: php
$app->get('/images/{file}', function ($file) use ($app) {
if (!file_exists(__DIR__.'/images/'.$file)) {
return $app->abort(404, 'The image was not found.');
}
$stream = function () use ($file) {
readfile($file);
};
return $app->stream($stream, 200, array('Content-Type' => 'image/png'));
});
If you need to send chunks, make sure you call ``ob_flush`` and ``flush`` after
every chunk.
code-block:: php
$stream = function () {
$fh = fopen('http://www.example.com/', 'rb');
while (!feof($fh)) {
echo fread($fh, 1024);
ob_flush();
flush();
}
fclose($fh);
};
Security
--------
......
......@@ -29,6 +29,7 @@ use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
......@@ -299,6 +300,20 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
return new RedirectResponse($url, $status);
}
/**
* Creates a streaming response.
*
* @param mixed $callback A valid PHP callback
* @param integer $status The response status code
* @param array $headers An array of response headers
*
* @see Symfony\Component\HttpFoundation\StreamedResponse
*/
public function stream($callback = null, $status = 200, $headers = array())
{
return new StreamedResponse($callback, $status, $headers);
}
/**
* Escapes a text for HTML.
*
......
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
* Stream test cases.
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class StreamTest extends \PHPUnit_Framework_TestCase
{
public function testStreamReturnsStreamingResponse()
{
$app = new Application();
$response = $app->stream();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response);
$this->assertSame(false, $response->getContent());
}
public function testStreamActuallyStreams()
{
$i = 0;
$stream = function () use (&$i) {
$i++;
};
$app = new Application();
$response = $app->stream($stream);
$this->assertEquals(0, $i);
$request = Request::create('/stream');
$response->prepare($request);
$response->sendContent();
$this->assertEquals(1, $i);
}
}
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