Skip to content

Commit 87d2b74

Browse files
[Debug] PSR-7 support
1 parent 2655072 commit 87d2b74

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"monolog/monolog": "~1.11",
7777
"ircmaxell/password-compat": "~1.0",
7878
"ocramius/proxy-manager": "~0.4|~1.0",
79+
"psr/http-message": "~1.0",
7980
"egulias/email-validator": "~1.2"
8081
},
8182
"autoload": {

src/Symfony/Component/Debug/ExceptionHandler.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Debug;
1313

14+
use Psr\Http\Message\ResponseInterface;
1415
use Symfony\Component\HttpFoundation\Response;
1516
use Symfony\Component\Debug\Exception\FlattenException;
1617
use Symfony\Component\Debug\Exception\OutOfMemoryException;
@@ -197,15 +198,27 @@ public function sendPhpResponse($exception)
197198
* Creates the error Response associated with the given Exception.
198199
*
199200
* @param \Exception|FlattenException $exception An \Exception instance
201+
* @param ResponseInterface $response An optional prototype PSR-7 response
200202
*
201-
* @return Response A Response instance
203+
* @return Response|ResponseInterface A response instance
202204
*/
203-
public function createResponse($exception)
205+
public function createResponse($exception, ResponseInterface $response = null)
204206
{
205207
if (!$exception instanceof FlattenException) {
206208
$exception = FlattenException::create($exception);
207209
}
208210

211+
if (null !== $response) {
212+
$response = $response->withStatus($exception->getStatusCode());
213+
foreach ($exception->getHeaders() as $k => $v) {
214+
$response = $response->withAddedHeader($k, $v);
215+
}
216+
$response = $response->withHeader('Content-Type', 'text/html; charset='.$this->charset);
217+
$response->getBody()->write($this->decorate($this->getContent($exception), $this->getStylesheet($exception)));
218+
219+
return $response;
220+
}
221+
209222
return Response::create($this->decorate($this->getContent($exception), $this->getStylesheet($exception)), $exception->getStatusCode(), $exception->getHeaders())->setCharset($this->charset);
210223
}
211224

src/Symfony/Component/Debug/Tests/ExceptionHandlerTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Debug\Tests;
1313

1414
use Symfony\Component\Debug\ExceptionHandler;
15+
use Symfony\Component\Debug\Exception\FlattenException;
1516
use Symfony\Component\Debug\Exception\OutOfMemoryException;
1617
use Symfony\Component\HttpFoundation\Response;
1718
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -89,6 +90,46 @@ public function testHandle()
8990
$handler->handle($exception);
9091
}
9192

93+
public function testPsr7()
94+
{
95+
$exception = FlattenException::create(new \Exception('foo'), 123, array('Header' => 'Value'));
96+
97+
$body = $this->getMock('Psr\Http\Message\StreamInterface', array('write'));
98+
$body
99+
->expects($this->exactly(1))
100+
->method('write')
101+
->with($this->stringContains('Whoops, looks like something went wrong.'));
102+
103+
$response0 = $this->getMock('Psr\Http\Message\ResponseInterface', array('withStatus'));
104+
$response1 = $this->getMock('Psr\Http\Message\ResponseInterface', array('withAddedHeader'));
105+
$response2 = $this->getMock('Psr\Http\Message\ResponseInterface', array('withHeader'));
106+
$response3 = $this->getMock('Psr\Http\Message\ResponseInterface', array('getBody'));
107+
$response0
108+
->expects($this->exactly(1))
109+
->method('withStatus')
110+
->with(123)
111+
->will($this->returnValue($response1));
112+
$response1
113+
->expects($this->exactly(1))
114+
->method('withAddedHeader')
115+
->with('Header', 'Value')
116+
->will($this->returnValue($response2));
117+
$response2
118+
->expects($this->exactly(1))
119+
->method('withHeader')
120+
->with('Content-Type', 'text/html; charset=UTF-8')
121+
->will($this->returnValue($response3));
122+
$response3
123+
->expects($this->exactly(1))
124+
->method('getBody')
125+
->will($this->returnValue($body));
126+
127+
$handler = new ExceptionHandler(false);
128+
$response = $handler->createResponse($exception, $response0);
129+
130+
$this->assertSame($response3, $response);
131+
}
132+
92133
public function testHandleOutOfMemoryException()
93134
{
94135
$exception = new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__);

src/Symfony/Component/Debug/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"symfony/http-kernel": ">=2.3,<2.3.24|~2.4.0|>=2.5,<2.5.9|>=2.6,<2.6.2"
2424
},
2525
"require-dev": {
26+
"psr/http-message": "~1.0",
2627
"symfony/phpunit-bridge": "~2.7|~3.0.0",
2728
"symfony/class-loader": "~2.2|~3.0.0",
2829
"symfony/http-kernel": "~2.3.24|~2.5.9|~2.6,>=2.6.2|~3.0.0",

0 commit comments

Comments
 (0)