Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
73fe0b17
Commit
73fe0b17
authored
Dec 31, 2011
by
Igor Wiedler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[stream] Add a nice API for streaming responses
parent
18292b7f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
105 additions
and
3 deletions
+105
-3
doc/usage.rst
doc/usage.rst
+36
-3
src/Silex/Application.php
src/Silex/Application.php
+15
-0
tests/Silex/Tests/StreamTest.php
tests/Silex/Tests/StreamTest.php
+54
-0
No files found.
doc/usage.rst
View file @
73fe0b17
...
@@ -464,14 +464,47 @@ Redirects
...
@@ -464,14 +464,47 @@ Redirects
You can redirect to another page by returning a redirect response, which
You can redirect to another page by returning a redirect response, which
you can create by calling the ``redirect`` method::
you can create by calling the ``redirect`` method::
use Silex\Application;
$app->get('/', function () use ($app) {
$app->get('/', function (Silex\Application $app) {
return $app->redirect('/hello');
return $app->redirect('/hello');
});
});
This will redirect from ``/`` to ``/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
Security
--------
--------
...
...
src/Silex/Application.php
View file @
73fe0b17
...
@@ -29,6 +29,7 @@ use Symfony\Component\HttpKernel\KernelEvents;
...
@@ -29,6 +29,7 @@ use Symfony\Component\HttpKernel\KernelEvents;
use
Symfony\Component\HttpFoundation\Request
;
use
Symfony\Component\HttpFoundation\Request
;
use
Symfony\Component\HttpFoundation\Response
;
use
Symfony\Component\HttpFoundation\Response
;
use
Symfony\Component\HttpFoundation\RedirectResponse
;
use
Symfony\Component\HttpFoundation\RedirectResponse
;
use
Symfony\Component\HttpFoundation\StreamedResponse
;
use
Symfony\Component\EventDispatcher\Event
;
use
Symfony\Component\EventDispatcher\Event
;
use
Symfony\Component\EventDispatcher\EventDispatcher
;
use
Symfony\Component\EventDispatcher\EventDispatcher
;
use
Symfony\Component\EventDispatcher\EventSubscriberInterface
;
use
Symfony\Component\EventDispatcher\EventSubscriberInterface
;
...
@@ -299,6 +300,20 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
...
@@ -299,6 +300,20 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
return
new
RedirectResponse
(
$url
,
$status
);
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.
* Escapes a text for HTML.
*
*
...
...
tests/Silex/Tests/StreamTest.php
0 → 100644
View file @
73fe0b17
<?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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment