alexpts/mutable-psr7

PSR-7 的快速 PHP7 实现

0.2.1 2021-01-29 14:41 UTC

This package is auto-updated.

Last update: 2024-09-04 12:57:13 UTC


README

Build Status Code Coverage Scrutinizer Code Quality

可变

该包基于 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