2016-09-29 08:14:43 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-19 00:30:12 +00:00
|
|
|
namespace Wallabag\Helper;
|
2016-09-29 08:14:43 +00:00
|
|
|
|
2017-10-24 20:55:40 +00:00
|
|
|
use GuzzleHttp\Client as GuzzleClient;
|
2016-09-29 08:14:43 +00:00
|
|
|
use GuzzleHttp\Cookie\CookieJar;
|
|
|
|
use GuzzleHttp\Event\SubscriberInterface;
|
2017-10-24 20:55:40 +00:00
|
|
|
use Http\Adapter\Guzzle5\Client as GuzzleAdapter;
|
|
|
|
use Http\Client\HttpClient;
|
|
|
|
use Http\HttplugBundle\ClientFactory\ClientFactory;
|
2019-01-28 05:10:26 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2016-09-29 08:14:43 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-24 20:55:40 +00:00
|
|
|
* Builds and configures the HTTP client.
|
2016-09-29 08:14:43 +00:00
|
|
|
*/
|
2017-10-24 20:55:40 +00:00
|
|
|
class HttpClientFactory implements ClientFactory
|
2016-09-29 08:14:43 +00:00
|
|
|
{
|
2022-11-23 14:51:33 +00:00
|
|
|
/** @var SubscriberInterface[] */
|
2017-01-14 15:42:03 +00:00
|
|
|
private $subscribers = [];
|
2016-09-29 08:14:43 +00:00
|
|
|
|
2022-08-28 14:59:43 +00:00
|
|
|
/** @var CookieJar */
|
2016-09-29 08:14:43 +00:00
|
|
|
private $cookieJar;
|
|
|
|
|
2016-11-22 13:56:53 +00:00
|
|
|
private $restrictedAccess;
|
2017-02-13 13:19:53 +00:00
|
|
|
private $logger;
|
2016-11-22 13:56:53 +00:00
|
|
|
|
2016-09-29 08:14:43 +00:00
|
|
|
/**
|
|
|
|
* HttpClientFactory constructor.
|
|
|
|
*
|
2019-11-12 13:18:58 +00:00
|
|
|
* @param string $restrictedAccess This param is a kind of boolean. Values: 0 or 1
|
2016-09-29 08:14:43 +00:00
|
|
|
*/
|
2017-01-14 15:42:03 +00:00
|
|
|
public function __construct(CookieJar $cookieJar, $restrictedAccess, LoggerInterface $logger)
|
2016-09-29 08:14:43 +00:00
|
|
|
{
|
|
|
|
$this->cookieJar = $cookieJar;
|
2016-11-22 13:56:53 +00:00
|
|
|
$this->restrictedAccess = $restrictedAccess;
|
2017-02-13 13:19:53 +00:00
|
|
|
$this->logger = $logger;
|
2016-09-29 08:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-24 20:55:40 +00:00
|
|
|
* Adds a subscriber to the HTTP client.
|
|
|
|
*/
|
|
|
|
public function addSubscriber(SubscriberInterface $subscriber)
|
|
|
|
{
|
|
|
|
$this->subscribers[] = $subscriber;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Input an array of configuration to be able to create a HttpClient.
|
|
|
|
*
|
|
|
|
* @return HttpClient
|
2016-09-29 08:14:43 +00:00
|
|
|
*/
|
2017-10-24 20:55:40 +00:00
|
|
|
public function createClient(array $config = [])
|
2016-09-29 08:14:43 +00:00
|
|
|
{
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->logger->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess]);
|
2017-02-13 13:19:53 +00:00
|
|
|
|
2016-11-22 13:56:53 +00:00
|
|
|
if (0 === (int) $this->restrictedAccess) {
|
2017-11-12 11:15:02 +00:00
|
|
|
return new GuzzleAdapter(new GuzzleClient($config));
|
2016-11-22 13:56:53 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 13:25:51 +00:00
|
|
|
// we clear the cookie to avoid websites who use cookies for analytics
|
|
|
|
$this->cookieJar->clear();
|
2017-11-12 11:15:02 +00:00
|
|
|
if (!isset($config['defaults']['cookies'])) {
|
|
|
|
// need to set the (shared) cookie jar
|
|
|
|
$config['defaults']['cookies'] = $this->cookieJar;
|
|
|
|
}
|
|
|
|
|
|
|
|
$guzzle = new GuzzleClient($config);
|
2017-01-14 15:42:03 +00:00
|
|
|
foreach ($this->subscribers as $subscriber) {
|
2017-10-24 20:55:40 +00:00
|
|
|
$guzzle->getEmitter()->attach($subscriber);
|
2017-01-14 15:42:03 +00:00
|
|
|
}
|
2016-09-29 08:14:43 +00:00
|
|
|
|
2017-10-24 20:55:40 +00:00
|
|
|
return new GuzzleAdapter($guzzle);
|
2017-01-14 15:42:03 +00:00
|
|
|
}
|
2016-09-29 08:14:43 +00:00
|
|
|
}
|