tebe / http-factory
HTTP Factory 为实现 PSR-17 标准的 Composer 包提供自动检测
v3.0.0
2023-01-10 18:15 UTC
Requires
- php: >=8.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
- guzzlehttp/psr7: ^2.0
- laminas/laminas-diactoros: ^2.0
- nyholm/psr7: ^1.0
- nyholm/psr7-server: ^1.0
- phpunit/phpunit: ^9.0
- slim/slim: ^3.0
- squizlabs/php_codesniffer: ^3.0
This package is auto-updated.
Last update: 2024-09-10 22:08:17 UTC
README
HTTP-Factory 是一个 PHP 包,它实现了 PSR-17 HTTP 工厂 接口。它作为一个简单的门面,提供对具体 HTTP 工厂包的便捷访问。其主要特性是支持自动发现支持的工厂。
实现了所有 PSR-17 接口
- Psr\Http\Message\ResponseFactoryInterface
- Psr\Http\Message\ServerRequestFactoryInterface
- Psr\Http\Message\StreamFactoryInterface
- Psr\Http\Message\UploadedFileFactoryInterface
- Psr\Http\Message\UriFactoryInterface
此外,它还实现了 createServerRequestFromGlobals 方法,这不是 PSR-17 的一部分。
自动发现 PSR-7 包
该包支持以下 PSR-7 包的自动发现
- laminas/laminas-diactoros
- guzzlehttp/psr7
- slim/slim
- nyholm/psr7
自动发现机制假定您在项目中使用上述 PSR-7 包中的一个(并且只有一个)。然后,将使用第一个检测到的 PSR-17 包来处理所有接口工厂方法。
安装
在开始一个新项目时,必须安装以下 PSR-7 包之一。
$ composer require laminas/laminas-diactoros $ composer require guzzlehttp/psr7 $ composer require slim/slim $ composer require nyholm/psr7
当使用 "nyholm/psr7" 包时,您还必须要求 "nyholm/psr7-server" 包。
$ composer require nyholm/psr7-server
然后安装 HTTP-Factory 包。
$ composer require tebe/http-factory
现在您可以在项目的代码库中使用 HTTP-Factory 如下所示
use Tebe\HttpFactory\HttpFactory; $factory = new HttpFactory(); # creates a ResponseInterface $factory->createResponse(int $code = 200, string $reasonPhrase = ''); # creates a ServerRequestInterface $factory->createServerRequest(string $method, $uri, array $serverParams = []); # creates a ServerRequestInterface $factory->createServerRequestFromGlobals(); # creates a StreamInterface $factory->createStream(string $content = ''); # creates a StreamInterface $factory->createStreamFromFile(string $filename, string $mode = 'r'); # creates a StreamInterface $factory->createStreamFromResource($resource); # creates an UriInterface $factory->createUri(string $uri = ''); # creates an UploadedFileInterface $factory->createUploadedFile( StreamInterface $stream, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null );
用法
使用构造函数
use Tebe\HttpFactory\HttpFactory; $factory = new HttpFactory(); $response = $factory->createResponse(200); echo $response->getStatusCode();
使用静态实例方法
use Tebe\HttpFactory\HttpFactory; $response = HttpFactory::instance()->createResponse(200); echo $response->getStatusCode();
使用自己的策略
use Tebe\HttpFactory\HttpFactory; HttpFactory::setStrategies([ DiactorosFactory::class, GuzzleFactory::class, SlimFactory::class ]); $response = HttpFactory::instance()->createResponse(200); echo $response->getStatusCode();
使用自己的工厂
use Tebe\HttpFactory\HttpFactory; use Tebe\HttpFactory\FactoryInterface; class MyFactory implements FactoryInterface { // implement interface methods } $factory = new HttpFactory(); $factory->setFactory(new MyFactory()); $response = $factory->createResponse(200); echo $response->getStatusCode();
测试
运行 PHPUnit
$ composer phpunit
运行 PHP_CodeSniffer
$ composer phpcs
运行 PHP 代码美化器和解包器
$ composer phpcbf
一起运行 PHPUnit 和 PHP_CodeSniffer
$ composer test
建议
有任何建议?请提出问题。