Common utilities used by the middlewares' packages:
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();
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->handle($request);
return $response->withHeader('X-Foo', 'Bar');
}
]);
$response = $dispatcher->dispatch(Factory::createServerRequest());
To resolve and execute a callable. It can be used as a middleware, server request handler or a callable:
use Middlewares\Utils\CallableHandler;
$callable = new CallableHandler(function () {
return 'Hello world';
});
$response = $callable();
echo $response->getBody(); //Hello world
The constructor accepts the following arguments:
mixed $callable
It can be callable or a string that can be resolved as callable, for exampleMyClass::method
.array $arguments
Extra arguments passed when it's invoked.CallableResolverInterface $resolver
Used to resolve the$callable
argument to generate something really callable. Currently there are two available resolvers:ReflectionResolver
(used by default) andContainerResolver
(to use a PSR-11 container)
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.