【发布时间】:2020-04-09 17:09:56
【问题描述】:
在 Guzzle 5.3 中,您可以使用 event subscribers,如下例所示:
use GuzzleHttp\Event\EmitterInterface;
use GuzzleHttp\Event\SubscriberInterface;
use GuzzleHttp\Event\BeforeEvent;
use GuzzleHttp\Event\CompleteEvent;
class SimpleSubscriber implements SubscriberInterface
{
public function getEvents()
{
return [
// Provide name and optional priority
'before' => ['onBefore', 100],
'complete' => ['onComplete'],
// You can pass a list of listeners with different priorities
'error' => [['beforeError', 'first'], ['afterError', 'last']]
];
}
public function onBefore(BeforeEvent $event, $name)
{
echo 'Before!';
}
public function onComplete(CompleteEvent $event, $name)
{
echo 'Complete!';
}
}
Guzzle 6 中的等效示例是什么?
正如我使用onBefore/onComplete 和onError 事件订阅者的phpunit 测试,文件需要升级。
【问题讨论】: