gamringer / pipe
PSR-15 HTTP Middleware Dispatcher
Requires
Requires (Dev)
- guzzlehttp/psr7: ^1.4
- phan/phan: ^1.1
- phpmd/phpmd: ^2.6
- phpstan/phpstan: ^0.10.5
- phpunit/phpunit: ^7
This package is auto-updated.
Last update: 2024-11-07 00:53:20 UTC
README
A PSR-15 handler with no assumption
License
Pipe is licensed under the MIT license.
Installation
$ composer require gamringer/pipe
Tests
$ composer install $ phpunit
Documentation
These are all the various ways in which Pipe can be used. These examples can be found in the examples directory. First, let the following code simply be assumed present in all examples:
<?php declare(strict_types=1); include __DIR__.'/../vendor/autoload.php'; use GuzzleHttp\Psr7\ServerRequest; use GuzzleHttp\Psr7\Response; use gamringer\Pipe\Pipe; use gamringer\Pipe\Example\StaticMiddleware; // Request to be handled later $request = new ServerRequest('GET', '/'); // For use in this example, we build a few Middleware objects // One that simply returns a predetermined response in all cases $staticResponse = new Response(); $staticResponseMiddleware = new StaticMiddleware($staticResponse); // One that echoes it's constructor argument before passing to the next one. $fooMiddleware = new FooMiddleware('1');
Create pipe from an array of Middlewares
// Build Pipe from an array of Middlewares $pipe = new Pipe([ $staticResponseMiddleware, ]); // Use Pipe via RequestHandlerInterface $response = $pipe->handle($request);
Create an empty pipe and add Middlewares dynamically
// Build empty Pipe $pipe = new Pipe(); // Add Middlewares dynamically $pipe->push($staticResponseMiddleware); // Use Pipe via RequestHandlerInterface $response = $pipe->handle($request);
Stack many Middlewares dynamically
// Build empty Pipe $pipe = new Pipe(); // Add Middlewares dynamically $pipe->push($fooMiddleware); $pipe->push($staticResponseMiddleware); // Use Pipe via RequestHandlerInterface $response = $pipe->handle($request);
Use pipe as a Middleware
Pipe can also be used via the MiddlewareInterface by pushing it into another Pipe
// Build empty Pipe $pipe = new Pipe(); // Add Middlewares dynamically $pipe->push($fooMiddleware); $pipe->push(new Pipe([ new FooMiddleware('2') ])); $pipe->push($staticResponseMiddleware); // Use Pipe via RequestHandlerInterface $response = $pipe->handle($request);
Running out of Middlewares
If it runs out of middlewares to call, Pipe will respond differently depending on the way it was called.
As a RequestHandler
Most applications will be using Pipe as a RequestHandler to stack Middlewares and handle user requests. If the request goes through the Pipe and the last Middleware attempts to delegate to the next one, a TerminalException is raised. The same logic also covers the empty Pipe case:
$pipe = new Pipe(); $pipe->handle($request); // Throws new \gamringer\Pipe\TerminalException()
As a Middleware
If called as a Middleware, when running out of Middlewares itself, it will simply pass along to its next sibling:
// Build empty Pipe $pipe = new Pipe(); // Add Middlewares dynamically $pipe->push($fooMiddleware); $pipe->push(new Pipe([ new FooMiddleware('2') ])); $pipe->push(new Pipe()); $pipe->push($staticResponseMiddleware); // Use Pipe via RequestHandlerInterface $response = $pipe->handle($request);