vekas/response-manager

Response Manager For php-8

dev-classesLoadableRM 2023-09-23 20:21 UTC

This package is not auto-updated.

Last update: 2024-04-20 23:17:16 UTC


README

template driven response manager service

instantiate the response-manager

    // instantiate the important dependencies needed for the library
    $container = new Container();
    $container->set(ResponseFactory::class,new ResponseFactory);
    $fileLoader = new FileLoader(
        "Response",
        realpath(__DIR__."/../responses"),
        "Responses"
    );

    // create new factory
    $rmf = new ResponseManagerFactory();

    //add file loader
    $rmf->setFileLoader($fileLoader);

    // finally get the response-manager
    $responseManager = $rmf->getResponseManager($container);

create class instide responses that end with suffix "Response" to identify it as a response it should be like Error404Response.php

and it's content are :

  class Error404Response extends AbstractResponseEntry{
      function __construct(ContainerInterface $container) {
          parent::__construct($container);
      }
      function __invoke() : ResponseInterface{
          /**
           * @var ResponseFactory
           */
          $rf = $this->container->get(ResponseFactory::class);
          $res = $rf->createResponse(200);
          $res->getBody()->write("error404");
          return $res;
      }
  }

when you need to get it's response you should call getResponse($className);

  $error404 = $responseManager->getResponse(Error404Response::class);

it will invoke the class and get the response interface class returned