nox a0fd572948 | ||
---|---|---|
.. | ||
src | ||
CHANGELOG.md | ||
CONTRIBUTING.md | ||
LICENSE | ||
README.md | ||
composer.json |
README.md
middlewares/utils
Common utilities used by the middlewares' packages:
Factory
Used to create psr-7 instances of ServerRequestInterface
, ResponseInterface
, StreamInterface
and UriInterface
. Detects automatically Diactoros, Guzzle and Slim but you can register a different factory using the http-interop/http-factory interface.
use Middlewares\Utils\Factory;
$request = Factory::createServerRequest();
$response = Factory::createResponse();
$stream = Factory::createStream();
$uri = Factory::createUri('http://example.com');
//Register other factory
Factory::setResponseFactory(new FooResponseFactory());
$fooResponse = Factory::createResponse();
CallableHandler
To execute a callable and return a response with the output. Useful to handle routes, etc:
use Middlewares\Utils\CallableHandler;
$response = CallableHandler::execute(function () {
echo 'Hello world';
});
echo $response->getBody(); //Hello world
Dispatcher
Minimalist PSR-15 compatible dispatcher. Used for testing purposes.
use Middlewares\Utils\Factory;
use Middlewares\Utils\Dispatcher;
$dispatcher = new Dispatcher([
new Middleware1(),
new Middleware2(),
new Middleware3(),
function ($request, $next) {
$response = $next->process($request);
return $response->withHeader('X-Foo', 'Bar');
}
]);
$response = $dispatcher->dispatch(Factory::createServerRequest());
CallableMiddleware
A simple way to create middlewares using callables. Internally uses CallableHandler so you can use echo
or return string
in the callables (the response is created automatically if it's not returned).
Note: You may not need use this directly in the Dispatcher
, because is used automatically with instances of Closure
.
use Middlewares\Utils\Dispatcher;
use Middlewares\Utils\CallableMiddleware;
$dispatcher = new Dispatcher([
new CallableMiddleware(function ($request, $delegate) {
$response = $delegate->process($request);
return $response->withHeader('Content-Type', 'text/html');
}),
//Providing a Closure directly, the dispatcher will convert to a CallableMiddleware automatically
function ($request, $delegate) {
echo '<h1>Hello world</h1>';
}
]);
$response = $dispatcher->dispatch(new Request());
Helpers
A collection of helpers for psr-7 message manipulation. It contains the following methods:
Helpers::fixContentLength(MessageInterface $response): MessageInterface
Fixes the Content-Length
header based in the body size.
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.