【发布时间】:2017-10-31 19:35:42
【问题描述】:
我正在使用依赖注入来注入 Guzzle 的 HTTP 客户端实例。我有几个调用外部服务的数据提供者类,其中一些需要通过代理进行路由。我有一个在这些类上实现的空接口ConnectViaProxy。我想用代理选项集绑定一个 Guzzle 实例,但我不能使用:
$this->app->when(ConnectViaProxy::class)
->needs(GuzzleClient::class)
->give(function (): ProxiedGuzzle {
return new ProxiedGuzzle();
});
因为when 需要一个具体的类,而不是一个接口。
以下是应代理的提供程序示例:
use GuzzleHttp\Client as GuzzleClient;
class FooProvider implements Provider, ConnectViaProxy
{
public function __construct(GuzzleClient $client)
{
// Should be an instance of ProxiedGuzzle
dd($client);
}
}
还有一个不应被代理的提供者示例:
use GuzzleHttp\Client as GuzzleClient;
class BarProvider implements Provider
{
public function __construct(GuzzleClient $client)
{
// Should be an instance of GuzzleClient
dd($client);
}
}
这里是ProxiedGuzzle 类:
use GuzzleHttp\Client as GuzzleClient;
class ProxiedGuzzle extends GuzzleClient
{
public function __construct()
{
parent::__construct([
'proxy' => [
'http' => PROXY_IP,
'https' => PROXY_IP,
],
]);
}
}
知道我该怎么做吗?
谢谢
【问题讨论】: