Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d9a8499

Browse files
dfridrichfabpot
authored andcommitted
[FrameworkBundle] Add file helper to Controller
1 parent 0b67fa3 commit d9a8499

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313

1414
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1515
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
16+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
17+
use Symfony\Component\HttpFoundation\File\File;
1618
use Symfony\Component\HttpFoundation\JsonResponse;
1719
use Symfony\Component\HttpFoundation\Response;
1820
use Symfony\Component\HttpFoundation\RedirectResponse;
21+
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
1922
use Symfony\Component\HttpFoundation\StreamedResponse;
2023
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2124
use Symfony\Component\HttpKernel\HttpKernelInterface;
@@ -123,6 +126,23 @@ protected function json($data, $status = 200, $headers = array(), $context = arr
123126
return new JsonResponse($data, $status, $headers);
124127
}
125128

129+
/**
130+
* Returns a BinaryFileResponse object with original or customized file name and disposition header.
131+
*
132+
* @param File|string $file File object or path to file to be sent as response
133+
* @param string|null $fileName File name to be sent to response or null (will use original file name)
134+
* @param string $disposition Disposition of response ("attachment" is default, other type is "inline")
135+
*
136+
* @return BinaryFileResponse
137+
*/
138+
protected function file($file, $fileName = null, $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT)
139+
{
140+
$response = new BinaryFileResponse($file);
141+
$response->setContentDisposition($disposition, $fileName === null ? $response->getFile()->getFileName() : $fileName);
142+
143+
return $response;
144+
}
145+
126146
/**
127147
* Adds a flash message to the current session for type.
128148
*

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1515
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
1616
use Symfony\Component\DependencyInjection\ContainerInterface;
17+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
18+
use Symfony\Component\HttpFoundation\File\File;
1719
use Symfony\Component\HttpFoundation\JsonResponse;
1820
use Symfony\Component\HttpFoundation\Request;
1921
use Symfony\Component\HttpFoundation\RequestStack;
2022
use Symfony\Component\HttpFoundation\Response;
23+
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
2124
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
2225
use Symfony\Component\HttpFoundation\StreamedResponse;
2326
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@@ -209,6 +212,126 @@ public function testJsonWithSerializerContextOverride()
209212
$this->assertEquals('{}', $response->getContent());
210213
}
211214

215+
public function testFile()
216+
{
217+
/* @var ContainerInterface $container */
218+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
219+
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
220+
$container->set('kernel', $kernel);
221+
222+
$controller = new TestController();
223+
$controller->setContainer($container);
224+
225+
/* @var BinaryFileResponse $response */
226+
$response = $controller->file(new File(__FILE__));
227+
$this->assertInstanceOf(BinaryFileResponse::class, $response);
228+
$this->assertSame(200, $response->getStatusCode());
229+
if ($response->headers->get('content-type')) {
230+
$this->assertSame('text/x-php', $response->headers->get('content-type'));
231+
}
232+
$this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition'));
233+
$this->assertContains(basename(__FILE__), $response->headers->get('content-disposition'));
234+
}
235+
236+
public function testFileAsInline()
237+
{
238+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
239+
$controller = new TestController();
240+
$controller->setContainer($container);
241+
242+
/* @var BinaryFileResponse $response */
243+
$response = $controller->file(new File(__FILE__), null, ResponseHeaderBag::DISPOSITION_INLINE);
244+
245+
$this->assertInstanceOf(BinaryFileResponse::class, $response);
246+
$this->assertSame(200, $response->getStatusCode());
247+
if ($response->headers->get('content-type')) {
248+
$this->assertSame('text/x-php', $response->headers->get('content-type'));
249+
}
250+
$this->assertContains(ResponseHeaderBag::DISPOSITION_INLINE, $response->headers->get('content-disposition'));
251+
$this->assertContains(basename(__FILE__), $response->headers->get('content-disposition'));
252+
}
253+
254+
public function testFileWithOwnFileName()
255+
{
256+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
257+
$controller = new TestController();
258+
$controller->setContainer($container);
259+
260+
/* @var BinaryFileResponse $response */
261+
$fileName = 'test.php';
262+
$response = $controller->file(new File(__FILE__), $fileName);
263+
264+
$this->assertInstanceOf(BinaryFileResponse::class, $response);
265+
$this->assertSame(200, $response->getStatusCode());
266+
if ($response->headers->get('content-type')) {
267+
$this->assertSame('text/x-php', $response->headers->get('content-type'));
268+
}
269+
$this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition'));
270+
$this->assertContains($fileName, $response->headers->get('content-disposition'));
271+
}
272+
273+
public function testFileWithOwnFileNameAsInline()
274+
{
275+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
276+
$controller = new TestController();
277+
$controller->setContainer($container);
278+
279+
/* @var BinaryFileResponse $response */
280+
$fileName = 'test.php';
281+
$response = $controller->file(new File(__FILE__), $fileName, ResponseHeaderBag::DISPOSITION_INLINE);
282+
283+
$this->assertInstanceOf(BinaryFileResponse::class, $response);
284+
$this->assertSame(200, $response->getStatusCode());
285+
if ($response->headers->get('content-type')) {
286+
$this->assertSame('text/x-php', $response->headers->get('content-type'));
287+
}
288+
$this->assertContains(ResponseHeaderBag::DISPOSITION_INLINE, $response->headers->get('content-disposition'));
289+
$this->assertContains($fileName, $response->headers->get('content-disposition'));
290+
}
291+
292+
public function testFileFromPath()
293+
{
294+
$controller = new TestController();
295+
296+
/* @var BinaryFileResponse $response */
297+
$response = $controller->file(__FILE__);
298+
299+
$this->assertInstanceOf(BinaryFileResponse::class, $response);
300+
$this->assertSame(200, $response->getStatusCode());
301+
if ($response->headers->get('content-type')) {
302+
$this->assertSame('text/x-php', $response->headers->get('content-type'));
303+
}
304+
$this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition'));
305+
$this->assertContains(basename(__FILE__), $response->headers->get('content-disposition'));
306+
}
307+
308+
public function testFileFromPathWithCustomizedFileName()
309+
{
310+
$controller = new TestController();
311+
312+
/* @var BinaryFileResponse $response */
313+
$response = $controller->file(__FILE__, 'test.php');
314+
315+
$this->assertInstanceOf(BinaryFileResponse::class, $response);
316+
$this->assertSame(200, $response->getStatusCode());
317+
if ($response->headers->get('content-type')) {
318+
$this->assertSame('text/x-php', $response->headers->get('content-type'));
319+
}
320+
$this->assertContains(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition'));
321+
$this->assertContains('test.php', $response->headers->get('content-disposition'));
322+
}
323+
324+
/**
325+
* @expectedException \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
326+
*/
327+
public function testFileWhichDoesNotExist()
328+
{
329+
$controller = new TestController();
330+
331+
/* @var BinaryFileResponse $response */
332+
$response = $controller->file('some-file.txt', 'test.php');
333+
}
334+
212335
public function testIsGranted()
213336
{
214337
$authorizationChecker = $this->getMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
@@ -494,6 +617,11 @@ public function json($data, $status = 200, $headers = array(), $context = array(
494617
return parent::json($data, $status, $headers, $context);
495618
}
496619

620+
public function file($file, $fileName = null, $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT)
621+
{
622+
return parent::file($file, $fileName, $disposition);
623+
}
624+
497625
public function isGranted($attributes, $object = null)
498626
{
499627
return parent::isGranted($attributes, $object);

0 commit comments

Comments
 (0)