dakujem / generic-middleware
Generic PSR-15 implementation: Turns callables into Handlers and Middleware.
Installs: 4 146
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^7.4 || ^8.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- ext-json: *
- nette/tester: ^2.3
- psr/http-factory: ^1.0
- slim/psr7: ^1.2
- slim/slim: ^4.5
- tracy/tracy: ^2.3
README
💿
composer require dakujem/generic-middleware
Contains a couple of classes:
GenericMiddleware
, an implementation ofPsr\Http\Server\MiddlewareInterface
GenericHandler
, an implementation ofPsr\Http\Server\RequestHandlerInterface
Note that I'm using aliases
Request
,Response
andHandler
for their respective PSR interface names for brevity.Consider the following
use
statements in use:use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Server\RequestHandlerInterface as Handler;
GenericMiddleware
The GenericMiddleware
is a general purpose middleware that turns a callable into a PSR-15 implementation.
It accepts any callable with signature fn(Request,Handler):Response
.
It can be used for convenient inline middleware implementation:
$app->add(new GenericMiddleware(function(Request $request, Handler $next): Response { $request = $request->withAttribute('foo', 42); $response = $next->handle($request); return $response->withHeader('foo', 'bar'); }));
GenericHandler
The GenericHandler
is a general purpose request handler, as per the PSR-15 specification.
It turns a callable with signature fn(Request):Response
into a PSR-15 implementation.
It can be used for convenient inline handler implementation where you don't want to bother with neither anonymous classes nor named classes:
$kernel = new GenericHandler( fn() => new Response(404, 'Not Found') ); $dispatcher = new MiddlewareDispatcher($kernel);
Testing
Run unit tests using the following command:
$
composer test
Contributing
Ideas, feature requests and other contribution is welcome. Please send a PR or create an issue.