1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Moodle component manager. |
5
|
|
|
* |
6
|
|
|
* @author Luke Carrier <[email protected]> |
7
|
|
|
* @copyright 2016 Luke Carrier |
8
|
|
|
* @license GPL-3.0+ |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace ComponentManager; |
12
|
|
|
|
13
|
|
|
use Http\Client\HttpClient as HttplugClient; |
14
|
|
|
use Http\Message\MessageFactory; |
15
|
|
|
use Http\Message\UriFactory; |
16
|
|
|
use Psr\Http\Message\RequestInterface; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Psr\Http\Message\StreamInterface; |
19
|
|
|
use Psr\Http\Message\UriInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Slightly sweeter HTTPlug. |
23
|
|
|
*/ |
24
|
|
|
class HttpClient { |
25
|
|
|
/** |
26
|
|
|
* HTTP client. |
27
|
|
|
* |
28
|
|
|
* @var HttplugClient |
29
|
|
|
*/ |
30
|
|
|
protected $client; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* HTTP message factory. |
34
|
|
|
* |
35
|
|
|
* @var MessageFactory |
36
|
|
|
*/ |
37
|
|
|
protected $messageFactory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* HTTP URI factory. |
41
|
|
|
* |
42
|
|
|
* @var UriFactory |
43
|
|
|
*/ |
44
|
|
|
protected $uriFactory; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* HttpClient constructor. |
48
|
|
|
* |
49
|
|
|
* @param HttplugClient $client |
50
|
|
|
* @param MessageFactory $messageFactory |
51
|
|
|
* @param UriFactory $uriFactory |
52
|
|
|
*/ |
53
|
|
|
public function __construct(HttplugClient $client, MessageFactory $messageFactory, UriFactory $uriFactory) { |
|
|
|
|
54
|
|
|
$this->client = $client; |
55
|
|
|
$this->messageFactory = $messageFactory; |
56
|
|
|
$this->uriFactory = $uriFactory; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Send request. |
61
|
|
|
* |
62
|
|
|
* @param RequestInterface $request |
63
|
|
|
* |
64
|
|
|
* @return ResponseInterface |
65
|
|
|
*/ |
66
|
|
|
public function sendRequest(RequestInterface $request) { |
67
|
|
|
return $this->client->sendRequest($request); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create a request. |
72
|
|
|
* |
73
|
|
|
* @param string $method |
74
|
|
|
* @param UriInterface|string $uri |
75
|
|
|
* @param mixed[] $headers |
76
|
|
|
* @param resource|string|StreamInterface|null $body |
77
|
|
|
* @param string $protocolVersion |
78
|
|
|
* |
79
|
|
|
* @return RequestInterface |
80
|
|
|
*/ |
81
|
|
|
public function createRequest($method, $uri, $headers=[], $body=null, |
82
|
|
|
$protocolVersion='1.1') { |
83
|
|
|
return $this->messageFactory->createRequest( |
84
|
|
|
$method, $uri, $headers, $body, $protocolVersion); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Create a URI. |
89
|
|
|
* |
90
|
|
|
* @param URIInterface|string $uri |
91
|
|
|
* |
92
|
|
|
* @return UriInterface |
93
|
|
|
*/ |
94
|
|
|
public function createUri($uri) { |
95
|
|
|
return $this->uriFactory->createUri($uri); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.