Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 4e76096

Browse files
committed
Merge branch 'feature/12-interop-middleware'
Close #12
2 parents 49ad5bc + 4c1834a commit 4e76096

File tree

9 files changed

+83
-31
lines changed

9 files changed

+83
-31
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ Versions 0.3.0 and prior were released as "weierophinney/problem-details".
3737
`Zend\Expressive\ProblemDetails\Exception\ExceptionInterface`, a marker
3838
interface for exceptions provided by the package.
3939

40+
- [#12](https://github.com/zendframework/zend-problem-details/pull/12) adds
41+
support for http-interop/http-middleware 0.5.0 via a polyfill provided by the
42+
package webimpress/http-middleware-compatibility. Essentially, this means you
43+
can drop this package into an application targeting either the 0.4.1 or 0.5.0
44+
versions of http-middleware, and it will "just work".
45+
4046
### Changed
4147

4248
- [#8](https://github.com/zendframework/zend-problem-details/pull/8) renames the

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
"php": "^7.1",
2525
"ext-json": "*",
2626
"fig/http-message-util": "^1.1.2",
27-
"http-interop/http-middleware": "^0.4.1",
2827
"psr/container": "^1.0",
2928
"psr/http-message": "^1.0",
3029
"spatie/array-to-xml": "^2.3",
30+
"webimpress/http-middleware-compatibility": "^0.1.1",
3131
"willdurand/negotiation": "^2.3"
3232
},
3333
"require-dev": {

composer.lock

Lines changed: 41 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/book/quick-start.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ As an example, the following catches domain excpetions and uses them to create
3535
problem details responses:
3636

3737
```php
38-
use Interop\Http\ServerMiddleware\DelegateInterface;
39-
use Interop\Http\ServerMiddleware\MiddlewareInterface;
4038
use Psr\Http\Message\ServerRequestInterface;
4139
use Throwable;
40+
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface as DelegateInterface;
41+
use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface;
4242
use Zend\Diactoros\Response\JsonResponse;
4343
use Zend\ProblemDetails\ProblemDetailsResponseFactory;
4444

@@ -91,10 +91,10 @@ an example, validation failure is an expected condition, but should likely
9191
result in problem details to the end user.
9292

9393
```php
94-
use Interop\Http\ServerMiddleware\DelegateInterface;
95-
use Interop\Http\ServerMiddleware\MiddlewareInterface;
9694
use Psr\Http\Message\ServerRequestInterface;
9795
use Throwable;
96+
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface as DelegateInterface;
97+
use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface;
9898
use Zend\Diactoros\Response\JsonResponse;
9999
use Zend\InputFilter\InputFilterInterface;
100100
use Zend\ProblemDetails\ProblemDetailsResponseFactory;

docs/book/response.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ Let's say you have middleware that you know will only be used in a production
153153
context, and need to return problem details:
154154

155155
```php
156-
use Interop\Http\ServerMiddleware\DelegateInterface;
157-
use Interop\Http\ServerMiddleware\MiddlewareInterface;
158156
use Psr\Http\Message\ServerRequestInterface;
157+
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface as DelegateInterface;
158+
use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface;
159159
use Zend\ProblemDetails\ProblemDetailsResponseFactory;
160160

161161
class ApiMiddleware implements MiddlewareInterface
@@ -186,15 +186,15 @@ composes, and that service could raise an exception or other `Throwable`. For
186186
this, you can use the `createResponseFromThrowable()` method instead.
187187

188188
```php
189-
use Interop\Http\ServerMiddleware\DelegateInterface;
190-
use Interop\Http\ServerMiddleware\MiddlewareInterface;
189+
use Interop\Http\Server\MiddlewareInterface;
190+
use Interop\Http\Server\RequestHandlerInterface;
191191
use Psr\Http\Message\ServerRequestInterface;
192192
use Throwable;
193193
use Zend\ProblemDetails\ProblemDetailsResponseFactory;
194194

195195
class ApiMiddleware implements MiddlewareInterface
196196
{
197-
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
197+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler)
198198
{
199199
try {
200200
// some code that may raise an exception or throwable
@@ -221,8 +221,8 @@ pass a configured `ProblemDetailsResponseFactory` instance to your middleware's
221221
constructor. As a more complete example:
222222

223223
```php
224-
use Interop\Http\ServerMiddleware\DelegateInterface;
225-
use Interop\Http\ServerMiddleware\MiddlewareInterface;
224+
use Interop\Http\Server\MiddlewareInterface;
225+
use Interop\Http\Server\RequestHandlerInterface;
226226
use Psr\Http\Message\ServerRequestInterface;
227227
use Throwable;
228228
use Zend\ProblemDetails\ProblemDetailsResponseFactory;
@@ -239,7 +239,7 @@ class ApiMiddleware implements MiddlewareInterface
239239
$this->problemDetailsFactory = $problemDetailsFactory;
240240
}
241241

242-
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
242+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler)
243243
{
244244
try {
245245
// some code that may raise an exception or throwable

src/ProblemDetailsMiddleware.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
namespace Zend\ProblemDetails;
99

1010
use ErrorException;
11-
use Interop\Http\ServerMiddleware\DelegateInterface;
12-
use Interop\Http\ServerMiddleware\MiddlewareInterface;
1311
use Negotiation\Negotiator;
1412
use Psr\Http\Message\ResponseInterface;
1513
use Psr\Http\Message\ServerRequestInterface;
1614
use Throwable;
15+
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface as DelegateInterface;
16+
use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface;
17+
18+
use const Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD;
1719

1820
/**
1921
* Middleware that ensures a Problem Details response is returned
@@ -38,12 +40,12 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele
3840
{
3941
// If we cannot provide a representation, act as a no-op.
4042
if (! $this->canActAsErrorHandler($request)) {
41-
return $delegate->process($request);
43+
return $delegate->{HANDLER_METHOD}($request);
4244
}
4345

4446
try {
4547
set_error_handler($this->createErrorHandler());
46-
$response = $delegate->process($request);
48+
$response = $delegate->{HANDLER_METHOD}($request);
4749

4850
if (! $response instanceof ResponseInterface) {
4951
throw new Exception\MissingResponseException('Application did not return a response');

src/ProblemDetailsNotFoundHandler.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88
namespace Zend\ProblemDetails;
99

10-
use Interop\Http\ServerMiddleware\DelegateInterface;
11-
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
1210
use Negotiation\Negotiator;
1311
use Psr\Http\Message\ResponseInterface;
1412
use Psr\Http\Message\ServerRequestInterface;
13+
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface as DelegateInterface;
14+
use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface as ServerMiddlewareInterface;
1515
use Zend\Stratigility\Delegate\CallableDelegateDecorator;
1616

17+
use const Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD;
18+
1719
class ProblemDetailsNotFoundHandler implements ServerMiddlewareInterface
1820
{
1921
/**
@@ -37,7 +39,7 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele
3739
{
3840
// If we cannot provide a representation, act as a no-op.
3941
if (! $this->canActAsErrorHandler($request)) {
40-
return $delegate->process($request);
42+
return $delegate->{HANDLER_METHOD}($request);
4143
}
4244

4345
return $this->responseFactory->createResponse(

test/ProblemDetailsMiddlewareTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
namespace ZendTest\ProblemDetails;
99

1010
use ErrorException;
11-
use Interop\Http\ServerMiddleware\DelegateInterface;
1211
use PHPUnit\Framework\TestCase;
1312
use Prophecy\Argument;
1413
use Psr\Http\Message\ResponseInterface;
1514
use Psr\Http\Message\ServerRequestInterface;
15+
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface as DelegateInterface;
1616
use Zend\ProblemDetails\Exception\MissingResponseException;
1717
use Zend\ProblemDetails\ProblemDetailsMiddleware;
1818
use Zend\ProblemDetails\ProblemDetailsResponseFactory;
1919
use ZendTest\ProblemDetails\TestAsset;
2020

21+
use const Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD;
22+
2123
class ProblemDetailsMiddlewareTest extends TestCase
2224
{
2325
use ProblemDetailsAssertionsTrait;
@@ -45,7 +47,7 @@ public function testSuccessfulDelegationReturnsDelegateResponse() : void
4547
$response = $this->prophesize(ResponseInterface::class);
4648
$delegate = $this->prophesize(DelegateInterface::class);
4749
$delegate
48-
->process(Argument::that([$this->request, 'reveal']))
50+
->{HANDLER_METHOD}(Argument::that([$this->request, 'reveal']))
4951
->will([$response, 'reveal']);
5052

5153

@@ -64,7 +66,7 @@ public function testDelegateNotReturningResponseResultsInProblemDetails(string $
6466

6567
$delegate = $this->prophesize(DelegateInterface::class);
6668
$delegate
67-
->process(Argument::that([$this->request, 'reveal']))
69+
->{HANDLER_METHOD}(Argument::that([$this->request, 'reveal']))
6870
->willReturn('Unexpected');
6971

7072
$expected = $this->prophesize(ResponseInterface::class)->reveal();
@@ -88,7 +90,7 @@ public function testThrowableRaisedByDelegateResultsInProblemDetails(string $acc
8890

8991
$delegate = $this->prophesize(DelegateInterface::class);
9092
$delegate
91-
->process(Argument::that([$this->request, 'reveal']))
93+
->{HANDLER_METHOD}(Argument::that([$this->request, 'reveal']))
9294
->willThrow($exception);
9395

9496
$expected = $this->prophesize(ResponseInterface::class)->reveal();
@@ -110,7 +112,7 @@ public function testMiddlewareRegistersErrorHandlerToConvertErrorsToProblemDetai
110112

111113
$delegate = $this->prophesize(DelegateInterface::class);
112114
$delegate
113-
->process(Argument::that([$this->request, 'reveal']))
115+
->{HANDLER_METHOD}(Argument::that([$this->request, 'reveal']))
114116
->will(function () {
115117
trigger_error('Triggered error!', \E_USER_ERROR);
116118
});
@@ -136,7 +138,7 @@ public function testRethrowsCaughtExceptionIfUnableToNegotiateAcceptHeader() : v
136138
$exception = new TestAsset\RuntimeException('Thrown!', 507);
137139
$delegate = $this->prophesize(DelegateInterface::class);
138140
$delegate
139-
->process(Argument::that([$this->request, 'reveal']))
141+
->{HANDLER_METHOD}(Argument::that([$this->request, 'reveal']))
140142
->willThrow($exception);
141143

142144
$middleware = new ProblemDetailsMiddleware();

test/ProblemDetailsNotFoundHandlerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77

88
namespace ZendTest\ProblemDetails;
99

10-
use ErrorException;
11-
use Interop\Http\ServerMiddleware\DelegateInterface;
1210
use PHPUnit\Framework\TestCase;
1311
use Prophecy\Argument;
1412
use Psr\Http\Message\ResponseInterface;
1513
use Psr\Http\Message\ServerRequestInterface;
16-
use Psr\Http\Message\StreamInterface;
14+
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface as DelegateInterface;
1715
use Zend\ProblemDetails\ProblemDetailsNotFoundHandler;
1816
use Zend\ProblemDetails\ProblemDetailsResponseFactory;
1917

18+
use const Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD;
19+
2020
class ProblemDetailsNotFoundHandlerTest extends TestCase
2121
{
2222
use ProblemDetailsAssertionsTrait;
@@ -97,7 +97,7 @@ public function testDelegateIsCalledIfAcceptHeaderIsUnacceptable() : void
9797
$response = $this->prophesize(ResponseInterface::class);
9898

9999
$delegate = $this->prophesize(DelegateInterface::class);
100-
$delegate->process($request->reveal())->will([$response, 'reveal']);
100+
$delegate->{HANDLER_METHOD}($request->reveal())->will([$response, 'reveal']);
101101

102102
$responseFactory = $this->prophesize(ProblemDetailsResponseFactory::class);
103103

0 commit comments

Comments
 (0)