[] (https://github.com/ThrusterIO/http-middleware/releases) [] (LICENSE) [] (https://travis-ci.org/ThrusterIO/http-middleware) [] (https://scrutinizer-ci.com/g/ThrusterIO/http-middleware) [] (https://scrutinizer-ci.com/g/ThrusterIO/http-middleware) [] (https://packagist.org/packages/thruster/http-middleware)
The Thruster HttpMiddleware Component. PSR-7 based middleware dispatcher
Via Composer
$ composer require thruster/http-middleware
A simple middlewares concept running all registered middlewares.
<?php
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Thruster\Component\HttpMiddleware\Middlewares;
$middlewares = new Middlewares();
$middlewares->add(
function (ServerRequestInterface $request, ResponseInterface $response, callable $next) {
// ... Do something cool
return $next($request, $response);
}
);
$middlewares->add(new SomeCoolMiddleware());
$middlewares->add([$object, 'execute']);
$response = $middlewares($request, new Response());
$notUsefulMiddleware = [$object, 'execute'];
if ($middlewares->has($notUsefulMiddleware)) {
$middlewares->remove($notUsefulMiddleware);
}
$response = $middlewares($request, new Response());
HttpMiddleware provides a simple trait to include middlewares inside your class.
<?php
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Thruster\Component\HttpMiddleware\MiddlewaresAwareTrait;
$application = new class
{
use MiddlewaresAwareTrait;
public function __construct()
{
$this->addMiddleware(
function (ServerRequestInterface $request, ResponseInterface $response, callable $next) {
// ... Do something cool
return $next($request, $response);
}
);
$this->addMiddleware(new SomeCoolMiddleware());
$this->addMiddleware([$this, 'executeAction']);
}
public function executeAction(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
// ...
}
public function handleRequest(ServerRequestInterface $request)
{
$response = new Response();
$response = $this->executeMiddlewares($request, $response);
}
};
$application->handleRequest(ServerRequest::fromGlobals());
When you want to have sepparete middlewares before calling controller action and after call to controller.
<?php
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Thruster\Component\HttpMiddleware\RequestMiddlewares;
$middlewares = new RequestMiddlewares();
$middlewares->pre(
function (ServerRequestInterface $request, ResponseInterface $response, callable $next) {
// ... Do something cool
return $next($request, $response);
}
);
$middlewares->post(new SomeCoolMiddleware());
$response = $middlewares($request, new Response(), [$object, 'executeControllerAction']);
Like MiddlewaresAwareTrait and so the same you can register RequestMiddlewares as a trait.
<?php
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Thruster\Component\HttpMiddleware\RequestMiddlewaresAwareTrait;
$application = new class
{
use RequestMiddlewaresAwareTrait;
public function __construct()
{
$this->addPreMiddleware(
function (ServerRequestInterface $request, ResponseInterface $response, callable $next) {
// ... Do something cool
return $next($request, $response);
}
);
$this->addPostMiddleware(new SomeCoolMiddleware());
}
public function executeAction(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
// ...
}
public function handleRequest(ServerRequestInterface $request)
{
$response = new Response();
$response = $this->executeMiddlewares($request, $response, [$this, 'executeAction']);
}
};
$application->handleRequest(ServerRequest::fromGlobals());
$ composer test
Please see CONTRIBUTING and CONDUCT for details.
Please see License File for more information.