phossa2/middleware [ABANDONED]
PLEASE USE phoole/middleware library instead, (*1)
, (*2)
phossa2/middleware is another cool middleware runner library for PHP., (*3)
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, (*4)
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., (*5)
-
Adopted nice feature of condition
from
woohoolabs/harmony. But we don't
agree its tight-binding with dispatcher., (*6)
-
A couple of cool features unique to this library., (*7)
Installation
Install via the composer
utility., (*8)
composer require "phossa2/middleware"
or add the following lines to your composer.json
, (*9)
{
"require": {
"phossa2/middleware": "2.*"
}
}
Features
-
Able to use most of the double-pass middlewares out there., (*10)
-
Able to use a middleware queue (a group of middlewares) as a
generic middleware in another(or the main) queue., (*11)
-
Able to conditionally execute a middleware or a sub queue base on a
condition., (*12)
-
Able to branching into a subqueue and terminate when the subqueue
finishes., (*13)
Usage
Create the middleware queue, then process all the middlewares., (*14)
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,, (*15)
$mws = (new Queue())
->push(new LoggerMiddleware())
->push(new DispatcherMiddleware());
Advanced
-
Compatibility with PSR-7 middlewares., (*16)
PSR-7 double-pass middleware with the following signature is supported,, (*17)
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., (*18)
-
Subqueue, (*19)
Phossa2\Middleware\Queue
implements the Phossa2\Middleware\Interfaces\MiddlewareInterface
,
so the queue itself can be used as a generic middleware., (*20)
// subqueue
$subQueue = new Queue([
new ResponseTimeMiddleware(),
new LoggingMiddleware(),
// ...
]);
// main middleware queue
$mws = new Queue([
$subQueue,
new DispatcherMiddleware(),
// ...
]);
$response = $mws->process(ServerRequestFactory::fromGlobals(), new Response());
-
Use of conditions, (*21)
A condition
is a callable with the signature of,, (*22)
function (RequestInterface $request, ResponseInterface $response) : bool
{
// ...
}
Or an instanceof Phossa2\Middleware\Interfaces\ConditionInterface
., (*23)
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
., (*24)
// 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, (*25)
Sometimes, user wants the whole middleware processing terminate right after
a subqueue finishes instead of continue processing the parent queue., (*26)
// 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., (*27)
Testing
$ composer test
Contributing
Please see CONTRIBUTE for more information., (*28)
Dependencies
-
PHP >= 5.4.0, (*29)
-
phossa2/shared >= 2.0.21, (*30)
-
A PSR-7 HTTP message implementation, such as zend-diactoros, (*31)
License
MIT License, (*32)