obullo/stack

A version of Node.Js Express middleware stack which is adapted to Psr15.

1.1.1 2018-08-19 19:02 UTC

README

Build Status Software License Total Downloads

Obullo stack is a version of Node.Js Express middleware which is adapted to Psr15.

Http layers are used to affect the $request or response objects when initializing your application. Each layer covers the application and the application is reached by moving towards the center.Therefore, the stages before and after the application starts are taken under the control.

Express 4 middleware

router.use(function(req, res, next) {
    if (!req.route)
        return next (new Error('404'));  
    next();
});

Obullo Psr15 middleware

class Router implements MiddlewareInterface
{
    public function process(Request $request, RequestHandler $handler) : ResponseInterface
    {
        $match = false;
        if (! $match) {
            return $handler->process(new Error('404'));
        }
        return $handler->handle($request);
    }
}

This approach allows us calling a sub process inside middleware.

Install

$ composer require obullo/stack

Requirements

The following versions of PHP are supported by this version.

  • 7.0
  • 7.1
  • 7.2

Testing

$ vendor/bin/phpunit

Quick start

Put listed middlewares in below to your App\Middleware folder. Then dispatch your application.

require '../vendor/autoload.php';

use App\Middleware\{
    Error,
    Router,
    NotAllowed,
    ParsedBody,
    Dummy
};
use Obullo\Stack\Builder as Stack;

$request = Zend\Diactoros\ServerRequestFactory::fromGlobals();

$stack = new Stack;
$stack = $stack->withMiddleware(new Error)
    ->withMiddleware(new Router);

$response = $stack->process($request);

echo $response->getBody();

Array Method

require '../vendor/autoload.php';

use App\Middleware\{
    Error,
    Router,
    NotAllowed,
    ParsedBody,
    Dummy
};
use Obullo\Stack\Builder as Stack;

$request = Zend\Diactoros\ServerRequestFactory::fromGlobals();

$queue = [
    new Error,
    new Dummy,
    new Router
];
$stack = new Stack;
foreach ($queue as $value) {
    $stack = $stack->withMiddleware($value);
}
$response = $stack->process($request);

echo $response->getBody();

Obullo\Stack uses the Http\Stack\StackInterface interface.

Middlewares

Documentation

Documents are available at http://stack.obullo.com/