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

Added support for http-interop/http-middleware 0.5.0 (no BC Break) #112

Merged
merged 1 commit into from
Oct 9, 2017
Merged
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"require": {
"php": "^5.6 || ^7.0",
"psr/http-message": "^1.0",
"zendframework/zend-escaper": "^2.3",
"http-interop/http-middleware": "^0.4.1"
"webimpress/http-middleware-compatibility": "^0.1.1",
"zendframework/zend-escaper": "^2.3"
},
"require-dev": {
"zendframework/zend-diactoros": "^1.0",
Expand Down
42 changes: 41 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions doc/book/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ expect the `__invoke()` signature (via the `__invoke()` signature), or stacks
expecting http-interop middleware signatures (via the `process()` method).


When using `__invoke()`, the callable `$out` argument should either be an
`Interop\Http\Middleware\DelegateInterface`, or use the signature:
When using `__invoke()`, the callable `$out` argument should either implement
delegator/request handler interface from `http-interop/http-middleware`
(depends on version you are using), or use the signature:

```php
use Psr\Http\Message\ResponseInterface;
Expand All @@ -80,8 +81,8 @@ callback will be provided for you (typically an instance of
dispatching to the various middleware in its pipeline).

Middleware should either return a response, or the result of
`$next()/DelegateInterface::process()` (which should eventually evaluate to a
response instance).
`$next()/DelegateInterface::process()/RequestHandlerInterface::handle()`
(which should eventually evaluate to a response instance).

Within Stratigility, `Zend\Stratigility\Next` provides an implementation
compatible with either usage.
Expand Down
3 changes: 3 additions & 0 deletions doc/book/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ Within Stratigility, middleware can be:
- Any [http-interop 0.4.1 - middleware](https://github.com/http-interop/http-middleware/tree/0.4.1).
`Zend\Stratigility\MiddlewarePipe` implements
`Interop\Http\ServerMiddleware\MiddlewareInterface`. (Stratigility 2.0 series.)
- Any [http-interop 0.5.0 - middleware](https://github.com/http-interop/http-middleware/tree/0.5.0).
`Zend\Stratigility\MiddlewarePipe` implements
`Interop\Http\Server\MiddlewareInterface`. (Since Stratigility 2.1)
- An object implementing `Zend\Stratigility\MiddlewareInterface`.
`Zend\Stratigility\MiddlewarePipe` implements this interface.
(Legacy; this interface is deprecated starting in 1.3.0.)
26 changes: 24 additions & 2 deletions src/Delegate/CallableDelegateDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

namespace Zend\Stratigility\Delegate;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface as DelegateInterface;

/**
* Decorate callable delegates as http-interop delegates in order to process
Expand Down Expand Up @@ -38,13 +39,34 @@ public function __construct(callable $delegate, ResponseInterface $response)
}

/**
* Proxies to the underlying callable delegate to process a request.
* Method provided for compatibility with http-interop/http-middleware 0.4.1
*
* {@inheritDoc}
*/
public function process(ServerRequestInterface $request)
{
return $this->handle($request);
}

/**
* Proxies to the underlying callable delegate to process a request.
*
* {@inheritDoc}
*/
public function handle(ServerRequestInterface $request)
{
$delegate = $this->delegate;
return $delegate($request, $this->response);
}

/**
* Method provided for compatibility with http-interop/http-middleware 0.1.1.
*
* @param RequestInterface $request
* @return mixed
*/
public function next(RequestInterface $request)
{
return $this->handle($request);
}
}
4 changes: 2 additions & 2 deletions src/Middleware/CallableInteropMiddlewareWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace Zend\Stratigility\Middleware;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface as DelegateInterface;
use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface as ServerMiddlewareInterface;

class CallableInteropMiddlewareWrapper implements ServerMiddlewareInterface
{
Expand Down
8 changes: 5 additions & 3 deletions src/Middleware/CallableMiddlewareWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

namespace Zend\Stratigility\Middleware;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface as DelegateInterface;
use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface as ServerMiddlewareInterface;
use Zend\Stratigility\Next;

use const Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD;

/**
* Decorate legacy callable middleware to make it dispatchable as server
* middleware.
Expand Down Expand Up @@ -52,7 +54,7 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele
$delegate = $delegate instanceof Next
? $delegate
: function ($request) use ($delegate) {
return $delegate->process($request);
return $delegate->{HANDLER_METHOD}($request);
};

return $middleware($request, $this->responsePrototype, $delegate);
Expand Down
8 changes: 5 additions & 3 deletions src/Middleware/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@

use ErrorException;
use Exception;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Throwable;
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface as DelegateInterface;
use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface as ServerMiddlewareInterface;
use Zend\Stratigility\Delegate\CallableDelegateDecorator;
use Zend\Stratigility\Exception\MissingResponseException;

use const Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD;

/**
* Error handler middleware.
*
Expand Down Expand Up @@ -154,7 +156,7 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele
set_error_handler($this->createErrorHandler());

try {
$response = $delegate->process($request);
$response = $delegate->{HANDLER_METHOD}($request);

if (! $response instanceof ResponseInterface) {
throw new MissingResponseException('Application did not return a response');
Expand Down
4 changes: 2 additions & 2 deletions src/Middleware/NotFoundHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

namespace Zend\Stratigility\Middleware;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface as DelegateInterface;
use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface as ServerMiddlewareInterface;
use Zend\Stratigility\Delegate\CallableDelegateDecorator;

class NotFoundHandler implements ServerMiddlewareInterface
Expand Down
4 changes: 2 additions & 2 deletions src/MiddlewarePipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
namespace Zend\Stratigility;

use Closure;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use ReflectionFunction;
use ReflectionMethod;
use SplQueue;
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface as DelegateInterface;
use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface as ServerMiddlewareInterface;
use Zend\Stratigility\Exception\InvalidMiddlewareException;

/**
Expand Down
33 changes: 30 additions & 3 deletions src/Next.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

namespace Zend\Stratigility;

use Interop\Http\ServerMiddleware\DelegateInterface;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
use SplQueue;
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface as DelegateInterface;

use const Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD;

/**
* Iterate a queue of middlewares and execute them.
Expand Down Expand Up @@ -68,6 +71,30 @@ public function __invoke(ServerRequestInterface $request)
return $this->process($request);
}

/**
* Proxy to handle method.
* It is needed to support http-interop/http-middleware 0.1.1.
*
* @param RequestInterface $request
* @return ResponseInterface
*/
public function next(RequestInterface $request)
{
return $this->handle($request);
}

/**
* Proxy to handle method.
* It is needed to support http-interop/http-middleware 0.2-0.4.1.
*
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request)
{
return $this->handle($request);
}

/**
* @param ServerRequestInterface $request
* @return ResponseInterface
Expand All @@ -76,14 +103,14 @@ public function __invoke(ServerRequestInterface $request)
* @throws Exception\MissingResponseException If the middleware executed does
* not return a response.
*/
public function process(ServerRequestInterface $request)
public function handle(ServerRequestInterface $request)
{
$request = $this->resetPath($request);

// No middleware remains; done
if ($this->queue->isEmpty()) {
if ($this->nextDelegate) {
return $this->nextDelegate->process($request);
return $this->nextDelegate->{HANDLER_METHOD}($request);
}

throw new Exception\MissingResponseException(sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace Zend\Stratigility;

use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use InvalidArgumentException;
use OutOfRangeException;
use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface as ServerMiddlewareInterface;

/**
* Value object representing route-based middleware
Expand Down
2 changes: 1 addition & 1 deletion test/Middleware/CallableInteropMiddlewareWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace ZendTest\Stratigility\Middleware;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface as DelegateInterface;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down
6 changes: 4 additions & 2 deletions test/Middleware/CallableMiddlewareWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
namespace ZendTest\Stratigility\Middleware;

use Closure;
use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface as DelegateInterface;
use Zend\Stratigility\Middleware\CallableMiddlewareWrapper;
use Zend\Stratigility\Next;

use const Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD;

class CallableMiddlewareWrapperTest extends TestCase
{
public function testWrapperDecoratesAndProxiesToCallableMiddleware()
Expand Down Expand Up @@ -75,7 +77,7 @@ public function testDecoratedDelegateWillBeInvokedWithOnlyRequest()
$expected = $this->prophesize(ResponseInterface::class)->reveal();

$delegate = $this->prophesize(DelegateInterface::class);
$delegate->process($request)->willReturn($expected);
$delegate->{HANDLER_METHOD}($request)->willReturn($expected);

$decorator = new CallableMiddlewareWrapper(
function ($request, $response, $next) {
Expand Down
Loading