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

Skip to content

Commit f54f7d2

Browse files
committed
[HttpFoundation] Fix BC break introduced by symfony#17642
1 parent 0e5ac97 commit f54f7d2

2 files changed

Lines changed: 41 additions & 29 deletions

File tree

src/Symfony/Component/HttpFoundation/JsonResponse.php

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ class JsonResponse extends Response
3939
* @param mixed $data The response data
4040
* @param int $status The response status code
4141
* @param array $headers An array of response headers
42-
* @param bool $preEncoded If the data is already a JSON string
42+
* @param bool $json If the data is already a JSON string
4343
*/
44-
public function __construct($data = null, $status = 200, $headers = array(), $preEncoded = false)
44+
public function __construct($data = null, $status = 200, $headers = array(), $json = false)
4545
{
4646
parent::__construct('', $status, $headers);
4747

4848
if (null === $data) {
4949
$data = new \ArrayObject();
5050
}
5151

52-
$this->setData($data, $preEncoded);
52+
$json ? $this->setJson($data) : $this->setData($data);
5353
}
5454

5555
/**
@@ -87,46 +87,57 @@ public function setCallback($callback = null)
8787
return $this->update();
8888
}
8989

90+
/**
91+
* Sets a raw string containing a JSON document to be sent.
92+
*
93+
* @param string $data
94+
*
95+
* @return JsonResponse
96+
*
97+
* @throws \InvalidArgumentException
98+
*/
99+
public function setJson($json)
100+
{
101+
$this->data = $json;
102+
103+
return $this->update();
104+
}
105+
90106
/**
91107
* Sets the data to be sent as JSON.
92108
*
93109
* @param mixed $data
94-
* @param bool $preEncoded If the data is already a JSON string
95110
*
96111
* @return JsonResponse
97112
*
98113
* @throws \InvalidArgumentException
99114
*/
100-
public function setData($data = array(), $preEncoded = false)
115+
public function setData($data = array())
101116
{
102-
if (!$preEncoded) {
103-
if (defined('HHVM_VERSION')) {
104-
// HHVM does not trigger any warnings and let exceptions
105-
// thrown from a JsonSerializable object pass through.
106-
// If only PHP did the same...
117+
if (defined('HHVM_VERSION')) {
118+
// HHVM does not trigger any warnings and let exceptions
119+
// thrown from a JsonSerializable object pass through.
120+
// If only PHP did the same...
121+
$data = json_encode($data, $this->encodingOptions);
122+
} else {
123+
try {
124+
// PHP 5.4 and up wrap exceptions thrown by JsonSerializable
125+
// objects in a new exception that needs to be removed.
126+
// Fortunately, PHP 5.5 and up do not trigger any warning anymore.
107127
$data = json_encode($data, $this->encodingOptions);
108-
} else {
109-
try {
110-
// PHP 5.4 and up wrap exceptions thrown by JsonSerializable
111-
// objects in a new exception that needs to be removed.
112-
// Fortunately, PHP 5.5 and up do not trigger any warning anymore.
113-
$data = json_encode($data, $this->encodingOptions);
114-
} catch (\Exception $e) {
115-
if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
116-
throw $e->getPrevious() ?: $e;
117-
}
118-
throw $e;
128+
} catch (\Exception $e) {
129+
if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
130+
throw $e->getPrevious() ?: $e;
119131
}
120-
}
121-
122-
if (JSON_ERROR_NONE !== json_last_error()) {
123-
throw new \InvalidArgumentException(json_last_error_msg());
132+
throw $e;
124133
}
125134
}
126135

127-
$this->data = $data;
136+
if (JSON_ERROR_NONE !== json_last_error()) {
137+
throw new \InvalidArgumentException(json_last_error_msg());
138+
}
128139

129-
return $this->update();
140+
return $this->setJson($data);
130141
}
131142

132143
/**

src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,16 @@ public function testConstructorWithCustomContentType()
7575
$this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
7676
}
7777

78-
public function testConstructorWithPreEncoded()
78+
public function testSetJson()
7979
{
8080
$response = new JsonResponse('1', 200, array(), true);
8181
$this->assertEquals('1', $response->getContent());
8282

8383
$response = new JsonResponse('[1]', 200, array(), true);
8484
$this->assertEquals('[1]', $response->getContent());
8585

86-
$response = new JsonResponse('true', 200, array(), true);
86+
$response = new JsonResponse(null, 200, array());
87+
$response->setJson('true');
8788
$this->assertEquals('true', $response->getContent());
8889
}
8990

0 commit comments

Comments
 (0)