2017-10-31 3 views
0

종속성 주입을 사용하여 Guzzle의 HTTP 클라이언트 인스턴스를 삽입하고 있습니다. 외부 서비스를 호출하는 여러 데이터 공급자 클래스가 있는데, 그 중 일부는 프록시를 통해 라우트되어야합니다. 해당 클래스에 구현 된 빈 인터페이스 ConnectViaProxy이 있습니다. 나는 프록시 옵션 세트와 목구멍 인스턴스를 바인딩 할,하지만 난 사용할 수 없습니다 when는 구체적인 클래스가 아닌 인터페이스가 필요클래스가 Laravel의 컨테이너를 사용하여 인터페이스를 구현하는 경우 다른 구현 방법을 제공합니다.

$this->app->when(ConnectViaProxy::class) 
    ->needs(GuzzleClient::class) 
    ->give(function(): ProxiedGuzzle { 
     return new ProxiedGuzzle(); 
    }); 

때문입니다.

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, 
      ], 
     ]); 
    } 
} 

어떻게하면 좋을까요?

감사

답변

1

이 상황 중 유일한 방법은 부모 클래스를 사용하는 것 같다.

/** 
* 
*/ 
class ViaProxy implements ConnectViaProxy 
{ 

    public function __construct(GuzzleClient $client) 
    { 
     // Should be an instance of ProxiedGuzzle 
     dd($client); 
    } 
} 

당신이 필요 그럼 그냥 경우에 사용

/** 
* 
*/ 
class FooProvider extends ViaProxy implements Provider 
{ 

} 

그리고하지

use GuzzleHttp\Client as GuzzleClient; 
/** 
* 
*/ 
class BarProvider implements Provider 
{ 
    public function __construct(GuzzleClient $client) 
    { 
     // Should be an instance of GuzzleClient 
     dd($client); 
    } 

사용 문맥 평소

$this->app->when(ViaProxy::class) 
    ->needs(GuzzleClient::class) 
    ->give(function(): ProxiedGuzzle { 
     return new ProxiedGuzzle(); 
    }); 
으로 결합하지 않는 경우
관련 문제