ascetic-soft/psr7
Composer 安装命令:
composer require ascetic-soft/psr7
包简介
A lightweight, performance-optimized PSR-7 and PSR-17 implementation for PHP 8.4+
README 文档
README
A lightweight, performance-optimized PSR-7 (HTTP Messages) and PSR-17 (HTTP Factories) implementation for PHP 8.4+.
Designed as a companion library for Waypoint router, but works with any PSR-7/PSR-15/PSR-17 compatible stack.
Features
- Full PSR-7 compliance — implements all 7 interfaces from
psr/http-messagev2.0 - Full PSR-17 compliance — implements all 6 factory interfaces from
psr/http-factoryv1.0 - Optimized headers — O(1) case-insensitive lookups via dual-array storage
- Lazy URI composition —
__toString()result cached, invalidated on mutation - Efficient immutability — shallow
clonewith shared stream references - Thin stream wrapper — minimal overhead over native PHP resources
- ServerRequestCreator — builds requests from
$_SERVER/$_GET/$_POST/$_COOKIE/$_FILES - PHPStan level 9 — fully statically analyzed
Requirements
- PHP >= 8.4
Installation
composer require ascetic-soft/psr7
Quick Start
use AsceticSoft\Psr7\HttpFactory; $factory = new HttpFactory(); // Create a response $response = $factory->createResponse(200); $response->getBody()->write('Hello, World!'); // Create a request $request = $factory->createRequest('GET', 'https://example.com/api/users'); // Create a URI $uri = $factory->createUri('https://example.com/path?query=1#fragment');
Usage
Creating HTTP Messages Directly
use AsceticSoft\Psr7\Request; use AsceticSoft\Psr7\Response; use AsceticSoft\Psr7\ServerRequest; use AsceticSoft\Psr7\Stream; use AsceticSoft\Psr7\Uri; // Request $request = new Request('POST', 'https://api.example.com/users', [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer token123', ], Stream::create(json_encode(['name' => 'John']))); // Response $response = new Response(200, ['Content-Type' => 'text/html']); // Server Request $serverRequest = new ServerRequest('GET', '/api/users', serverParams: $_SERVER); // URI $uri = new Uri('https://example.com:8080/path?q=1#frag'); echo $uri->getHost(); // "example.com" echo $uri->getPort(); // 8080 echo $uri->getPath(); // "/path"
Using the HttpFactory (PSR-17)
The HttpFactory class implements all six PSR-17 factory interfaces in a single class:
use AsceticSoft\Psr7\HttpFactory; $factory = new HttpFactory(); // RequestFactoryInterface $request = $factory->createRequest('GET', '/path'); // ResponseFactoryInterface $response = $factory->createResponse(404, 'Not Found'); // ServerRequestFactoryInterface $serverRequest = $factory->createServerRequest('POST', '/api', $_SERVER); // StreamFactoryInterface $stream = $factory->createStream('content'); $fileStream = $factory->createStreamFromFile('/path/to/file', 'r'); // UriFactoryInterface $uri = $factory->createUri('https://example.com'); // UploadedFileFactoryInterface $uploadedFile = $factory->createUploadedFile($stream, 1024, UPLOAD_ERR_OK, 'file.txt');
Creating Server Requests from Globals
use AsceticSoft\Psr7\ServerRequestCreator; // Create from PHP superglobals ($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES) $request = ServerRequestCreator::fromGlobals(); // Or pass custom arrays $request = ServerRequestCreator::fromGlobals( server: $_SERVER, get: $_GET, post: $_POST, cookies: $_COOKIE, files: $_FILES, );
Using with Waypoint Router
use AsceticSoft\Psr7\ServerRequestCreator; use AsceticSoft\Waypoint\Router; $router = new Router($container); $router->get('/hello/{name}', function (string $name) use ($factory) { $response = $factory->createResponse(); $response->getBody()->write("Hello, {$name}!"); return $response; }); $request = ServerRequestCreator::fromGlobals(); $response = $router->handle($request);
Architecture
AsceticSoft\Psr7\
├── Stream — StreamInterface (thin resource wrapper)
├── Uri — UriInterface (lazy __toString, cached composition)
├── Request — RequestInterface (uses MessageTrait)
├── Response — ResponseInterface (const reason phrases)
├── ServerRequest — ServerRequestInterface (extends Request)
├── UploadedFile — UploadedFileInterface
├── HttpFactory — All 6 PSR-17 factory interfaces
├── ServerRequestCreator — Creates ServerRequest from PHP globals
└── MessageTrait — Shared header/protocol/body logic (trait)
Optimization Details
| Component | Technique | Benefit |
|---|---|---|
| Headers | Dual-array: $headers (original case) + $headerNames (lowercase → original) |
O(1) case-insensitive lookup |
| Uri | Cached __toString(), invalidated on with*() |
Avoid recomposition on repeated access |
| Stream | Direct resource wrapper, pre-computed capabilities | Minimal overhead, no unnecessary wrapping |
| Immutability | Shallow clone, shared stream references |
Fast with*() operations |
| Response | const PHRASES array for status codes |
Zero-cost reason phrase lookup |
Development
make fix # Auto-fix code style (PHP CS Fixer) make cs-check # Check code style (dry-run) make stan # Run PHPStan static analysis (level 9) make test # Run PHPUnit tests make check # Run all checks (cs-check + stan + test) make all # Fix code style, then run stan and tests
License
MIT
ascetic-soft/psr7 适用场景与选型建议
ascetic-soft/psr7 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「stream」 「response」 「message」 「uri」 「request」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ascetic-soft/psr7 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ascetic-soft/psr7 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ascetic-soft/psr7 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Object-Oriented API for PHP streams
Safely namespaced guzzle psr7 for WP2Static
HTTP request logger middleware for Laravel
Interface for a factory to create Psr\Http\Message\StreamInterface objects
Implementation of webignition/stream-factory-interface
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-17