polus/adr

base for my version of adr

5.0.0 2020-10-13 12:53 UTC

This package is auto-updated.

Last update: 2021-09-06 15:06:01 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 Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Polus\Adr\Adr;
use Polus\Adr\AbstractAction;
use Polus\Adr\Interfaces\ResolverInterface;
use Polus\Adr\Interfaces\ResponderInterface;
use Polus\Adr\ResponseHandler\HttpResponseHandler;
use Polus\Router\RouterMiddleware;
use Polus\Router\AuraRouter\RouterCollection;

$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 ResolverInterface {
    //..
};

$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 ResponderInterface
{
    public function __invoke(
        ServerRequestInterface $request,
        ResponseInterface $response,
        PayloadInterface $payload
    ): ResponseInterface {
        $response->getBody()->write("Index responder\n");
        return $response;
    }
}

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

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