Skip to content

Commit

Permalink
Merge pull request #3 from idealo/use-new-psr15-interfaces
Browse files Browse the repository at this point in the history
use new psr-15 interface packages
  • Loading branch information
datitran authored Apr 25, 2018
2 parents 7589180 + afb97c1 commit d8c85b9
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 1,496 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: php
php:
- '7.0'
- '7.1'
- '7.2'

before_script: composer install
script: phpunit
26 changes: 13 additions & 13 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# PHP Middleware Stack
[![Build Status](https://travis-ci.com/idealo/php-middleware-stack.svg?token=dB3owzyXmEKz9x3RX1AW&branch=master)](https://travis-ci.com/idealo/php-middleware-stack)
[![Build Status](https://travis-ci.org/idealo/php-middleware-stack.svg?branch=master)](https://travis-ci.org/idealo/php-middleware-stack)

This is an implementation of [PSR-15 Draft](https://github.com/php-fig/fig-standards/blob/master/proposed/http-middleware/middleware.md) using the proposed Interface package [http-interop/http-middleware](https://github.com/http-interop/http-middleware) for PHP7+ runtime environment.
This is an implementation of [PSR-15](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-15-request-handlers.md) using the proposed Interface packages [psr/http-server-middleware](https://github.com/php-fig/http-server-middleware) and [psr/http-server-handler](https://github.com/php-fig/http-server-handler) for PHP7+ runtime environment.

It enables a sequential execution of middlewares that use a PSR-7 conform Response/Request implementation.

Expand All @@ -27,22 +27,22 @@ $stack = new Stack(
$middleware3
);

$stackResponse = $stack->process($request);
$stackResponse = $stack->handle($request);


```

## Usage
**idealo/php-middleware-stack** provides the ```Idealo\Middleware\Stack``` class. All it has to know in order to be instantiable is:
* an instance of ```Psr\Http\Message\ResponseInterface``` as the default response
* and middlewares, that implement the ```Psr\Http\Middleware\ServerMiddlewareInterface```
* and middlewares, that implement the ```Psr\Http\Server\MiddlewareInterface```

To perform a sequential processing of injected middlewares you have to call stack's ```process``` method with:
* an instance of ```Psr\Http\Message\RequestInterface```.
To perform a sequential processing of injected middlewares you have to call stack's ```handle``` method with:
* an instance of ```Psr\Http\Message\ServerRequestInterface```.

By default stack's ```process``` method returns the injected response object. If any middleware decides to answer on it's own, than the response object of this certain middleware is returned.
By default stack's ```handle``` method returns the injected response object. If any middleware decides to answer on it's own, than the response object of this certain middleware is returned.

Stack implements ```Interop\Http\ServerMiddleware\DelegateInterface```.
Stack implements ```Psr\Http\Server\RequestHandlerInterface```.

## For example

Expand All @@ -53,12 +53,12 @@ Stack implements ```Interop\Http\ServerMiddleware\DelegateInterface```.
// you decide what middleware you want to put in a stack.
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Server\MiddlewareInterface;

class TrickyMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, DelegateInterface $frame) : ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
{
$requestBody = $request->getBody();
try {
Expand All @@ -67,7 +67,7 @@ class TrickyMiddleware implements MiddlewareInterface
return new CustomExceptionResponse($exception);
}

return $frame->process($request);
return $handler->handle($request);
}
}

Expand Down Expand Up @@ -95,7 +95,7 @@ $stack = new \Idealo\Middleware\Stack(
new LessTrickyMiddleware()
);

$stackResponse = $stack->process($request);
$stackResponse = $stack->handle($request);

// if everything goes well then
var_dump($stackResponse === $defaultResponse); // gives: true
Expand Down
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@
"require": {
"php": "^7.0",
"psr/http-message": "^1.0",
"http-interop/http-middleware": "^0.4"
"psr/http-server-middleware": "^1.0",
"psr/http-server-handler": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^5.6"
"phpunit/phpunit": "^6.5"
},
"autoload": {
"psr-4": {
"Idealo\\Middleware\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Idealo\\Middleware\\Tests\\": "tests/"
}
}
}
Loading

0 comments on commit d8c85b9

Please sign in to comment.