vaibhavpandeyvpz/sandesh
Composer 安装命令:
composer require vaibhavpandeyvpz/sandesh
包简介
PSR-7 and PSR-17 implementation, works with PHP >= 8.2.
README 文档
README
A modern, PHP 8.2+ implementation of PSR-7 (HTTP Message Interfaces) and PSR-17 (HTTP Factories). Sandesh (संदेश) means "message" in Hindi.
Features
- ✅ Full PSR-7 and PSR-17 compliance
- ✅ PHP 8.2+ with strict types and modern features
- ✅ Immutable message objects
- ✅ Type-safe HTTP method enum
- ✅ Cookie parsing and management (RFC 6265)
- ✅ Server response sender
- ✅ Automatic body parsing (JSON, form data, XML)
- ✅ Comprehensive test coverage
Installation
composer require vaibhavpandeyvpz/sandesh
Requirements
- PHP >= 8.2
- PSR-7 HTTP Message Interfaces (^2.0)
- PSR-17 HTTP Factory Interfaces (^1.1)
Quick Start
Creating HTTP Messages
Request
<?php use Sandesh\RequestFactory; use Sandesh\Request; // Using factory $request = (new RequestFactory()) ->createRequest('POST', 'https://api.example.com/users') ->withHeader('Content-Type', 'application/json') ->withBody($stream); // Direct instantiation $request = new Request('GET', 'https://example.com');
Server Request
<?php use Sandesh\ServerRequestFactory; // Create from server superglobal $request = (new ServerRequestFactory()) ->createServerRequest('POST', 'https://example.com/api', $_SERVER) ->withParsedBody(['name' => 'John']) ->withQueryParams(['page' => 1]) ->withCookieParams($_COOKIE) ->withUploadedFiles($uploadedFiles);
Response
<?php use Sandesh\ResponseFactory; use Sandesh\Response; // Using factory $response = (new ResponseFactory()) ->createResponse(200) ->withHeader('Content-Type', 'application/json') ->withBody($stream); // Direct instantiation with body $response = new Response(404, 'Not Found', 'Page not found');
Working with URIs
<?php use Sandesh\UriFactory; $uri = (new UriFactory()) ->createUri('https://user:pass@example.com:8080/path?query=value#fragment'); echo $uri->getScheme(); // 'https' echo $uri->getHost(); // 'example.com' echo $uri->getPort(); // 8080 echo $uri->getPath(); // '/path' echo $uri->getQuery(); // 'query=value' echo $uri->getFragment(); // 'fragment'
Working with Streams
<?php use Sandesh\StreamFactory; // Create empty stream $stream = (new StreamFactory())->createStream(); $stream->write('Hello, World!'); // Create from file $stream = (new StreamFactory()) ->createStreamFromFile('/path/to/file.txt', 'r'); // Create from resource $resource = fopen('php://input', 'r'); $stream = (new StreamFactory()) ->createStreamFromResource($resource); // Read stream $content = $stream->getContents(); $stream->rewind(); // Reset pointer
Working with Uploaded Files
<?php use Sandesh\UploadedFileFactory; use Sandesh\StreamFactory; $streamFactory = new StreamFactory(); $fileFactory = new UploadedFileFactory(); $stream = $streamFactory->createStreamFromFile($_FILES['avatar']['tmp_name']); $uploadedFile = $fileFactory->createUploadedFile( $stream, $_FILES['avatar']['size'], $_FILES['avatar']['error'], $_FILES['avatar']['name'], $_FILES['avatar']['type'] ); // Move uploaded file $uploadedFile->moveTo('/path/to/destination.jpg');
Working with Cookies
<?php use Sandesh\CookieFactory; // Parse Set-Cookie header $cookie = (new CookieFactory()) ->createCookie('session=abc123; Domain=.example.com; Path=/; HttpOnly; Secure; Max-Age=3600'); // Access cookie properties echo $cookie->getName(); // 'session' echo $cookie->getValue(); // 'abc123' echo $cookie->getDomain(); // '.example.com' echo $cookie->getPath(); // '/' echo $cookie->isHttpOnly(); // true echo $cookie->isSecure(); // true // Convert back to Set-Cookie header string $header = (string) $cookie;
Sending Responses
<?php use Sandesh\ServerResponseSender; use Sandesh\ResponseFactory; $response = (new ResponseFactory()) ->createResponse(200) ->withHeader('Content-Type', 'application/json') ->withBody($stream); $sender = new ServerResponseSender(); $sender->send($response);
Working with HTTP Methods
<?php use Sandesh\HttpMethod; // Type-safe HTTP method enum $method = HttpMethod::POST; echo $method->toString(); // 'POST' // Create from string $method = HttpMethod::fromString('get'); // HttpMethod::GET // Use in request $request = new Request($method->toString(), $uri);
Advanced Usage
Immutable Message Pattern
All message objects are immutable. Methods like withHeader(), withBody(), etc., return new instances:
<?php $request = new Request('GET', 'https://example.com'); $request2 = $request->withHeader('X-Custom', 'value'); // $request is unchanged assert($request->hasHeader('X-Custom') === false); assert($request2->hasHeader('X-Custom') === true);
Automatic Body Parsing
Server requests automatically parse common content types:
<?php // JSON body $request = $serverRequest->withHeader('Content-Type', 'application/json') ->withBody($jsonStream); $data = $serverRequest->getParsedBody(); // Automatically decoded JSON // Form data $request = $serverRequest->withHeader('Content-Type', 'application/x-www-form-urlencoded') ->withBody($formStream); $data = $serverRequest->getParsedBody(); // Parsed as array // XML body $request = $serverRequest->withHeader('Content-Type', 'text/xml') ->withBody($xmlStream); $data = $serverRequest->getParsedBody(); // SimpleXMLElement instance
Request Attributes
Server requests support custom attributes for middleware and routing:
<?php $request = $request->withAttribute('route', 'users.show') ->withAttribute('user', $user); $route = $request->getAttribute('route'); $user = $request->getAttribute('user');
API Reference
Core Classes
Request- HTTP request messageResponse- HTTP response messageServerRequest- Server-side HTTP request with parsed dataUri- URI implementationStream- Stream implementationUploadedFile- Uploaded file implementationCookie- Cookie implementation (RFC 6265)HttpMethod- Type-safe HTTP method enum
Factory Classes
RequestFactory- Creates request instancesResponseFactory- Creates response instancesServerRequestFactory- Creates server request instancesUriFactory- Creates URI instancesStreamFactory- Creates stream instancesUploadedFileFactory- Creates uploaded file instancesCookieFactory- Creates cookie instances from headers
Utilities
ServerResponseSender- Sends HTTP responses to the client
Testing
# Run tests vendor/bin/phpunit # Run tests with coverage vendor/bin/phpunit --coverage-text
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Vaibhav Pandey
- Email: contact@vaibhavpandey.com
- GitHub: @vaibhavpandeyvpz
Links
vaibhavpandeyvpz/sandesh 适用场景与选型建议
vaibhavpandeyvpz/sandesh 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 514 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「message」 「factory」 「psr-7」 「psr-17」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vaibhavpandeyvpz/sandesh 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vaibhavpandeyvpz/sandesh 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vaibhavpandeyvpz/sandesh 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Testing object factory for PHP
Testing object factory for PHP
PHP AMQP Binding Library
Laravel 5 Model Factory Generator.
A model factory package for Laravel 4 with expressive API for creating custom tailored dummy objects
统计信息
- 总下载量: 514
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 9
- 依赖项目数: 3
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2017-01-04