1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mediawiki\Api\Guzzle; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Handler\CurlHandler; |
7
|
|
|
use GuzzleHttp\HandlerStack; |
8
|
|
|
use Psr\Log\LoggerAwareInterface; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use Psr\Log\NullLogger; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @since 2.1 |
14
|
|
|
* |
15
|
|
|
* @author Addshore |
16
|
|
|
*/ |
17
|
|
|
class ClientFactory implements LoggerAwareInterface { |
18
|
|
|
|
19
|
|
|
private $client; |
20
|
|
|
private $logger; |
21
|
|
|
private $config; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @since 2.1 |
25
|
|
|
* |
26
|
|
|
* @param array $config All configuration settings supported by Guzzle, and these: |
27
|
|
|
* middleware => array of extra middleware to pass to guzzle |
28
|
|
|
* user-agent => string default user agent to use for requests |
29
|
|
|
*/ |
30
|
5 |
|
public function __construct( array $config = [] ) { |
31
|
5 |
|
$this->logger = new NullLogger(); |
32
|
5 |
|
$this->config = $config; |
33
|
5 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @since 2.1 |
37
|
|
|
* |
38
|
|
|
* @return Client |
39
|
|
|
*/ |
40
|
5 |
|
public function getClient() { |
41
|
5 |
|
if ( $this->client === null ) { |
42
|
5 |
|
$this->client = $this->newClient(); |
43
|
5 |
|
} |
44
|
5 |
|
return $this->client; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return Client |
49
|
|
|
*/ |
50
|
5 |
|
private function newClient() { |
51
|
|
|
$this->config += [ |
52
|
5 |
|
'cookies' => true, |
53
|
5 |
|
'headers' => [], |
54
|
5 |
|
'middleware' => [], |
55
|
|
|
]; |
56
|
|
|
|
57
|
5 |
|
if ( !array_key_exists( 'User-Agent', $this->config['headers'] ) ) { |
58
|
4 |
|
if ( array_key_exists( 'user-agent', $this->config ) ) { |
59
|
1 |
|
$this->config['headers']['User-Agent'] = $this->config['user-agent']; |
60
|
1 |
|
} else { |
61
|
3 |
|
$this->config['headers']['User-Agent'] = 'Addwiki - mediawiki-api-base'; |
62
|
|
|
} |
63
|
4 |
|
} |
64
|
5 |
|
unset( $this->config['user-agent'] ); |
65
|
|
|
|
66
|
5 |
|
if ( !array_key_exists( 'handler', $this->config ) ) { |
67
|
4 |
|
$this->config['handler'] = HandlerStack::create( new CurlHandler() ); |
68
|
4 |
|
} |
69
|
|
|
|
70
|
5 |
|
$middlewareFactory = new MiddlewareFactory(); |
71
|
5 |
|
$middlewareFactory->setLogger( $this->logger ); |
72
|
|
|
|
73
|
5 |
|
$this->config['middleware'][] = $middlewareFactory->retry(); |
74
|
|
|
|
75
|
5 |
|
foreach ( $this->config['middleware'] as $name => $middleware ) { |
76
|
5 |
|
$this->config['handler']->push( $middleware ); |
77
|
5 |
|
} |
78
|
5 |
|
unset( $this->config['middleware'] ); |
79
|
|
|
|
80
|
5 |
|
return new Client( $this->config ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Sets a logger instance on the object |
85
|
|
|
* |
86
|
|
|
* @since 2.1 |
87
|
|
|
* |
88
|
|
|
* @param LoggerInterface $logger The new Logger object. |
89
|
|
|
* |
90
|
|
|
* @return null |
91
|
|
|
*/ |
92
|
|
|
public function setLogger( LoggerInterface $logger ) { |
93
|
|
|
$this->logger = $logger; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|