phossa2 / middleware
Another cool middleware runner library for PHP.
Installs: 12 985
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ~5.4|~7.0
- container-interop/container-interop: ~1.0
- phossa2/shared: ^2.0.21
- psr/http-message-implementation: ^1.0.0
Requires (Dev)
- phpunit/phpunit: 4.*
- squizlabs/php_codesniffer: 2.*
- zendframework/zend-diactoros: ^1.3.0
Suggests
- phossa2/di: Allows PSR compatible container
- phossa2/route: Allows routing middleware
- zendframework/zend-diactoros: Allows using PSR-7 HTTP messages
Replaces
- phossa/phossa-middleware: *
This package is not auto-updated.
Last update: 2020-01-24 16:24:12 UTC
README
PLEASE USE phoole/middleware library instead
phossa2/middleware is another cool middleware runner library for PHP.
It requires PHP 5.4, supports PHP 7.0+ and HHVM. It is compliant with PSR-1, PSR-2, PSR-3, PSR-4, PSR-7 and the proposed PSR-5
Why another middleware runner ?
-
It was started to be a PSR-15 compatible middleware runner. But we don't like the single-pass approach of PSR-15. So it turns out to be a double-pass and PSR-15ish library.
-
Adopted nice feature of
condition
from woohoolabs/harmony. But we don't agree its tight-binding with dispatcher. -
A couple of cool features unique to this library.
Installation
Install via the composer
utility.
composer require "phossa2/middleware"
or add the following lines to your composer.json
{ "require": { "phossa2/middleware": "2.*" } }
Features
-
Able to use most of the double-pass middlewares out there.
-
Able to use a middleware queue (a group of middlewares) as a generic middleware in another(or the main) queue.
-
Able to conditionally execute a middleware or a sub queue base on a condition.
-
Able to branching into a subqueue and terminate when the subqueue finishes.
Usage
Create the middleware queue, then process all the middlewares.
use Phossa2\Middleware\Queue; use Zend\Diactoros\Response; use Zend\Diactoros\ServerRequestFactory; // create middleware queue $mws = new Queue([ new LoggerMiddleware(), new DispatcherMiddleware() ]); // process the queue $response = $mws->process(ServerRequestFactory::fromGlobals(), new Response());
Or push middlewares to the queue after its instantiation,
$mws = (new Queue()) ->push(new LoggerMiddleware()) ->push(new DispatcherMiddleware());
Advanced
-
Compatibility with PSR-7 middlewares.
PSR-7 double-pass middleware with the following signature is supported,
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; function ( RequestInterface $request, ResponseInterface $response, callable $next ) : ResponseInterface { // ... }
Lots of middlewares out there then can be used without modification, such as psr7-middlewares.
-
Phossa2\Middleware\Queue
implements thePhossa2\Middleware\Interfaces\MiddlewareInterface
, so the queue itself can be used as a generic middleware.// subqueue $subQueue = new Queue([ new ResponseTimeMiddleware(), new LoggingMiddleware(), // ... ]); // main middleware queue $mws = new Queue([ $subQueue, new DispatcherMiddleware(), // ... ]); $response = $mws->process(ServerRequestFactory::fromGlobals(), new Response());
-
A
condition
is a callable with the signature of,function (RequestInterface $request, ResponseInterface $response) : bool { // ... }
Or an instanceof
Phossa2\Middleware\Interfaces\ConditionInterface
.A condition can be attached to a middleware or a subqueue. And the middleware will be executed only if the condition is evaluated to
TRUE
.// add condition during instantiation $mws = new Queue([ [$subQueue, new DebugTurnedOnCondition()], new DispatcherMiddleware(), ]); // or during the push $mws->push(new AuthMiddleware(), new PathPrefixCondition('/user'));
-
Subqueue termination - branching
Sometimes, user wants the whole middleware processing terminate right after a subqueue finishes instead of continue processing the parent queue.
// use terminatable queue $tQueue = new TerminateQueue([...]); $mws = new Queue([ [$tQueue, new SomeCondition()], // execute & terminate if condition true $mw2, $mw3, // ... ]); $response = $mws->process($request, $response);
Change log
Please see CHANGELOG from more information.
Testing
$ composer test
Contributing
Please see CONTRIBUTE for more information.
Dependencies
-
PHP >= 5.4.0
-
phossa2/shared >= 2.0.21
-
A PSR-7 HTTP message implementation, such as zend-diactoros