vekas/response-manager

php-8 的响应管理器

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

This package is not auto-updated.

Last update: 2024-09-22 01:16:21 UTC


README

基于模板的响应管理服务

实例化响应管理器

    // 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);

在以 "Response" 后缀结尾的响应类中创建类,以标识其为响应,例如 Error404Response.php

它的内容是

  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;
      }
  }

当你需要获取它的响应时,你应该调用 getResponse($className);

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

它将调用类并返回响应接口类