-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathDevtoolsClient.php
More file actions
207 lines (169 loc) · 4.83 KB
/
DevtoolsClient.php
File metadata and controls
207 lines (169 loc) · 4.83 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace ChromeDevtoolsProtocol;
use ChromeDevtoolsProtocol\Exception\ClientClosedException;
use ChromeDevtoolsProtocol\Exception\ErrorException;
use ChromeDevtoolsProtocol\Exception\LogicException;
use ChromeDevtoolsProtocol\Exception\RuntimeException;
use ChromeDevtoolsProtocol\WebSocket\WebSocketClient;
use Wrench\Client;
use Wrench\Payload\Payload;
/**
* Connects to given WebSocket URL using Chrome DevTools Protocol.
*
* @author Jakub Kulhan <jakub.kulhan@gmail.com>
*/
class DevtoolsClient implements DevtoolsClientInterface, InternalClientInterface
{
use DevtoolsClientTrait;
/** @var callable[][] */
private $listeners = [];
/** @var Client|null */
private $wsClient;
/** @var int */
private $messageId = 0;
/** @var object[] */
private $commandResults = [];
/** @var object[][] */
private $eventBuffers = [];
public function __construct(WebSocketClient $wsClient)
{
$this->wsClient = $wsClient;
}
public static function createFromDebuggerUrl(string $wsUrl)
{
$wsClient = new WebSocketClient($wsUrl, "http://" . parse_url($wsUrl, PHP_URL_HOST));
if (!$wsClient->connect()) {
throw new RuntimeException(sprintf("Could not connect to [%s].", $wsUrl));
}
return new self($wsClient);
}
public function __destruct()
{
if ($this->wsClient !== null) {
throw new LogicException(sprintf(
"You must call [%s::%s] method to release underlying WebSocket connection.",
__CLASS__,
"close"
));
}
}
public function close(): void
{
$wsClient = $this->wsClient;
$this->wsClient = null;
$wsClient->disconnect();
}
/**
* @internal
*/
public function executeCommand(ContextInterface $ctx, string $method, $parameters)
{
$messageId = ++$this->messageId;
$payload = new \stdClass();
$payload->id = $messageId;
$payload->method = $method;
$payload->params = $parameters;
$this->getWsClient()->setDeadline($ctx->getDeadline());
$this->getWsClient()->sendData(json_encode($payload));
for (; ;) {
$this->getWsClient()->setDeadline($ctx->getDeadline());
foreach ($this->getWsClient()->receive() ?: [] as $payload) {
/** @var Payload $payload */
$message = json_decode($payload->getPayload());
$this->handleMessage($message);
}
if (isset($this->commandResults[$messageId])) {
$result = $this->commandResults[$messageId];
unset($this->commandResults[$messageId]);
return $result;
}
}
}
/**
* @internal
*/
public function awaitEvent(ContextInterface $ctx, string $method)
{
if (!empty($this->eventBuffers[$method])) {
return array_shift($this->eventBuffers[$method])->params;
}
$this->eventBuffers = [];
for (; ;) {
$this->getWsClient()->setDeadline($ctx->getDeadline());
foreach ($this->getWsClient()->receive() ?: [] as $payload) {
/** @var Payload $payload */
$message = json_decode($payload->getPayload());
$this->handleMessage($message);
}
if (!empty($this->eventBuffers[$method])) {
return array_shift($this->eventBuffers[$method])->params;
}
}
}
private function handleMessage($message)
{
if (isset($message->error)) {
throw new ErrorException($message->error->message, $message->error->code);
} else if (isset($message->method)) {
if (isset($this->listeners[$message->method])) {
foreach ($this->listeners[$message->method] as $callback) {
$callback($message->params);
}
}
if (!isset($this->eventBuffers[$message->method])) {
$this->eventBuffers[$message->method] = [];
}
array_push($this->eventBuffers[$message->method], $message);
} else if (isset($message->id)) {
$this->commandResults[$message->id] = $message->result ?? new \stdClass();
} else {
throw new RuntimeException(sprintf(
"Unhandled message: %s",
json_encode($message, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
));
}
return null;
}
private function getWsClient()
{
if ($this->wsClient === null) {
throw new ClientClosedException("Client has been closed.");
}
return $this->wsClient;
}
/**
* @internal
*/
public function addListener(string $method, callable $listener): SubscriptionInterface
{
if (!isset($this->listeners[$method])) {
$this->listeners[$method] = [];
}
$this->listeners[$method][] = $listener;
$callback = function () use ($method, $listener) {
foreach ($this->listeners[$method] as $k => $candidateListener) {
if ($candidateListener === $listener) {
unset($this->listeners[$method][$k]);
break;
}
}
if (empty($this->listeners[$method])) {
unset($this->listeners[$method]);
}
};
return new class($callback) implements SubscriptionInterface
{
/** @var callable */
private $callback;
public function __construct(callable $callable)
{
$this->callback = $callable;
}
public function cancel(): void
{
$callback = $this->callback;
$callback();
}
};
}
}