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
734d5738
Commit
734d5738
authored
Feb 06, 2013
by
Victor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Application] Add the ability to send files
parent
22a8fea7
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
0 deletions
+35
-0
src/Silex/Application.php
src/Silex/Application.php
+22
-0
tests/Silex/Tests/ApplicationTest.php
tests/Silex/Tests/ApplicationTest.php
+13
-0
No files found.
src/Silex/Application.php
View file @
734d5738
...
@@ -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.
*
*
...
...
tests/Silex/Tests/ApplicationTest.php
View file @
734d5738
...
@@ -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
...
...
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