polus/adr

base for my version of adr

5.0.0 2020-10-13 12:53 UTC

This package is auto-updated.

Last update: 2024-12-06 22:38:25 UTC


README

This is my implementation of ADR

Installation

The preferred method of installing this library is with Composer by running the following from your project root:

$ composer require polus/adr

You will also need to include an implementation of a router and a middleware-dispatcher

Currently available routers

Currently available middleware dispatchers

Old versions

See polus/polus-adr for version 1 and 2

Example

<?php


use Aura\Payload_Interface\PayloadInterface;use Aura\Router\RouterContainer;use Http\Factory\Diactoros\ResponseFactory;use Http\Factory\Diactoros\ServerRequestFactory;use Polus\Adr\Actions\AbstractDomainAction;use Polus\Adr\Adr;use Polus\Adr\Interfaces\Resolver;use Polus\Adr\ResponseHandler\HttpResponseHandler;use Polus\Router\AuraRouter\RouterCollection;use Polus\Router\RouterMiddleware;use Psr\Http\Message\ResponseInterface;use Psr\Http\Message\ServerRequestInterface;

$responseFactory = new \Http\Factory\Diactoros\ResponseFactory();
$routerContainer = new RouterContainer();
$routerCollection = new RouterCollection($routerContainer->getMap());
$routerDispatcher = new Polus\Router\AuraRouter\Dispatcher($routerContainer);

$actionResolver =  new class implements Resolver {
    //..
};

$adr = new Adr(
    new ResponseFactory(),
    $actionResolver,
    $routerCollection,
    new HttpResponseHandler(),
    new \Polus\MiddlewareDispatcher\Factory(
        new \Polus\MiddlewareDispatcher\Relay\Dispatcher($responseFactory),
        [
            new RouterMiddleware($routerDispatcher),
            //More psr-15 middlewares
        ]
    )
);

//Define routes and actions
class Responder implements Responder
{
    public function __invoke(
        ServerRequestInterface $request,
        ResponseInterface $response,
        PayloadInterface $payload
    ): ResponseInterface {
        $response->getBody()->write("Index responder\n");
        return $response;
    }
}

$adr->get('/', new class extends AbstractDomainAction {
    protected $responder = Responder::class;
});

//Run application
$factory = new ServerRequestFactory();
$adr->run($factory->createServerRequestFromArray($_SERVER));