Obullo / Stack (Psr15 Middleware)
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.
- 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.
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
Documentation
Documents are available at http://stack.obullo.com/