diff options
author | Jérémy Benoist <j0k3r@users.noreply.github.com> | 2017-05-09 17:10:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-09 17:10:03 +0200 |
commit | 0eb8220204953b874ebd2dbd0362973f3f45074c (patch) | |
tree | 3a77c57fc49d5ba00b90681232a1f814652fe797 | |
parent | 54c2d164a362e64a320438b439bf9dd6d2c02424 (diff) | |
parent | d047530dc07ceb5a109cd0caa95055d8b071dbd4 (diff) | |
download | wallabag-0eb8220204953b874ebd2dbd0362973f3f45074c.tar.gz wallabag-0eb8220204953b874ebd2dbd0362973f3f45074c.tar.zst wallabag-0eb8220204953b874ebd2dbd0362973f3f45074c.zip |
Merge pull request #2751 from bdunogier/2.2-guzzle_subscribers_improvement
Improved Guzzle subscribers extensibility
4 files changed, 49 insertions, 12 deletions
diff --git a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php index 6d4129e8..1c866f17 100644 --- a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php +++ b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php | |||
@@ -54,7 +54,7 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder | |||
54 | 'loginUri' => $config->login_uri ?: null, | 54 | 'loginUri' => $config->login_uri ?: null, |
55 | 'usernameField' => $config->login_username_field ?: null, | 55 | 'usernameField' => $config->login_username_field ?: null, |
56 | 'passwordField' => $config->login_password_field ?: null, | 56 | 'passwordField' => $config->login_password_field ?: null, |
57 | 'extraFields' => is_array($config->login_extra_fields) ? $config->login_extra_fields : [], | 57 | 'extraFields' => $this->processExtraFields($config->login_extra_fields), |
58 | 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, | 58 | 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, |
59 | ]; | 59 | ]; |
60 | 60 | ||
@@ -65,4 +65,30 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder | |||
65 | 65 | ||
66 | return new SiteConfig($parameters); | 66 | return new SiteConfig($parameters); |
67 | } | 67 | } |
68 | |||
69 | /** | ||
70 | * Processes login_extra_fields config, transforming an '=' separated array of strings | ||
71 | * into a key/value array. | ||
72 | * | ||
73 | * @param array|mixed $extraFieldsStrings | ||
74 | * | ||
75 | * @return array | ||
76 | */ | ||
77 | protected function processExtraFields($extraFieldsStrings) | ||
78 | { | ||
79 | if (!is_array($extraFieldsStrings)) { | ||
80 | return []; | ||
81 | } | ||
82 | |||
83 | $extraFields = []; | ||
84 | foreach ($extraFieldsStrings as $extraField) { | ||
85 | if (strpos($extraField, '=') === false) { | ||
86 | continue; | ||
87 | } | ||
88 | list($fieldName, $fieldValue) = explode('=', $extraField, 2); | ||
89 | $extraFields[$fieldName] = $fieldValue; | ||
90 | } | ||
91 | |||
92 | return $extraFields; | ||
93 | } | ||
68 | } | 94 | } |
diff --git a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php index 1ac8feb1..11ef26d8 100644 --- a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php +++ b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php | |||
@@ -13,8 +13,8 @@ use Psr\Log\LoggerInterface; | |||
13 | */ | 13 | */ |
14 | class HttpClientFactory | 14 | class HttpClientFactory |
15 | { | 15 | { |
16 | /** @var \GuzzleHttp\Event\SubscriberInterface */ | 16 | /** @var [\GuzzleHttp\Event\SubscriberInterface] */ |
17 | private $authenticatorSubscriber; | 17 | private $subscribers = []; |
18 | 18 | ||
19 | /** @var \GuzzleHttp\Cookie\CookieJar */ | 19 | /** @var \GuzzleHttp\Cookie\CookieJar */ |
20 | private $cookieJar; | 20 | private $cookieJar; |
@@ -25,14 +25,12 @@ class HttpClientFactory | |||
25 | /** | 25 | /** |
26 | * HttpClientFactory constructor. | 26 | * HttpClientFactory constructor. |
27 | * | 27 | * |
28 | * @param \GuzzleHttp\Event\SubscriberInterface $authenticatorSubscriber | 28 | * @param \GuzzleHttp\Cookie\CookieJar $cookieJar |
29 | * @param \GuzzleHttp\Cookie\CookieJar $cookieJar | 29 | * @param string $restrictedAccess This param is a kind of boolean. Values: 0 or 1 |
30 | * @param string $restrictedAccess this param is a kind of boolean. Values: 0 or 1 | 30 | * @param LoggerInterface $logger |
31 | * @param LoggerInterface $logger | ||
32 | */ | 31 | */ |
33 | public function __construct(SubscriberInterface $authenticatorSubscriber, CookieJar $cookieJar, $restrictedAccess, LoggerInterface $logger) | 32 | public function __construct(CookieJar $cookieJar, $restrictedAccess, LoggerInterface $logger) |
34 | { | 33 | { |
35 | $this->authenticatorSubscriber = $authenticatorSubscriber; | ||
36 | $this->cookieJar = $cookieJar; | 34 | $this->cookieJar = $cookieJar; |
37 | $this->restrictedAccess = $restrictedAccess; | 35 | $this->restrictedAccess = $restrictedAccess; |
38 | $this->logger = $logger; | 36 | $this->logger = $logger; |
@@ -53,8 +51,20 @@ class HttpClientFactory | |||
53 | $this->cookieJar->clear(); | 51 | $this->cookieJar->clear(); |
54 | // need to set the (shared) cookie jar | 52 | // need to set the (shared) cookie jar |
55 | $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); | 53 | $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); |
56 | $client->getEmitter()->attach($this->authenticatorSubscriber); | 54 | foreach ($this->subscribers as $subscriber) { |
55 | $client->getEmitter()->attach($subscriber); | ||
56 | } | ||
57 | 57 | ||
58 | return $client; | 58 | return $client; |
59 | } | 59 | } |
60 | |||
61 | /** | ||
62 | * Adds a subscriber to the HTTP client. | ||
63 | * | ||
64 | * @param SubscriberInterface $subscriber | ||
65 | */ | ||
66 | public function addSubscriber(SubscriberInterface $subscriber) | ||
67 | { | ||
68 | $this->subscribers[] = $subscriber; | ||
69 | } | ||
60 | } | 70 | } |
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index bccb2e19..68f900a1 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml | |||
@@ -71,10 +71,11 @@ services: | |||
71 | wallabag_core.guzzle.http_client_factory: | 71 | wallabag_core.guzzle.http_client_factory: |
72 | class: Wallabag\CoreBundle\Helper\HttpClientFactory | 72 | class: Wallabag\CoreBundle\Helper\HttpClientFactory |
73 | arguments: | 73 | arguments: |
74 | - "@bd_guzzle_site_authenticator.authenticator_subscriber" | ||
75 | - "@wallabag_core.guzzle.cookie_jar" | 74 | - "@wallabag_core.guzzle.cookie_jar" |
76 | - '@=service(''craue_config'').get(''restricted_access'')' | 75 | - '@=service(''craue_config'').get(''restricted_access'')' |
77 | - '@logger' | 76 | - '@logger' |
77 | calls: | ||
78 | - ["addSubscriber", ["@bd_guzzle_site_authenticator.authenticator_subscriber"]] | ||
78 | 79 | ||
79 | wallabag_core.guzzle.cookie_jar: | 80 | wallabag_core.guzzle.cookie_jar: |
80 | class: GuzzleHttp\Cookie\FileCookieJar | 81 | class: GuzzleHttp\Cookie\FileCookieJar |
diff --git a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php index aee67259..8341b11f 100644 --- a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php +++ b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php | |||
@@ -24,7 +24,7 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase | |||
24 | $grabySiteConfig->login_uri = 'http://example.com/login'; | 24 | $grabySiteConfig->login_uri = 'http://example.com/login'; |
25 | $grabySiteConfig->login_username_field = 'login'; | 25 | $grabySiteConfig->login_username_field = 'login'; |
26 | $grabySiteConfig->login_password_field = 'password'; | 26 | $grabySiteConfig->login_password_field = 'password'; |
27 | $grabySiteConfig->login_extra_fields = ['field' => 'value']; | 27 | $grabySiteConfig->login_extra_fields = ['field=value']; |
28 | $grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]'; | 28 | $grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]'; |
29 | 29 | ||
30 | $grabyConfigBuilderMock | 30 | $grabyConfigBuilderMock |