alexpts / mutable-psr7
PSR-7 的快速 PHP7 实现
0.2.1
2021-01-29 14:41 UTC
Requires
- php: ~8.0
- ext-json: *
- nyholm/psr7-server: ^1.0
- php-http/message-factory: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.5
Provides
README
可变
该包基于 https://github.com/Nyholm/psr7/。该包是可变的。如果您需要不可变实现,请查看 nyholm/psr7
。
超轻量级的 PSR-7 实现。非常严格且非常快速。
安装
composer require alexpts/mutable-psr7
如果您使用 Symfony Flex,则所有消息工厂都将注册为服务。
使用方法
PSR-7 对象不包含任何其他公共方法,除了在 PSR-7 规范 中定义的方法。
创建对象
使用 PSR-17 工厂创建请求、流、URI 等。
$psr17Factory = new \PTS\Psr7\Factory\Psr17Factory(); $request = $psr17Factory->createRequest('GET', 'http://some.com'); $stream = $psr17Factory->createStream('foobar');
发送请求
使用 HTTPlug 或任何其他 PSR-18(HTTP 客户端)发送请求
composer require kriswallsmith/buzz
$psr17Factory = new \PTS\Psr7\Factory\Psr17Factory(); $psr18Client = new \Buzz\Client\Curl($psr17Factory); $request = $psr17Factory->createRequest('GET', 'http://some.com'); $response = $psr18Client->sendRequest($request);
创建服务器请求
nyholm/psr7-server
包可用于从 PHP 超全局变量创建服务器请求。
composer require nyholm/psr7-server
$psr17Factory = new \PTS\Psr7\Factory\Psr17Factory(); $creator = new \Nyholm\Psr7Server\ServerRequestCreator( $psr17Factory, // ServerRequestFactory $psr17Factory, // UriFactory $psr17Factory, // UploadedFileFactory $psr17Factory // StreamFactory ); $serverRequest = $creator->fromGlobals();
发出响应
composer require laminas/laminas-httphandlerrunner
$psr17Factory = new \PTS\Psr7\Factory\Psr17Factory(); $responseBody = $psr17Factory->createStream('Hello world'); $response = $psr17Factory->createResponse(200)->withBody($responseBody); (new \Laminas\HttpHandlerRunner\Emitter\SapiEmitter())->emit($response);
基准测试
composer bench