jasny / dummy-middleware
Dummy client and server middleware for PSR-7 requests
Requires
- php: >=7.2.0
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- guzzlehttp/guzzle: ^6.3
- jasny/php-code-quality: ^2.3
- php-http/client-common: ^2.0
- php-http/mock-client: ^1.3
This package is auto-updated.
Last update: 2024-10-24 15:57:20 UTC
README
Dummy client and server middleware for PSR-7 requests. Works both as PSR-15 and double pass middleware.
The dummy services work as null objects / passthrough, preventing the use of if's.
Installation
composer require jasny/dummy-middleware
Usage
Server middleware
Server middleware can be used all passthrough of PSR-7 server requests.
Single pass middleware (PSR-15)
The middleware implements the PSR-15 MiddlewareInterface
. As PSR standard many new libraries support this type of
middleware, for example Zend Stratigility.
use Jasny\Dummy\ServerMiddleware; use Zend\Stratigility\MiddlewarePipe; use Zend\Diactoros\ResponseFactory; $middleware = new ServerMiddleware(); $app = new MiddlewarePipe(); $app->pipe($middleware);
Double pass middleware
Many PHP libraries support double pass middleware. These are callables with the following signature;
fn(ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface
To get a callback to be used by libraries as Jasny Router and
Relay, use the asDoublePass()
method.
use Jasny\Dummy\ServerMiddleware; use Relay\RelayBuilder; $middleware = new ServerMiddleware(); $relayBuilder = new RelayBuilder($resolver); $relay = $relayBuilder->newInstance([ $middleware->asDoublePass(), ]); $response = $relay($request, $baseResponse);
Client middleware
Client middleware can be used for PSR-7 compatible HTTP clients like Guzzle and HTTPlug.
Double pass middleware
The client middleware can be used by any client that does support double pass middleware. Such middleware are callables with the following signature;
fn(RequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface
Most HTTP clients do not support double pass middleware, but a type of single pass instead. However more general purpose PSR-7 middleware libraries, like Relay, do support double pass.
use Relay\RelayBuilder; use Jasny\Dummy\ClientMiddleware; $middleware = new ClientMiddleware(); $relayBuilder = new RelayBuilder($resolver); $relay = $relayBuilder->newInstance([ $middleware->asDoublePass(), ]); $response = $relay($request, $baseResponse);
The client middleware does not conform to PSR-15 (single pass) as that is intended for server requests only.
Guzzle
Guzzle is the most popular HTTP Client for PHP. The middleware has a forGuzzle()
method
that creates a callback which can be used as Guzzle middleware.
use GuzzleHttp\HandlerStack; use GuzzleHttp\Client; use Jasny\Dummy\ClientMiddleware; $middleware = new ClientMiddleware(); $stack = new HandlerStack(); $stack->push($middleware->forGuzzle()); $client = new Client(['handler' => $stack]);
HTTPlug
HTTPlug is the HTTP client of PHP-HTTP. It allows you to write reusable libraries and applications that need an HTTP client without binding to a specific implementation.
The forHttplug()
method for the middleware creates an object that can be used as HTTPlug plugin.
use Http\Discovery\HttpClientDiscovery; use Http\Client\Common\PluginClient; use Jasny\Dummy\ClientMiddleware; $middleware = new ClientMiddleware(); $pluginClient = new PluginClient( HttpClientDiscovery::find(), [ $middleware->forHttplug(), ] );