slepic/http-transfer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

slepic/http-transfer

最新稳定版本:1.0.0

Composer 安装命令:

composer require slepic/http-transfer

包简介

Simple PHP library working with PSR HTTP message transfers.

README 文档

README

Build Status Style Status

http-transfer

Simple PHP library working with PSR HTTP message transfers.

Usage

There are 3 components at this moment:

Log

This component consists of:

$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);

Observer

An abstraction over the transfer process where the observer is notified about start and end of transfer processes.

The observation is provided by the Slepic\Http\Transfer\Observer\ObserverInterface which is in fact just a factory.

The observer takes the initiating request and returns a one use object implementing Slepic\Http\Transfer\Observer\ObserverDelegateInterface.

The delegate is destined to be notified of either success or failure of the transfer, and then it gets destroyed by garbage collector.

$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);

History

A coposition of the two above that implements the osbserver to create the logs with duration information.

This one contains just an implementation of the Slepic\Http\Transfer\Observer\ObserverInterface.

The Slepic\Http\Transfer\History\HistoryObserver pushes transfer logs to a storage as they get completed.

Well of course the job is actualy done by its delegate 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 Providers

If you use this library in a public project and you use composer and share the project on packagist, consider adding the packagist providers above to your composer.json like this:

"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)
}

统计信息

  • 总下载量: 18.08k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2019-05-01