colossal / colossal-middleware-queue
A PSR-15 middleware allowing multiple other PSR-15 middleware to be treated as a single entity.
v1.0.2
2023-11-05 06:48 UTC
Requires
- php: ^8.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- phpstan/phpstan: ^1.9
- squizlabs/php_codesniffer: ^3.7
README
A PSR-15 middleware allowing multiple other PSR-15 middleware to be treated as a single entity.
Usage
namespace Dummy; use Psr\Http\Message\{ ResponseInterface, ServerRequestInterface }; use Psr\Http\Server\{ MiddlewareInterface, RequestHandlerInterface }; class DummyMiddlewareA { public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { echo 'A'; return $handler->handle($request); } } class DummyMiddlewareB { public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { echo 'B'; return $handler->handle($request); } } class DummyHandler { public function handle(ServerRequestInterface $request): ResponseInterface { echo 'Handler'; // Return a ResponseInterface } }
// ---------------------------------------------------------------------------- // // Creating and using the MiddlewareQueue is trivial. // // ---------------------------------------------------------------------------- // use Colossal\MiddlewareQueue\MiddlewareQueue; use Dummy\{ DummyMiddlewareA, DummyMiddlewareB, DummyHandler }; $dummyMiddlewareA = new DummyMiddlewareA(); $dummyMiddlewareB = new DummyMiddlewareB(); $middlewareQueue = new MiddlewareQueue(); $middlewareQueue->enqueue($dummyMiddlewareA); $middlewareQueue->enqueue($dummyMiddlewareB); // ---------------------------------------------------------------------------- // // The resulting MiddlewareQueue can now be used as a single middleware. // // A call to process will now do the following (in order): // // - Call $dummyMiddlewareA->process() passing the request passed to // // $middlewareQueue->process(). // // - Call $dummyMiddlewareB->process() passing the request passed to // // $middlewareQueue->process(). // // - Call RequestHandlerInterface::handle() on the handler passed to // // $middlewareInterface->process(). // // ---------------------------------------------------------------------------- // $middlewareQueue->process($request, $dummyHandler); // Prints: // A // B // Handler
Development Tips
Running PHPStan Code Quality Analysis
Run the PHPStan code quality analysis with the following command:
>> .\vendor\bin\phpstan --configuration=phpstan.neon
Running PHP Code Sniffer Code Style Analysis
Run the PHP Code Sniffer code style analysis with the following commands:
>> .\vendor\bin\phpcs --standard=phpcs.xml src
To fix automatically resolve issues found by PHP Code Sniffer run the following commands:
>> .\vendor\bin\phpcbf --standard=phpcs.xml src