# middlewares/utils [![Latest Version on Packagist][ico-version]][link-packagist] [![Software License][ico-license]](LICENSE) [![Build Status][ico-travis]][link-travis] [![Quality Score][ico-scrutinizer]][link-scrutinizer] [![Total Downloads][ico-downloads]][link-downloads] [![SensioLabs Insight][ico-sensiolabs]][link-sensiolabs] Common utilities used by the middlewares' packages: ## Factory Used to create psr-7 instances of `ServerRequestInterface`, `ResponseInterface`, `StreamInterface` and `UriInterface`. Detects automatically [Diactoros](https://github.com/zendframework/zend-diactoros), [Guzzle](https://github.com/guzzle/psr7) and [Slim](https://github.com/slimphp/Slim) but you can register a different factory using the [http-interop/http-factory](https://github.com/http-interop/http-factory) interface. ```php 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: ```php 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. ```php 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](#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`. ```php 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 '