slpic / http-transfer
一个简单的PHP库,与PSR HTTP消息传输一起工作。
1.0.0
2022-01-07 17:55 UTC
Requires
- php: >=8.0
- psr/http-message: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- phpunit/phpunit: ^9
Suggests
- slepic/http-transfer-log-consumer: See various log consumers.
- slepic/http-transfer-observer-consumer: See observer consumers, possibly adapters of various http clients.
- slepic/http-transfer-observer-implementation: See various observer implementations.
Provides
This package is auto-updated.
Last update: 2024-09-07 23:57:02 UTC
README
http-transfer
一个简单的PHP库,与PSR HTTP消息传输一起工作。
使用方法
目前有3个组件
日志
该组件包括
- 接口
Slepic\Http\Transfer\Log\LogInterface
定义了一个简单的结构,用于存储请求、响应、错误以及传输的起始和结束时间。 - 接口
Slepic\Http\Transfer\Log\StorageInterface
用于存储日志。- 当然,还包括其简单的实现
Slepic\Http\Transfer\Log\ArrayStorage
,它将日志存储在数组中。
- 当然,还包括其简单的实现
$storage = new ArrayStorage();
$storage->store(new Log($request, $start, $end, $response, $exception, $context));
assert($storage[0]->getRequest() === $request);
assert($storage[0]->getResponse() === $response);
assert($storage[0]->getException() === $exception);
assert($storage[0]->getStartTime() === $start);
assert($storage[0]->getEndTime() === $end);
assert($storage[0]->getContext() === $context);
观察者
对传输过程的抽象,观察者会在传输开始和结束时被通知。
观察由 Slepic\Http\Transfer\Observer\ObserverInterface
提供,实际上它只是一个工厂。
观察者接收启动请求并返回一个一次性对象,该对象实现 Slepic\Http\Transfer\Observer\ObserverDelegateInterface
。
代表被通知传输的成功或失败,然后它被垃圾收集器回收。
$observer = new SomeImplementationOfObserverInterface();
$delegate = $observer->observe($request, $context);
//process the $request
//...
//got $response ...
$delegate->success($response);
//or maybe got network exception
$delegate->error($exception);
//or maybe some client like guzzle throw exceptions for HTTP 4xx and 5xx when the response object exists along the exception
$delegate->error($exception, $exception instanceof GuzzleException ? $exception->getResponse() : null);
历史
上述两个的组合,实现了观察者以创建带有持续时间信息的日志。
这个包含了对 Slepic\Http\Transfer\Observer\ObserverInterface
的实现。
Slepic\Http\Transfer\History\HistoryObserver
在传输完成后将传输日志推送到存储中。
当然,这项工作实际上是由其代表 Slepic\Http\Transfer\History\HistoryObserverDelegate
来完成的
//create services
$storage = new ArrayStorage();
$observer = new HistoryObserver($storage);
//somewhere you send some requests
foreach ($requests as $request) {his
$delegate = $observer->observe($request);
try {
$response = $client->sendRequest($request);
} catch (\Exception $e) {
$delegate->error($e);
throw $e;
}
$delegate->success($response);
}
//and when u need it you can access transfer logs
foreach ($storage as $log) {
var_dump($log->getRequest());
var_dump($log->getResponse());
var_dump($log->getEndTime() - $log->getStartTime());
//...
}
Packagist 提供者
slepic/http-transfer-log-consumer
- LogInterface 的消费者slepic/http-transfer-observer-consumer
- ObserverInterface 和 ObserverDelegateInterface 的消费者slepic/http-transfer-observer-implementation
- ObserverInterface 和 ObserverDelegateInterface 的实现
如果您在一个公开项目中使用这个库,并且使用 composer 并且在 Packagist 上共享项目,请考虑将上面的 Packagist 提供者添加到您的 composer.json 中,如下所示
"provide": {
"slepic/http-transfer-*-consumer": "*", //if you created a consumer of correspoding interface(s)
"slepic/http-transfer-*-implementation": "*" //if you created implementation of corresponding interface(s)
},
"suggest": {
"slepic/http-transfer-*-consumer": "*", //if you created implementation of corresponding interface(s)
"slepic/http-transfer-*-implementation": "*" //if you created a consumer of correspoding interface(s)
}