affinity4 / middleware-factory
使用 Psr17Factories 分发 PSR-15 中间件包。默认 Psr17Factory 是 Nyholm\Psr7\Factory\Psr17Factory
Requires
- php: >=7.2
- nyholm/psr7: ^1.1
- psr/container: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- phpunit/phpunit: ^8.1
- squizlabs/php_codesniffer: ^3.4
This package is auto-updated.
Last update: 2022-06-03 14:32:34 UTC
README
这是 Middlewares\Utils 包的一个分支,它们移除了 Nyholm\Psr7 的支持和测试。
此分支还将 PHPUnit 升级到 8.1+,要求 PHP7.2+,并放弃对 GuzzleHttp、Slim/Http 和 Zend Diactoros 的支持。Middlewares\Utils 中的这些 Http 库版本已经很老了,而且其中许多库将很快有自己的 Psr17 实现。
选择 Nyholm\Psr7\Factory\Psr17Factory 是因为它与其他 Psr7 实现相比性能更好。
工厂
用于创建 ServerRequestInterface
、ResponseInterface
、StreamInterface
和 UriInterface
的 psr-7 实例。默认情况下支持 Nyholm/psr7,但您可以使用 psr/http-factory 接口注册任何不同的工厂。
use Affinity4\MiddlewareFactory\Factory; $request = Factory::createServerRequest('GET', '/'); $response = Factory::createResponse(200); $stream = Factory::createStream('Hello world'); $uri = Factory::createUri('http://example.com'); // By default MiddlewareFactory uses Nyholm\Psr7\Factory\Psr17Factory, // but you can change it and add other classes use Acme\Psr17Factory as AcmePsr17Factory Factory::setStrategies([ AcmePsr17Factory::class ]); // And also register directly an initialized factory Factory::setResponseFactory(new FooResponseFactory()); $fooResponse = Factory::createResponse(); // Get the PSR-17 factory used $uriFactory = Factory::getUriFactory(); $uri = $uriFactory->createUri('http://hello-world.com');
分发器
最小的 PSR-15 兼容分发器
use Affinity4\MiddlewareFactory\Dispatcher; $response = Dispatcher::run([ new Middleware1(), new Middleware2(), new Middleware3(), function ($request, $next) { $response = $next->handle($request); return $response->withHeader('X-Foo', 'Bar'); } ]);
CallableHandler
用于解析和执行可调用对象。它可以作为中间件、服务器请求处理器或可调用对象使用。
use Affinity4\MiddlewareFactory\CallableHandler; $callable = new CallableHandler(function () { return 'Hello world'; }); $response = $callable(); echo $response->getBody(); //Hello world
HttpErrorException
用于表示 HTTP 错误的通用异常
use Affinity4\MiddlewareFactory\HttpErrorException; $exception = HttpErrorException::create(500, [ 'problem' => 'Something bad happened', ]); // Additional context can be get and set on the exception $context = $exception->getContext();
特质
许多中间件之间共享的通用工具,如自定义 PSR-17 工厂的能力
HasResponseFactory
HasStreamFactory
接下来做什么
- 代码风格清理
- 更多测试以提高代码覆盖率,使用 @covers 注解以减少误报并确保测试的隔离
有关最近更改的更多信息,请参阅 CHANGELOG,有关贡献细节请参阅 CONTRIBUTING
MIT 许可证 (MIT)。有关更多信息,请参阅 LICENSE