Skip to content

Deprecate *Response::create() methods #34771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions UPGRADE-5.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ FrameworkBundle
* Marked `MicroKernelTrait::configureRoutes()` as `@internal` and `@final`.
* Deprecated not overriding `MicroKernelTrait::configureRouting()`.

HttpFoundation
--------------

* Deprecate `Response::create()`, `JsonResponse::create()`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be Deprecated for consistency (all messages are using the past in the upgrade guide)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm slowly moving to use more "modern" message patterns as use everywhere else.

`RedirectResponse::create()`, and `StreamedResponse::create()` methods (use
`__construct()` instead)

Routing
-------

Expand Down
7 changes: 7 additions & 0 deletions UPGRADE-6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ FrameworkBundle
* Removed `MicroKernelTrait::configureRoutes()`.
* Made `MicroKernelTrait::configureRouting()` abstract.

HttpFoundation
--------------

* Removed `Response::create()`, `JsonResponse::create()`,
`RedirectResponse::create()`, and `StreamedResponse::create()` methods (use
`__construct()` instead)

Routing
-------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ConcreteMicroKernel extends Kernel implements EventSubscriberInterface
public function onKernelException(ExceptionEvent $event)
{
if ($event->getThrowable() instanceof Danger) {
$event->setResponse(Response::create('It\'s dangerous to go alone. Take this ⚔'));
$event->setResponse(new Response('It\'s dangerous to go alone. Take this ⚔'));
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
CHANGELOG
=========

5.1.0
-----

* Deprecate `Response::create()`, `JsonResponse::create()`,
`RedirectResponse::create()`, and `StreamedResponse::create()` methods (use
`__construct()` instead)

5.0.0
-----

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpFoundation/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ public function __construct($data = null, int $status = 200, array $headers = []
* @param array $headers An array of response headers
*
* @return static
*
* @deprecated since Symfony 5.1, use __construct() instead.
*/
public static function create($data = null, int $status = 200, array $headers = [])
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 5.1; use __construct() instead.', __METHOD__), E_USER_DEPRECATED);

return new static($data, $status, $headers);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpFoundation/RedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ public function __construct(string $url, int $status = 302, array $headers = [])
* @param string $url The URL to redirect to
*
* @return static
*
* @deprecated since Symfony 5.1, use __construct() instead.
*/
public static function create($url = '', int $status = 302, array $headers = [])
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 5.1; use __construct() instead.', __METHOD__), E_USER_DEPRECATED);

return new static($url, $status, $headers);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpFoundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,13 @@ public function __construct(?string $content = '', int $status = 200, array $hea
* ->setSharedMaxAge(300);
*
* @return static
*
* @deprecated since Symfony 5.1, use __construct() instead.
*/
public static function create(?string $content = '', int $status = 200, array $headers = [])
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 5.1; use __construct() instead.', __METHOD__), E_USER_DEPRECATED);

return new static($content, $status, $headers);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpFoundation/StreamedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ public function __construct(callable $callback = null, int $status = 200, array
* @param callable|null $callback A valid PHP callback or null to set it later
*
* @return static
*
* @deprecated since Symfony 5.1, use __construct() instead.
*/
public static function create($callback = null, int $status = 200, array $headers = [])
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 5.1; use __construct() instead.', __METHOD__), E_USER_DEPRECATED);

return new static($callback, $status, $headers);
}

Expand Down
35 changes: 31 additions & 4 deletions src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public function testSetJson()
$this->assertEquals('true', $response->getContent());
}

/**
* @group legacy
*/
public function testCreate()
{
$response = JsonResponse::create(['foo' => 'bar'], 204);
Expand All @@ -99,27 +102,39 @@ public function testCreate()
$this->assertEquals(204, $response->getStatusCode());
}

/**
* @group legacy
*/
public function testStaticCreateEmptyJsonObject()
{
$response = JsonResponse::create();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertSame('{}', $response->getContent());
}

/**
* @group legacy
*/
public function testStaticCreateJsonArray()
{
$response = JsonResponse::create([0, 1, 2, 3]);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertSame('[0,1,2,3]', $response->getContent());
}

/**
* @group legacy
*/
public function testStaticCreateJsonObject()
{
$response = JsonResponse::create(['foo' => 'bar']);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertSame('{"foo":"bar"}', $response->getContent());
}

/**
* @group legacy
*/
public function testStaticCreateWithSimpleTypes()
{
$response = JsonResponse::create('foo');
Expand All @@ -140,25 +155,37 @@ public function testStaticCreateWithSimpleTypes()
$this->assertSame('true', $response->getContent());
}

/**
* @group legacy
*/
public function testStaticCreateWithCustomStatus()
{
$response = JsonResponse::create([], 202);
$this->assertSame(202, $response->getStatusCode());
}

/**
* @group legacy
*/
public function testStaticCreateAddsContentTypeHeader()
{
$response = JsonResponse::create();
$this->assertSame('application/json', $response->headers->get('Content-Type'));
}

/**
* @group legacy
*/
public function testStaticCreateWithCustomHeaders()
{
$response = JsonResponse::create([], 200, ['ETag' => 'foo']);
$this->assertSame('application/json', $response->headers->get('Content-Type'));
$this->assertSame('foo', $response->headers->get('ETag'));
}

/**
* @group legacy
*/
public function testStaticCreateWithCustomContentType()
{
$headers = ['Content-Type' => 'application/vnd.acme.blog-v1+json'];
Expand All @@ -169,7 +196,7 @@ public function testStaticCreateWithCustomContentType()

public function testSetCallback()
{
$response = JsonResponse::create(['foo' => 'bar'])->setCallback('callback');
$response = (new JsonResponse(['foo' => 'bar']))->setCallback('callback');

$this->assertEquals('/**/callback({"foo":"bar"});', $response->getContent());
$this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
Expand Down Expand Up @@ -217,7 +244,7 @@ public function testSetCallbackInvalidIdentifier()
public function testSetContent()
{
$this->expectException('InvalidArgumentException');
JsonResponse::create("\xB1\x31");
new JsonResponse("\xB1\x31");
}

public function testSetContentJsonSerializeError()
Expand All @@ -230,12 +257,12 @@ public function testSetContentJsonSerializeError()

$serializable = new JsonSerializableObject();

JsonResponse::create($serializable);
new JsonResponse($serializable);
}

public function testSetComplexCallback()
{
$response = JsonResponse::create(['foo' => 'bar']);
$response = new JsonResponse(['foo' => 'bar']);
$response->setCallback('ಠ_ಠ["foo"].bar[0]');

$this->assertEquals('/**/ಠ_ಠ["foo"].bar[0]({"foo":"bar"});', $response->getContent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public function testSetTargetUrl()
$this->assertEquals('baz.beep', $response->getTargetUrl());
}

/**
* @group legacy
*/
public function testCreate()
{
$response = RedirectResponse::create('foo', 301);
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
*/
class ResponseTest extends ResponseTestCase
{
/**
* @group legacy
*/
public function testCreate()
{
$response = Response::create('foo', 301, ['Foo' => 'bar']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public function testGetContent()
$this->assertFalse($response->getContent());
}

/**
* @group legacy
*/
public function testCreate()
{
$response = StreamedResponse::create(function () {}, 204);
Expand Down