thruster / http-middleware
Thruster HttpMiddleware Component (PSR-7 based middlewares dispatcher)
Requires
- php: >=7.0
- psr/http-message: ~1.0
Requires (Dev)
- phpunit/phpunit: ~5.0
README
[] (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
Install
Via Composer
$ composer require thruster/http-middleware
Usage
Middlewares
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());
Using MiddlewaresAwareTrait Trait
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());
RequestMiddlewares
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']);
RequestMiddlewaresAwareTrait
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());
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CONDUCT for details.
License
Please see License File for more information.