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
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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 n1215/http-context 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Behat Context for testing Symfony Api
Lazy loading for middleware and request handlers
repository php library
Context support (request, session and application scope) for n2n framework.
Set Links with a specific language parameter
Bootstraps errors and handles them via reporters and renderers
统计信息
- 总下载量: 31
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-04-02
