This adapter can be used, if you dlike to use single pass (no response argument given) middleware within a multipass environment as for example slim or zend-expressive are.
- php: ~5.4
- psr/http-message: ~1.0
Through Composer as chubbyphp/chubbyphp-psr7-middleware-singlepass-to-multipass-adapter.
composer require chubbyphp/chubbyphp-psr7-middleware-singlepass-to-multipass-adapter "~1.0"
The variable $next
, within the single pass middleware arguments will always be a \Closure from the adapter.
<?php
use Chubbyphp\Psr7SinglePassToMultiPassAdapter\Psr7SinglePassToMultiPassAdapter;
$existingSinglePassMiddleware = function (RequestInterface $request, callable $next) {
$request = $request->withHeader('X-Custom', '1');
$response = $next($request);
$body = $response->getBody();
$body->seek(0, SEEK_END);
$body->write('<!-- provided by x-custom -->');
return $response;
};
$adapter = new Psr7SinglePassToMultiPassAdapter($existingSinglePassMiddleware);
$response = $adapter($request, $response, $next);
Dominik Zogg 2016