Skip to content

Commit f0bd199

Browse files
committed
Make ClientFactory configurable
1 parent 0c2959d commit f0bd199

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/Guzzle/ClientFactory.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@ class ClientFactory implements LoggerAwareInterface {
1818

1919
private $client;
2020
private $logger;
21+
private $config;
2122

22-
public function __construct() {
23+
/**
24+
* @since 2.1.0
25+
*
26+
* @param array $config with possible keys:
27+
* middleware => array of extra middleware to pass to guzzle
28+
* user-agent => string default user agent to use for requests
29+
*/
30+
public function __construct( array $config = array() ) {
2331
$this->logger = new NullLogger();
32+
$this->config = $config;
2433
}
2534

2635
/**
@@ -45,10 +54,22 @@ private function newClient() {
4554
$handlerStack = HandlerStack::create( new CurlHandler() );
4655
$handlerStack->push( $middlewareFactory->retry() );
4756

57+
if( array_key_exists( 'user-agent', $this->config ) ) {
58+
$ua = $this->config['user-agent'];
59+
} else {
60+
$ua = 'Addwiki - mediawiki-api-base';
61+
}
62+
63+
if( array_key_exists( 'middleware', $this->config ) ) {
64+
foreach( $this->config['middleware'] as $middleware ) {
65+
$handlerStack->push( $middleware );
66+
}
67+
}
68+
4869
return new Client( array(
4970
'cookies' => true,
5071
'handler' => $handlerStack,
51-
'headers' => array( 'User-Agent' => 'Addwiki - mediawiki-api-base' ),
72+
'headers' => array( 'User-Agent' => $ua ),
5273
) );
5374
}
5475

0 commit comments

Comments
 (0)