# Class HttpSoft\Runner\MiddlewareResolver
A class that implements HttpSoft\Runner\MiddlewareResolverInterface and resolves the request handler by converting it to an instance of the Psr\Http\Server\MiddlewareInterface.
use HttpSoft\Message\Response;
use HttpSoft\Runner\MiddlewareResolver;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
/**
* @var Psr\Http\Server\RequestHandlerInterface $handler
* @var Psr\Http\Server\MiddlewareInterface $middleware
*/
/** @var Psr\Container\ContainerInterface|null $container */
$resolver = new MiddlewareResolver($container);
$middleware = $resolver->resolve($handler);
$middleware = $resolver->resolve($middleware);
// Or
$middleware = $resolver->resolve(ClassImplementsMiddlewareInterface::class);
$middleware = $resolver->resolve(ClassImplementsRequestHandlerInterface::class);
// From callable handler
$middleware = $resolver->resolve([new HandlerClass(), 'methodName']);
$middleware = $resolver->resolve([HandlerClass::class, 'staticMethodName']);
// Or
$middleware = $resolver->resolve(fn(): ResponseInterface => new Response());
// Or with arguments
$callable = function (ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
// Some actions with the request
$response = $handler->handle($request);
// Some actions with the response
return $response;
};
$middleware = $resolver->resolve($callable);
// From array handlers
$middleware = $resolver->resolve([$middleware, $callable, $handler]);
# Public methods
See the original detailed description of the methods in the HttpSoft\Runner\MiddlewareResolverInterface.
public function __construct(ContainerInterface $container = null);
If a Psr\Container\ContainerInterface instance is specified, the $container->get($className);
is used when resolving the class name (string), otherwise the object will be created using new
.
The
Psr\Container\ContainerInterface
implementation MUST use autowiring.
# resolve
Resolves the request handler by converting it to an instance of the Psr\Http\Server\MiddlewareInterface.
/**
* @param mixed $handler
* @return Psr\Http\Server\MiddlewareInterface
* @throws HttpSoft\Runner\Exception\InvalidMiddlewareResolverHandlerException
*/
public function resolve($handler): MiddlewareInterface;
The handler must be one of:
- A string (class name or identifier of a container definition) or an instance that implements Psr\Http\Server\MiddlewareInterface or Psr\Http\Server\RequestHandlerInterface.
/**
* @var HttpSoft\Runner\MiddlewareResolver $resolver
* @var Psr\Http\Server\RequestHandlerInterface $handler
* @var Psr\Http\Server\MiddlewareInterface $middleware
*/
$middleware = $resolver->resolve($handler);
$middleware = $resolver->resolve($middleware);
// Or
$middleware = $resolver->resolve(ClassImplementsMiddlewareInterface::class);
$middleware = $resolver->resolve(ClassImplementsRequestHandlerInterface::class);
- A callable without arguments that returns an instance of Psr\Http\Message\ResponseInterface.
$middleware = $resolver->resolve([new HandlerClass(), 'methodName']);
$middleware = $resolver->resolve([HandlerClass::class, 'staticMethodName']);
// Or
$middleware = $resolver->resolve(fn(): ResponseInterface => new Response());
$middleware = $resolver->resolve(function (): ResponseInterface {
// Some actions
return new Response(404);
});
- A callable matching signature of Psr\Http\Server\MiddlewareInterface::process().
$middleware = $resolver->resolve([new MiddlewareClass(), 'process']);
$middleware = $resolver->resolve([MiddlewareClass::class, 'staticProcess']);
// Or
$callable = function (ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
// Some actions with the request
$response = $handler->handle($request);
// Some actions with the response
return $response;
};
$middleware = $resolver->resolve($callable);
- An array of previously listed handlers.
$middleware = $resolver->resolve([
new ClassImplementsMiddlewareInterface(),
[new MiddlewareClass(), 'process'],
ClassImplementsRequestHandlerInterface::class,
]);
if the handler is not valid, the HttpSoft\Runner\Exception\InvalidMiddlewareResolverHandlerException exception will be thrown.