forked from clue/reactphp-buzz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowser.php
More file actions
154 lines (129 loc) · 4.73 KB
/
Browser.php
File metadata and controls
154 lines (129 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Clue\React\Buzz;
use Clue\React\Buzz\Io\Sender;
use Clue\React\Buzz\Io\Transaction;
use Clue\React\Buzz\Message\MessageFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
use React\EventLoop\LoopInterface;
use React\Socket\ConnectorInterface;
class Browser
{
private $sender;
private $messageFactory;
private $baseUri = null;
private $options = array();
/**
* Instantiate the Browser
*
* @param LoopInterface $loop
* @param ConnectorInterface|Sender|null $connector [optional] Connector to
* use. Should be `null` in order to use default Connector. Passing a
* Sender instance is deprecated and only supported for BC reasons and
* will be removed in future versions.
* @param MessageFactory $messageFactory [internal] Only used internally and
* will be removed in future versions.
*/
public function __construct(LoopInterface $loop, $connector = null, MessageFactory $messageFactory = null)
{
if (!$connector instanceof Sender) {
$connector = Sender::createFromLoop($loop, $connector);
}
if ($messageFactory === null) {
$messageFactory = new MessageFactory();
}
$this->sender = $connector;
$this->messageFactory = $messageFactory;
}
public function get($url, $headers = array())
{
return $this->send($this->messageFactory->request('GET', $url, $headers));
}
public function post($url, $headers = array(), $content = '')
{
return $this->send($this->messageFactory->request('POST', $url, $headers, $content));
}
public function head($url, $headers = array())
{
return $this->send($this->messageFactory->request('HEAD', $url, $headers));
}
public function patch($url, $headers = array(), $content = '')
{
return $this->send($this->messageFactory->request('PATCH', $url , $headers, $content));
}
public function put($url, $headers = array(), $content = '')
{
return $this->send($this->messageFactory->request('PUT', $url, $headers, $content));
}
public function delete($url, $headers = array(), $content = '')
{
return $this->send($this->messageFactory->request('DELETE', $url, $headers, $content));
}
public function submit($url, array $fields, $headers = array(), $method = 'POST')
{
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
$content = http_build_query($fields);
return $this->send($this->messageFactory->request($method, $url, $headers, $content));
}
public function send(RequestInterface $request)
{
if ($this->baseUri !== null) {
// ensure we're actually below the base URI
$request = $request->withUri($this->messageFactory->expandBase($request->getUri(), $this->baseUri));
}
$transaction = new Transaction($request, $this->sender, $this->options, $this->messageFactory);
return $transaction->send();
}
/**
* Creates a new Browser instance with the given absolute base URI
*
* This is mostly useful for using (RESTful) HTTP APIs.
* Any relative URI passed to any of the request methods will simply be
* appended behind the given `$baseUri`.
*
* By definition of this library, a given base URI MUST always absolute and
* can not contain any placeholders.
*
* @param string|UriInterface $baseUri absolute base URI
* @return self
* @throws InvalidArgumentException if the given $baseUri is not a valid absolute URI
* @see self::withoutBase()
*/
public function withBase($baseUri)
{
$browser = clone $this;
$browser->baseUri = $this->messageFactory->uri($baseUri);
if ($browser->baseUri->getScheme() === '' || $browser->baseUri->getHost() === '') {
throw new \InvalidArgumentException('Base URI must be absolute');
}
return $browser;
}
/**
* Creates a new Browser instance *without* a base URL
*
* @return self
* @see self::withBase()
*/
public function withoutBase()
{
$browser = clone $this;
$browser->baseUri = null;
return $browser;
}
public function withOptions(array $options)
{
$browser = clone $this;
// merge all options, but remove those explicitly assigned a null value
$browser->options = array_filter($options + $this->options, function ($value) {
return ($value !== null);
});
return $browser;
}
/** @deprecated */
public function withSender(Sender $sender)
{
$browser = clone $this;
$browser->sender = $sender;
return $browser;
}
}