aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php
diff options
context:
space:
mode:
authorlizyn <zhiylin@outlook.com>2020-02-24 10:04:13 +0800
committerGitHub <noreply@github.com>2020-02-24 10:04:13 +0800
commitb19df31d78d881a43bcf6b3215e5fc3781e8e8aa (patch)
treed0d9861694a4b5e5fbfdbeb53c255ecd15fe9328 /src/Wallabag/CoreBundle/Helper/HttpClientFactory.php
parent4d0c632c70ea50d459c3c55ddda2e0f394dd51cb (diff)
parent04d918cae0227c06a41d27fb6533dddbf30dfe71 (diff)
downloadwallabag-b19df31d78d881a43bcf6b3215e5fc3781e8e8aa.tar.gz
wallabag-b19df31d78d881a43bcf6b3215e5fc3781e8e8aa.tar.zst
wallabag-b19df31d78d881a43bcf6b3215e5fc3781e8e8aa.zip
Merge pull request #1 from wallabag/master
Keep up with the master again
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/HttpClientFactory.php')
-rw-r--r--src/Wallabag/CoreBundle/Helper/HttpClientFactory.php51
1 files changed, 27 insertions, 24 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php
index 4602a684..ea864acb 100644
--- a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php
+++ b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php
@@ -2,16 +2,18 @@
2 2
3namespace Wallabag\CoreBundle\Helper; 3namespace Wallabag\CoreBundle\Helper;
4 4
5use Graby\Ring\Client\SafeCurlHandler; 5use GuzzleHttp\Client as GuzzleClient;
6use GuzzleHttp\Client;
7use GuzzleHttp\Cookie\CookieJar; 6use GuzzleHttp\Cookie\CookieJar;
8use GuzzleHttp\Event\SubscriberInterface; 7use GuzzleHttp\Event\SubscriberInterface;
8use Http\Adapter\Guzzle5\Client as GuzzleAdapter;
9use Http\Client\HttpClient;
10use Http\HttplugBundle\ClientFactory\ClientFactory;
9use Psr\Log\LoggerInterface; 11use Psr\Log\LoggerInterface;
10 12
11/** 13/**
12 * Builds and configures the Guzzle HTTP client. 14 * Builds and configures the HTTP client.
13 */ 15 */
14class HttpClientFactory 16class HttpClientFactory implements ClientFactory
15{ 17{
16 /** @var [\GuzzleHttp\Event\SubscriberInterface] */ 18 /** @var [\GuzzleHttp\Event\SubscriberInterface] */
17 private $subscribers = []; 19 private $subscribers = [];
@@ -25,9 +27,7 @@ class HttpClientFactory
25 /** 27 /**
26 * HttpClientFactory constructor. 28 * HttpClientFactory constructor.
27 * 29 *
28 * @param \GuzzleHttp\Cookie\CookieJar $cookieJar 30 * @param string $restrictedAccess This param is a kind of boolean. Values: 0 or 1
29 * @param string $restrictedAccess This param is a kind of boolean. Values: 0 or 1
30 * @param LoggerInterface $logger
31 */ 31 */
32 public function __construct(CookieJar $cookieJar, $restrictedAccess, LoggerInterface $logger) 32 public function __construct(CookieJar $cookieJar, $restrictedAccess, LoggerInterface $logger)
33 { 33 {
@@ -37,35 +37,38 @@ class HttpClientFactory
37 } 37 }
38 38
39 /** 39 /**
40 * @return \GuzzleHttp\Client|null 40 * Adds a subscriber to the HTTP client.
41 */
42 public function addSubscriber(SubscriberInterface $subscriber)
43 {
44 $this->subscribers[] = $subscriber;
45 }
46
47 /**
48 * Input an array of configuration to be able to create a HttpClient.
49 *
50 * @return HttpClient
41 */ 51 */
42 public function buildHttpClient() 52 public function createClient(array $config = [])
43 { 53 {
44 $this->logger->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess]); 54 $this->logger->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess]);
45 55
46 if (0 === (int) $this->restrictedAccess) { 56 if (0 === (int) $this->restrictedAccess) {
47 return; 57 return new GuzzleAdapter(new GuzzleClient($config));
48 } 58 }
49 59
50 // we clear the cookie to avoid websites who use cookies for analytics 60 // we clear the cookie to avoid websites who use cookies for analytics
51 $this->cookieJar->clear(); 61 $this->cookieJar->clear();
52 // need to set the (shared) cookie jar 62 if (!isset($config['defaults']['cookies'])) {
53 $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); 63 // need to set the (shared) cookie jar
64 $config['defaults']['cookies'] = $this->cookieJar;
65 }
54 66
67 $guzzle = new GuzzleClient($config);
55 foreach ($this->subscribers as $subscriber) { 68 foreach ($this->subscribers as $subscriber) {
56 $client->getEmitter()->attach($subscriber); 69 $guzzle->getEmitter()->attach($subscriber);
57 } 70 }
58 71
59 return $client; 72 return new GuzzleAdapter($guzzle);
60 }
61
62 /**
63 * Adds a subscriber to the HTTP client.
64 *
65 * @param SubscriberInterface $subscriber
66 */
67 public function addSubscriber(SubscriberInterface $subscriber)
68 {
69 $this->subscribers[] = $subscriber;
70 } 73 }
71} 74}