Use httplug

This commit is contained in:
adev
2017-10-24 22:55:40 +02:00
committed by Jeremy Benoist
parent 92a6683562
commit bf9ace0643
10 changed files with 329 additions and 373 deletions

View File

@ -2,16 +2,18 @@
namespace Wallabag\CoreBundle\Helper;
use Graby\Ring\Client\SafeCurlHandler;
use GuzzleHttp\Client;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Event\SubscriberInterface;
use Http\Adapter\Guzzle5\Client as GuzzleAdapter;
use Psr\Log\LoggerInterface;
use Http\Client\HttpClient;
use Http\HttplugBundle\ClientFactory\ClientFactory;
/**
* Builds and configures the Guzzle HTTP client.
* Builds and configures the HTTP client.
*/
class HttpClientFactory
class HttpClientFactory implements ClientFactory
{
/** @var [\GuzzleHttp\Event\SubscriberInterface] */
private $subscribers = [];
@ -36,29 +38,6 @@ class HttpClientFactory
$this->logger = $logger;
}
/**
* @return \GuzzleHttp\Client|null
*/
public function buildHttpClient()
{
$this->logger->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess]);
if (0 === (int) $this->restrictedAccess) {
return;
}
// we clear the cookie to avoid websites who use cookies for analytics
$this->cookieJar->clear();
// need to set the (shared) cookie jar
$client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]);
foreach ($this->subscribers as $subscriber) {
$client->getEmitter()->attach($subscriber);
}
return $client;
}
/**
* Adds a subscriber to the HTTP client.
*
@ -68,4 +47,30 @@ class HttpClientFactory
{
$this->subscribers[] = $subscriber;
}
/**
* Input an array of configuration to be able to create a HttpClient.
*
* @param array $config
*
* @return HttpClient
*/
public function createClient(array $config = [])
{
$this->logger->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess]);
if (0 === (int) $this->restrictedAccess) {
return new GuzzleAdapter(new GuzzleClient());
}
// we clear the cookie to avoid websites who use cookies for analytics
$this->cookieJar->clear();
// need to set the (shared) cookie jar
$guzzle = new GuzzleClient(['defaults' => ['cookies' => $this->cookieJar]]);
foreach ($this->subscribers as $subscriber) {
$guzzle->getEmitter()->attach($subscriber);
}
return new GuzzleAdapter($guzzle);
}
}