Skip to content

Commit

Permalink
Support using App as PSR-15 RequestHandler
Browse files Browse the repository at this point in the history
Implementing the RequestHandlerInterface means the
Slim App is embeddedable to another PSR-15 middleware
stack.
  • Loading branch information
bnf committed Feb 16, 2018
1 parent 3c41d33 commit de89805
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions Slim/App.php
Expand Up @@ -14,6 +14,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Exception\MethodNotAllowedException;
use Slim\Exception\NotFoundException;
use Slim\Handlers\Error;
Expand All @@ -37,7 +38,7 @@
* configure, and run a Slim Framework application.
* The \Slim\App class also accepts Slim Framework middleware.
*/
class App
class App implements RequestHandlerInterface
{
use MiddlewareAwareTrait;

Expand Down Expand Up @@ -558,15 +559,30 @@ public function run()
// create request
$request = Request::createFromGlobals($_SERVER);

$response = $this->handle($request);

$this->respond($response);
return $response;
}

/**
* Handle a request
*
* This method traverses the application middleware stack and then returns the
* resultant Response object.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
// create response
$headers = new Headers(['Content-Type' => 'text/html; charset=UTF-8']);
$response = new Response(200, $headers);
$response = $response->withProtocolVersion($this->getSetting('httpVersion'));

$response = $this->process($request, $response);

$this->respond($response);
return $response;
return $this->process($request, $response);
}

/**
Expand Down

0 comments on commit de89805

Please sign in to comment.