Skip to content

Commit e457627

Browse files
committed
feature #39128 [HttpFoundation] Deprecate BinaryFileResponse::create() (derrabus)
This PR was merged into the 5.2 branch. Discussion ---------- [HttpFoundation] Deprecate BinaryFileResponse::create() | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | N/A | License | MIT | Doc PR | N/A Follows #34771. We've deprecated the static `::create()` methods on all response classes but `BinaryFileResponse`. This PR proposes to fix this inconsistency. Commits ------- 9ce2e86207 [HttpFoundation] Deprecate BinaryFileResponse::create().
2 parents ba0aeb1 + 3272231 commit e457627

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

BinaryFileResponse.php

+4
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,13 @@ public function __construct($file, int $status = 200, array $headers = [], bool
6565
* @param bool $autoLastModified Whether the Last-Modified header should be automatically set
6666
*
6767
* @return static
68+
*
69+
* @deprecated since Symfony 5.2, use __construct() instead.
6870
*/
6971
public static function create($file = null, int $status = 200, array $headers = [], bool $public = true, string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)
7072
{
73+
trigger_deprecation('symfony/http-foundation', '5.2', 'The "%s()" method is deprecated, use "new %s()" instead.', __METHOD__, static::class);
74+
7175
return new static($file, $status, $headers, $public, $contentDisposition, $autoEtag, $autoLastModified);
7276
}
7377

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
* added `RateLimiter\RequestRateLimiterInterface` and `RateLimiter\AbstractRequestRateLimiter`
1313
* deprecated not passing a `Closure` together with `FILTER_CALLBACK` to `ParameterBag::filter()`; wrap your filter in a closure instead.
1414
* Deprecated the `Request::HEADER_X_FORWARDED_ALL` constant, use either `HEADER_X_FORWARDED_FOR | HEADER_X_FORWARDED_HOST | HEADER_X_FORWARDED_PORT | HEADER_X_FORWARDED_PROTO` or `HEADER_X_FORWARDED_AWS_ELB` or `HEADER_X_FORWARDED_TRAEFIK` constants instead.
15+
* Deprecated `BinaryFileResponse::create()`, use `__construct()` instead
1516

1617

1718
5.1.0

Tests/BinaryFileResponseTest.php

+33-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpFoundation\Tests;
1313

14+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1415
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1516
use Symfony\Component\HttpFoundation\File\Stream;
1617
use Symfony\Component\HttpFoundation\Request;
@@ -19,6 +20,8 @@
1920

2021
class BinaryFileResponseTest extends ResponseTestCase
2122
{
23+
use ExpectDeprecationTrait;
24+
2225
public function testConstruction()
2326
{
2427
$file = __DIR__.'/../README.md';
@@ -29,6 +32,26 @@ public function testConstruction()
2932
$this->assertTrue($response->headers->has('Last-Modified'));
3033
$this->assertFalse($response->headers->has('Content-Disposition'));
3134

35+
$response = new BinaryFileResponse($file, 404, [], true, ResponseHeaderBag::DISPOSITION_INLINE);
36+
$this->assertEquals(404, $response->getStatusCode());
37+
$this->assertFalse($response->headers->has('ETag'));
38+
$this->assertEquals('inline; filename=README.md', $response->headers->get('Content-Disposition'));
39+
}
40+
41+
/**
42+
* @group legacy
43+
*/
44+
public function testConstructionLegacy()
45+
{
46+
$file = __DIR__.'/../README.md';
47+
$this->expectDeprecation('Since symfony/http-foundation 5.2: The "Symfony\Component\HttpFoundation\BinaryFileResponse::create()" method is deprecated, use "new Symfony\Component\HttpFoundation\BinaryFileResponse()" instead.');
48+
$response = BinaryFileResponse::create($file, 404, ['X-Header' => 'Foo'], true, null, true, true);
49+
$this->assertEquals(404, $response->getStatusCode());
50+
$this->assertEquals('Foo', $response->headers->get('X-Header'));
51+
$this->assertTrue($response->headers->has('ETag'));
52+
$this->assertTrue($response->headers->has('Last-Modified'));
53+
$this->assertFalse($response->headers->has('Content-Disposition'));
54+
3255
$response = BinaryFileResponse::create($file, 404, [], true, ResponseHeaderBag::DISPOSITION_INLINE);
3356
$this->assertEquals(404, $response->getStatusCode());
3457
$this->assertFalse($response->headers->has('ETag'));
@@ -83,7 +106,7 @@ public function testSetContentDispositionGeneratesSafeFallbackFilenameForWrongly
83106
*/
84107
public function testRequests($requestRange, $offset, $length, $responseRange)
85108
{
86-
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();
109+
$response = (new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']))->setAutoEtag();
87110

88111
// do a request to get the ETag
89112
$request = Request::create('/');
@@ -115,7 +138,7 @@ public function testRequests($requestRange, $offset, $length, $responseRange)
115138
*/
116139
public function testRequestsWithoutEtag($requestRange, $offset, $length, $responseRange)
117140
{
118-
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
141+
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
119142

120143
// do a request to get the LastModified
121144
$request = Request::create('/');
@@ -156,7 +179,7 @@ public function provideRanges()
156179
public function testRangeRequestsWithoutLastModifiedDate()
157180
{
158181
// prevent auto last modified
159-
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'], true, null, false, false);
182+
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'], true, null, false, false);
160183

161184
// prepare a request for a range of the testing file
162185
$request = Request::create('/');
@@ -177,7 +200,7 @@ public function testRangeRequestsWithoutLastModifiedDate()
177200
*/
178201
public function testFullFileRequests($requestRange)
179202
{
180-
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();
203+
$response = (new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']))->setAutoEtag();
181204

182205
// prepare a request for a range of the testing file
183206
$request = Request::create('/');
@@ -213,7 +236,7 @@ public function testRangeOnPostMethod()
213236
{
214237
$request = Request::create('/', 'POST');
215238
$request->headers->set('Range', 'bytes=10-20');
216-
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
239+
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
217240

218241
$file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
219242
$data = fread($file, 35);
@@ -231,7 +254,7 @@ public function testRangeOnPostMethod()
231254

232255
public function testUnpreparedResponseSendsFullFile()
233256
{
234-
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200);
257+
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200);
235258

236259
$data = file_get_contents(__DIR__.'/File/Fixtures/test.gif');
237260

@@ -247,7 +270,7 @@ public function testUnpreparedResponseSendsFullFile()
247270
*/
248271
public function testInvalidRequests($requestRange)
249272
{
250-
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();
273+
$response = (new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']))->setAutoEtag();
251274

252275
// prepare a request for a range of the testing file
253276
$request = Request::create('/');
@@ -278,7 +301,7 @@ public function testXSendfile($file)
278301
$request->headers->set('X-Sendfile-Type', 'X-Sendfile');
279302

280303
BinaryFileResponse::trustXSendfileTypeHeader();
281-
$response = BinaryFileResponse::create($file, 200, ['Content-Type' => 'application/octet-stream']);
304+
$response = new BinaryFileResponse($file, 200, ['Content-Type' => 'application/octet-stream']);
282305
$response->prepare($request);
283306

284307
$this->expectOutputString('');
@@ -338,7 +361,7 @@ public function testDeleteFileAfterSend()
338361
public function testAcceptRangeOnUnsafeMethods()
339362
{
340363
$request = Request::create('/', 'POST');
341-
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
364+
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
342365
$response->prepare($request);
343366

344367
$this->assertEquals('none', $response->headers->get('Accept-Ranges'));
@@ -347,7 +370,7 @@ public function testAcceptRangeOnUnsafeMethods()
347370
public function testAcceptRangeNotOverriden()
348371
{
349372
$request = Request::create('/', 'POST');
350-
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
373+
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
351374
$response->headers->set('Accept-Ranges', 'foo');
352375
$response->prepare($request);
353376

0 commit comments

Comments
 (0)