我正在编写一个简单的 API 包装器,这个包装器需要配置的 GuzzleHttp\Client 实例作为依赖项。
use GuzzleHttp\Client;
class Api {
private $client;
public function __construct(Client $client)
{
$this->client = $client;
}
}
如果我使用in创建Client
实例,一切正常:new Client()
AppServiceProvider->register()
$this->app->when(Api::class)
->needs(Client::class)
->give(function () {
return new Client(['base_uri' => config('services.api.base_uri')]);
});
但是如果我使用orApi
创建实例,实例解析会挂起:Client
make()
makeWith()
$this->app->when(Api::class)
->needs(Client::class)
->give(function () {
return $this->app->makeWith(Client::class, ['config' => ['base_uri' => config('services.api.base_uri')]]);
});
我通过修补程序进行了快速测试:
app(\App\Api::class);
它只是永远挂起。