Skip to content

Commit 5b00df4

Browse files
authored
Merge pull request #157 from php-http/allow-custom-plugins
http client factory should allow to specify additional plugins
2 parents d3bb714 + bb625ae commit 5b00df4

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

plugins/introduction.rst

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,13 @@ Libraries that Require Plugins
109109

110110
When :doc:`writing a library based on HTTPlug <../httplug/library-developers>`, you might require
111111
specific plugins to be active. The recommended way for doing this is to provide a factory method
112-
for the ``PluginClient`` that your library users should use. This allows them to inject their own
112+
for the ``PluginClient`` that library users should use. This allows them to inject their own
113113
plugins or configure a different client. For example::
114114

115115
$myApiClient = new My\Api\Client('https://api.example.org', My\Api\HttpClientFactory::create('john', 's3cr3t'));
116116

117117
use Http\Client\HttpClient;
118+
use Http\Client\Common\Plugin;
118119
use Http\Client\Common\Plugin\AuthenticationPlugin;
119120
use Http\Client\Common\Plugin\ErrorPlugin;
120121
use Http\Discovery\HttpClientDiscovery;
@@ -124,22 +125,22 @@ plugins or configure a different client. For example::
124125
/**
125126
* Build the HTTP client to talk with the API.
126127
*
127-
* @param string $user Username
128-
* @param string $pass Password
129-
* @param HttpClient $client Base HTTP client
128+
* @param string $user Username for the application on the API
129+
* @param string $pass Password for the application on the API
130+
* @param Plugin[] $plugins List of additional plugins to use
131+
* @param HttpClient $client Base HTTP client
130132
*
131133
* @return HttpClient
132134
*/
133-
public static function create($user, $pass, HttpClient $client = null)
135+
public static function create($user, $pass, array $plugins = [], HttpClient $client = null)
134136
{
135137
if (!$client) {
136138
$client = HttpClientDiscovery::find();
137139
}
138-
return new PluginClient($client, [
139-
new ErrorPlugin(),
140-
new AuthenticationPlugin(
141-
// This API has it own authentication algorithm
142-
new ApiAuthentication(Client::AUTH_OAUTH_TOKEN, $user, $pass)
143-
),
144-
]);
140+
$plugins[] = new ErrorPlugin();
141+
$plugins[] = new AuthenticationPlugin(
142+
// This API has it own authentication algorithm
143+
new ApiAuthentication(Client::AUTH_OAUTH_TOKEN, $user, $pass)
144+
);
145+
return new PluginClient($client, $plugins);
145146
}

0 commit comments

Comments
 (0)