Skip to content

[Debug] PSR-7 support #15415

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"monolog/monolog": "~1.11",
"ircmaxell/password-compat": "~1.0",
"ocramius/proxy-manager": "~0.4|~1.0",
"psr/http-message": "~1.0",
"egulias/email-validator": "~1.2"
},
"autoload": {
Expand Down
17 changes: 15 additions & 2 deletions src/Symfony/Component/Debug/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Debug;

use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\Exception\OutOfMemoryException;
Expand Down Expand Up @@ -197,15 +198,27 @@ public function sendPhpResponse($exception)
* Creates the error Response associated with the given Exception.
*
* @param \Exception|FlattenException $exception An \Exception instance
* @param ResponseInterface $response An optional prototype PSR-7 response
*
* @return Response A Response instance
* @return Response|ResponseInterface A response instance
*/
public function createResponse($exception)
public function createResponse($exception, ResponseInterface $response = null)
Copy link
Member

Choose a reason for hiding this comment

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

It looks really weird to pass a Response here, especially because it's an immutable one.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hello psr-7, this is how it works. Since you can't instantiate anything, you can only start from a prototype response and play with it.

Copy link
Member

Choose a reason for hiding this comment

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

well, we could instantiate something if we choose a particular implementation for that.

Copy link
Member Author

Choose a reason for hiding this comment

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

but that would be a failure of interoperability and psr-7

Copy link
Member Author

Choose a reason for hiding this comment

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

And I don't think it's really hard for a caller to already have a prototype response.

{
if (!$exception instanceof FlattenException) {
$exception = FlattenException::create($exception);
}

if (null !== $response) {
$response = $response->withStatus($exception->getStatusCode());
foreach ($exception->getHeaders() as $k => $v) {
$response = $response->withAddedHeader($k, $v);
}
$response = $response->withHeader('Content-Type', 'text/html; charset='.$this->charset);
$response->getBody()->write($this->decorate($this->getContent($exception), $this->getStylesheet($exception)));

return $response;
}

return Response::create($this->decorate($this->getContent($exception), $this->getStylesheet($exception)), $exception->getStatusCode(), $exception->getHeaders())->setCharset($this->charset);
}

Expand Down
41 changes: 41 additions & 0 deletions src/Symfony/Component/Debug/Tests/ExceptionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Debug\Tests;

use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\Exception\OutOfMemoryException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand Down Expand Up @@ -89,6 +90,46 @@ public function testHandle()
$handler->handle($exception);
}

public function testPsr7()
{
$exception = FlattenException::create(new \Exception('foo'), 123, array('Header' => 'Value'));

$body = $this->getMock('Psr\Http\Message\StreamInterface');
$body
->expects($this->exactly(1))
->method('write')
->with($this->stringContains('Whoops, looks like something went wrong.'));

$response0 = $this->getMock('Psr\Http\Message\ResponseInterface');
$response1 = $this->getMock('Psr\Http\Message\ResponseInterface');
$response2 = $this->getMock('Psr\Http\Message\ResponseInterface');
$response3 = $this->getMock('Psr\Http\Message\ResponseInterface');
$response0
->expects($this->exactly(1))
->method('withStatus')
->with(123)
->will($this->returnValue($response1));
$response1
->expects($this->exactly(1))
->method('withAddedHeader')
->with('Header', 'Value')
->will($this->returnValue($response2));
$response2
->expects($this->exactly(1))
->method('withHeader')
->with('Content-Type', 'text/html; charset=UTF-8')
->will($this->returnValue($response3));
$response3
->expects($this->exactly(1))
->method('getBody')
->will($this->returnValue($body));

$handler = new ExceptionHandler(false);
$response = $handler->createResponse($exception, $response0);

$this->assertSame($response3, $response);
}

public function testHandleOutOfMemoryException()
{
$exception = new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Debug/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"symfony/http-kernel": ">=2.3,<2.3.24|~2.4.0|>=2.5,<2.5.9|>=2.6,<2.6.2"
},
"require-dev": {
"psr/http-message": "~1.0",
"symfony/phpunit-bridge": "~2.7|~3.0.0",
"symfony/class-loader": "~2.2|~3.0.0",
"symfony/http-kernel": "~2.3.24|~2.5.9|~2.6,>=2.6.2|~3.0.0",
Expand Down