Skip to content

Commit 5863fc5

Browse files
committed
DevtoolsClient allow constructor injection of WebSocketClient
1 parent b80e1c5 commit 5863fc5

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/ChromeDevtoolsProtocol/DevtoolsClient.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,18 @@ class DevtoolsClient implements DevtoolsClientInterface, InternalClientInterface
3434
/** @var object[][] */
3535
private $eventBuffers = [];
3636

37-
public function __construct(string $wsUrl)
37+
public function __construct(WebSocketClient $wsClient)
3838
{
39-
$this->wsClient = new WebSocketClient($wsUrl, "http://" . parse_url($wsUrl, PHP_URL_HOST));
40-
if (!$this->wsClient->connect()) {
39+
$this->wsClient = $wsClient;
40+
}
41+
42+
public static function createFromDebuggerUrl(string $wsUrl)
43+
{
44+
$wsClient = new WebSocketClient($wsUrl, "http://" . parse_url($wsUrl, PHP_URL_HOST));
45+
if (!$wsClient->connect()) {
4146
throw new RuntimeException(sprintf("Could not connect to [%s].", $wsUrl));
4247
}
48+
return new self($wsClient);
4349
}
4450

4551
public function __destruct()

src/ChromeDevtoolsProtocol/Instance/Instance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function createSession(ContextInterface $ctx, string $url = "about:blank"
103103
throw new RuntimeException("No browser debugger URL. Try upgrading to newer version of Chrome.");
104104
}
105105

106-
$browser = new DevtoolsClient($version->webSocketDebuggerUrl);
106+
$browser = DevtoolsClient::createFromDebuggerUrl($version->webSocketDebuggerUrl);
107107
try {
108108
$browserContextId = $browser->target()->createBrowserContext($ctx)->browserContextId;
109109
try {

src/ChromeDevtoolsProtocol/Instance/Tab.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function __construct($data, InternalInstanceInterface $internalInstance)
6060
*/
6161
public function devtools(): DevtoolsClientInterface
6262
{
63-
return new DevtoolsClient($this->webSocketDebuggerUrl);
63+
return DevtoolsClient::createFromDebuggerUrl($this->webSocketDebuggerUrl);
6464
}
6565

6666
/**

0 commit comments

Comments
 (0)