This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/request-handler-middleware' into release-3.0.0
Close #150
- Loading branch information
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* @see https://github.com/zendframework/zend-stratigility for the canonical source repository | ||
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com) | ||
* @license https://github.com/zendframework/zend-stratigility/blob/master/LICENSE.md New BSD License | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Zend\Stratigility\Middleware; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
/** | ||
* Decorate a request handler as middleware. | ||
* | ||
* When pulling handlers from a container, or creating pipelines, it's | ||
* simplest if everything is of the same type, so we do not need to worry | ||
* about varying execution based on type. | ||
* | ||
* To manage this, this class decorates request handlers as middleware, so that | ||
* they may be piped or routed to. When processed, they delegate handling to the | ||
* decorated handler, which will return a response. | ||
*/ | ||
class RequestHandlerMiddleware implements MiddlewareInterface, RequestHandlerInterface | ||
{ | ||
/** | ||
* @var RequestHandlerInterface Decorated handler to invoke. | ||
*/ | ||
private $handler; | ||
|
||
public function __construct(RequestHandlerInterface $handler) | ||
{ | ||
$this->handler = $handler; | ||
} | ||
|
||
/** | ||
* Proxies to decorated handler to handle the request. | ||
*/ | ||
public function handle(ServerRequestInterface $request) : ResponseInterface | ||
{ | ||
return $this->handler->handle($request); | ||
} | ||
|
||
/** | ||
* Proxies to decorated handler to handle the request. | ||
*/ | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface | ||
{ | ||
return $this->handler->handle($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
/** | ||
* @see https://github.com/zendframework/zend-stratigility for the canonical source repository | ||
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com) | ||
* @license https://github.com/zendframework/zend-stratigility/blob/master/LICENSE.md New BSD License | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace ZendTest\Stratigility\Middleware; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Zend\Stratigility\Middleware\RequestHandlerMiddleware; | ||
|
||
class RequestHandlerMiddlewareTest extends TestCase | ||
{ | ||
public function setUp() | ||
{ | ||
$this->request = $this->prophesize(ServerRequestInterface::class)->reveal(); | ||
$this->response = $this->prophesize(ResponseInterface::class)->reveal(); | ||
|
||
$this->handler = $this->prophesize(RequestHandlerInterface::class); | ||
$this->handler->handle($this->request)->willReturn($this->response); | ||
|
||
$this->middleware = new RequestHandlerMiddleware($this->handler->reveal()); | ||
} | ||
|
||
public function testDecoratesHandlerAsMiddleware() | ||
{ | ||
$handler = $this->prophesize(RequestHandlerInterface::class); | ||
$handler->handle(Argument::any())->shouldNotBeCalled(); | ||
|
||
$this->assertSame( | ||
$this->response, | ||
$this->middleware->process($this->request, $handler->reveal()) | ||
); | ||
} | ||
|
||
public function testDecoratesHandlerAsHandler() | ||
{ | ||
$this->assertSame( | ||
$this->response, | ||
$this->middleware->handle($this->request) | ||
); | ||
} | ||
} |