Obullo / Stack (Psr15 Middleware)

Build Status Software License Total Downloads

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

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.

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

Error middleware

This layer is defined in the application by default. It catches the exceptional errors in the application. Thus, it enables management of the application errors from a single location.

Also it handles the 404 error management using Router middleware.

In order your application to print the errors in different types like Json,Html, Xml, It can be customized.

NotAllowed middleware

This layer enables you to restrict the HTTP request types coming to the application.

if your application receives a PUT request, the response object in the NotAllowed layer ends up with an error like below before the application runs:

Only Http GET, POST Methods Allowed

ParsedBody middleware

Decoding the the media types like json,xml coming to your web service, this middleware converts these to http raw type.

Router middleware

Allows to manage routing process of your application.