Permalink
Browse files

Support Responsable objects.

This is the foundation for allowing objects to be converted into full
HTTP responses, allowing more fluent API packages to be built in the
future.
  • Loading branch information...
taylorotwell committed Jun 1, 2017
1 parent ba6cc92 commit c0c89fd73cebf9ed56e6c5e69ad35106df03d9db
@@ -0,0 +1,13 @@
<?php
namespace Illuminate\Contracts\Support;
interface Responsable
{
/**
* Create an HTTP response that represents the object.
*
* @return \Illuminate\Http\Response
*/
public function toResponse();
}
@@ -71,7 +71,8 @@ protected function carry()
*/
protected function handleException($passable, Exception $e)
{
if (! $this->container->bound(ExceptionHandler::class) || ! $passable instanceof Request) {
if (! $this->container->bound(ExceptionHandler::class) ||
! $passable instanceof Request) {
throw $e;
}
@@ -15,6 +15,7 @@
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Contracts\Routing\BindingRegistrar;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
use Illuminate\Contracts\Routing\Registrar as RegistrarContract;
@@ -620,6 +621,8 @@ public static function prepareResponse($request, $response)
{
if ($response instanceof PsrResponseInterface) {
$response = (new HttpFoundationFactory)->createResponse($response);
} elseif ($response instanceof Responsable) {
$response = $response->toResponse();
} elseif (! $response instanceof SymfonyResponse &&
($response instanceof Arrayable ||
$response instanceof Jsonable ||
@@ -0,0 +1,33 @@
<?php
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Route;
use Illuminate\Contracts\Support\Responsable;
/**
* @group integration
*/
class ResponsableTest extends TestCase
{
public function test_responsable_objects_are_rendered()
{
Route::get('/responsable', function () {
return new TestResponsableResponse;
});
$response = $this->get('/responsable');
$this->assertEquals(201, $response->status());
$this->assertEquals('Taylor', $response->headers->get('X-Test-Header'));
$this->assertEquals('hello world', $response->getContent());
}
}
class TestResponsableResponse implements Responsable
{
public function toResponse()
{
return response('hello world', 201, ['X-Test-Header' => 'Taylor']);
}
}

0 comments on commit c0c89fd

Please sign in to comment.