承接 n1215/http-context 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

n1215/http-context

Composer 安装命令:

composer require n1215/http-context

包简介

Additional interfaces for handling PSR-7 HTTP messages.

README 文档

README

Additional interfaces for PSR-7 HTTP Messages. Make PSR-7 HTTP middlewares (or applications) simpler and more composable.

HttpContext

HttpContext holds PSR-7 HTTP request, HTTP response, and state.

interface HttpContextInterface
{
    public function getRequest() : ServerRequestInterface;

    public function getResponse() : ResponseInterface;

    public function isTerminated(): bool;

    public function withRequest(ServerRequestInterface $request): HttpContextInterface;

    public function withResponse(ResponseInterface $response): HttpContextInterface;

    public function withIsTerminated(bool $isTerminated): HttpContextInterface;

    public function handledBy(HttpHandlerInterface $handler): HttpContextInterface;

}

HttpHandler

Handles HttpContext. An abstraction of Http middlewares, HTTP applications, or controller actions in typical MVC web frameworks.

interface HttpHandlerInterface
{
    public function __invoke(HttpContextInterface $context) : HttpContextInterface;
}

Comparison with popular PSR-7 middlewares

  • No callable chain
  • Naturally compose pipeline

middleware comparison

Example

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use N1215\Http\Context\HttpContextInterface;
use N1215\Http\Context\HttpHandlerInterface;

class HttpHandler implements HttpHandlerInterface {

    public function __invoke(HttpContextInterface $context): HttpContextInterface
    {
        //do stuff
        $context->getResponse()->getBody()->write('Hello, world!');
        return $context;
    }

}

/**
 * @var ServerRequestInterface $request
 */
$request = ServerRequestFactory::fromGlobals();

/**
 * @var ResponseInterface $response
 */
$response = new Response();

$context = new HttpContext($request, $response); // implements HttpContextInterface

$handler = new HttpHandler();

$newContext = $handler->__invoke($context); // or $handler($context);

$newResponse = $newContext->getResponse();

sequential context handling

$context = new HttpContext($request, $response);

$first = new FirstHttpHandler(); // implements HttpHandlerInterface
$second = new SecondHttpHandler(); // implements HttpHandlerInterface

$newContext = $second($first($context));

sequential context handling (method chain)

$context = new HttpContext($request, $response);

$newContext = $context
    ->handledBy(new FirstHttpHandler());
    ->handledBy(new SecondHttpHandler());

compose handler pipeline as a HttpHandler

class HandlerPipeline implements HttpHandlerInterface {

    /**
     * @var HttpHandlerInterface[]
     */
    private $handlers = [];


    public function __construct(array $handlers = []) {
        $this->handlers = $handlers;
    }

    public function __invoke(HttpContextInterface $context) : HttpContextInterface
    {
        foreach($this->handlers as $handler) {
            if($context->isTerminated()) {
                return $context;
            }
            $context = $handler->__invoke($context);
        }

        return $context;
    }
}

$context = new HttpContext($request, $response);

$pipeline = new HandlerPipeline([
    new FirstHttpHandler(),
    new SecondHttpHandler(),
]);

$newContext = $pipeline($context);

License

MIT License.

n1215/http-context 适用场景与选型建议

n1215/http-context 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31 次下载、GitHub Stars 达 7, 最近一次更新时间为 2016 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「http」 「Context」 「handler」 「psr-7」 「http-message」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 n1215/http-context 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 n1215/http-context 我们能提供哪些服务?
定制开发 / 二次开发

基于 n1215/http-context 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 31
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-04-02