Obullo / Stack (Psr15 Middleware)

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 SendResponse implements MiddlewareInterface
{
    public function process(Request $request, RequestHandler $handler) : ResponseInterface
    {
        $router = $this->getContainer()->get('router');
        $response = null;
        if ($router->hasMatch()) {  // Dispatch application
            // $response = call_user_func_array(array($class, $method),$parameters);
        }
        if ($response instanceof ResponseInterface) {
            return $response;
        }
        return $handler->process(new Error('404'));
    }
}

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.

Psr7 container support

Any container class that uses the Psr7\Container\ContainerInterface interface can be set externally to the Stack class via the construct() method as follows.

use Obullo\Stack\Builder as Stack;

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

If one of the middleware uses a ContainerAwareInterface interface like this one, then your container class is automatically injected into this middleware.

use Psr\Container\ContainerInterface;

interface ContainerAwareInterface
{
    /**
     * Set container
     * 
     * @param ContainerInterface $container container
     */
    public function setContainer(ContainerInterface $container);

    /**
     * Get container
     * 
     * @return object
     */
    public function getContainer() : ContainerInterface;
}

An example middleware that uses ContainerAwareInterface.

namespace App\Middleware;

class ParsedBody implements MiddlewareInterface, ContainerAwareInterface
{
    use ContainerAwareTrait;
    ...
}

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.

SendResponse middleware

After dispatching the application, it is the last middleware to send response to browser.