bermudaphp/psr15factory

v3.0 2025-04-06 10:05 UTC

This package is auto-updated.

Last update: 2025-04-06 10:06:08 UTC


README

composer require bermudaphp/psr15factory

Usage

$factory = new MiddlewareFactory($containerInterface, $responseFactoryInterface);

Classname

class MyMiddleware implements MiddlewareInterface 
{
    private ResponseFactoryInterface $factory;
    
    public function __construct(ResponseFactoryInterface $factory)
    {
        $this->factory = $factory;
    }
    
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        return $this->factory->createResponse(200, 'OK!');
    }
}


$middleware = $factory->make(MyMiddleware::class);
$middleware instanceof MyMiddleware::class // true


class MyHandler implements RequestHandlerInterface 
{
    private ResponseFactoryInterface $factory;
    
    public function __construct(ResponseFactoryInterface $factory)
    {
        $this->factory = $factory;
    }
    
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        return $this->factory->createResponse(200, 'OK!');
    }
}


$middleware = $factory->make(MyHandler::class);
$middleware instanceof MiddlewareInterface::class // true

Lazy Middleware

$middleware = $factory->make(static function(ContainerInterface $c) use ($uri, $permanent): RedirectMiddleware
{         
    return new RedirectMiddleware($uri, $c->get(ResponseFactoryInterface::class), $permanent);
});

$middleware instanceof MiddlewareInterface::class // true
$middleware instanceof RedirectMiddleware::class // true

Callable Middleware

$middleware = $factory->make(static function(ServerRequestInterface $req): ResponseInterface
{
    return new TextResponse('Hello World!');
});

$middleware instanceof MiddlewareInterface::class // true

class MyCallback
{
    public function methodName(ServerRequestInterface $req) : ResponseInterface
    {
        return new TextResponse('Hello World');
    }
}

$middleware = $factory->make('MyCallback::methodName');
$middleware instanceof MiddlewareInterface::class // true

Invokable Middleware

class Invokable
{
    public function __invoke(string $name) : ResponseInterface
    {
        return new TextResponse('Hello, '. $name);
    }
}

$factory->make('Invokable::class') instanceof MiddlewareInterface::class ; // true

Request Args Middleware

$middleware = $factory->make(static function(#[RequestAttributes('name')] string $name): ResponseInterface
{
    return new TextResponse(sprintf('Hello, %s!', $name));
});

$response = $middleware->process((new ServerRequest())->withAttribute('name', 'John'), $requestHandler);
$response instanceof TextResponse // true

Availables callback method signature

function(): ResponseInterface ;
function(ContainerInterface $container): ResponseInterface ;
function(mixed ... $args): ResponseInterface ;
function(ServerRequestInterface $req): ResponseInterface ;
function(ServerRequestInterface $req, RequestHandlerInterface $handler): ResponseInterface ;
function(ServerRequestInterface $req, ResponseInterface $resp, callable $next): ResponseInterface ;
function(ServerRequestInterface $req, callable $next): ResponseInterface ;