-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathLauncher.php
More file actions
263 lines (227 loc) · 6.16 KB
/
Launcher.php
File metadata and controls
263 lines (227 loc) · 6.16 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
namespace ChromeDevtoolsProtocol\Instance;
use ChromeDevtoolsProtocol\ContextInterface;
use ChromeDevtoolsProtocol\Exception\LogicException;
use ChromeDevtoolsProtocol\Exception\RuntimeException;
use GuzzleHttp\Exception\ConnectException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;
/**
* Launcher starts new Chrome process.
*
* @author Jakub Kulhan <jakub.kulhan@gmail.com>
*/
class Launcher
{
const DEFAULT_LINUX_EXECUTABLE = "google-chrome";
const DEFAULT_WINDOWS_EXECUTABLE = "chrome";
public static $defaultArgs = [
"--headless",
];
/** @var string */
private $executable;
/** @var int */
private $port;
/** @var string|null */
private $workDir;
/** @var array|null */
private $env;
/** @var mixed */
private $input;
/**
* @param int $port If port <= 0, random available port is used.
*/
public function __construct($port = 0)
{
$this->port = max(0, $port);
}
/**
* Set full path to executable.
*
* @param string $executable
* @return self
*/
public function setExecutable(string $executable)
{
$this->executable = $executable;
return $this;
}
/**
* Set remote debugging port.
*
* @param int $port
* @return self
*/
public function setPort(int $port)
{
$this->port = $port;
return $this;
}
/**
* Working directory where process will be launched.
*
* @param string|null $workDir
* @return self
*/
public function setWorkDir(?string $workDir)
{
$this->workDir = $workDir;
return $this;
}
/**
* Environment variables of launched process.
*
* @param array|null $env If `null`, Chrome process will inherit current process environment variables.
* @return self
*/
public function setEnv(?array $env)
{
$this->env = $env;
return $this;
}
/**
* Input will be passed to `stdin` of launched process.
*
* @param mixed $input
* @return self
*/
public function setInput($input)
{
$this->input = $input;
return $this;
}
/**
* Start new Chrome process.
*
* @param ContextInterface $ctx
* @param array ...$args
* @return ProcessInstance
* @throws \Exception
*/
public function launch(ContextInterface $ctx, ...$args): ProcessInstance
{
if ($this->executable) {
$executable = $this->executable;
} else if (PHP_OS === "Linux") {
$finder = new ExecutableFinder();
$executable = $finder->find(static::DEFAULT_LINUX_EXECUTABLE);
if ($executable === null) {
throw new RuntimeException(sprintf("Executable [%s] not found.", static::DEFAULT_LINUX_EXECUTABLE));
}
} else if (strncasecmp(PHP_OS, "Win", 3) === 0) {
$finder = new ExecutableFinder();
$executable = $finder->find(static::DEFAULT_WINDOWS_EXECUTABLE);
if ($executable === null) {
throw new RuntimeException(sprintf("Executable [%s] not found.", static::DEFAULT_WINDOWS_EXECUTABLE));
}
} else if (PHP_OS === "Darwin") {
$candidateExecutables = [
// Chrome Canary
"/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
// Chrome Stable
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
];
$executable = null;
foreach ($candidateExecutables as $candidateExecutable) {
if (is_executable($candidateExecutable)) {
$executable = $candidateExecutable;
break;
}
}
if ($executable === null) {
throw new RuntimeException(sprintf("No OS X executable found."));
}
} else {
throw new LogicException(sprintf("Operating system [%s] not supported.", PHP_OS));
}
return $this->launchWithExecutable($ctx, $executable, ...$args);
}
/**
* @param ContextInterface $ctx
* @param string $executable
* @param array ...$args
* @return ProcessInstance
* @throws \Exception
*/
private function launchWithExecutable(ContextInterface $ctx, string $executable, ...$args): ProcessInstance
{
$args = array_unique(array_merge(static::$defaultArgs, $args));
$foundPort = null;
foreach ($args as $arg) {
if (strncmp($arg, "--remote-debugging-port=", 24 /* strlen("--remote-debugging-port=") */) === 0) {
$foundPort = (int)substr($arg, 24 /* strlen("--remote-debugging-port=") */);
if ($foundPort !== $this->port) {
throw new LogicException(sprintf(
"You are trying to launch Chrome instance with argument '--remote-debugging-port=%d', however, " .
"%s is created to start instance with port %d. Either do not set '--remote-debugging-port' " .
"in \$args for launch() method, or create %s instance that would use given port number " .
"(by constructor or setPort() method).",
$foundPort,
get_class($this),
$this->port,
get_class($this)
));
}
}
}
if ($foundPort === null) {
$args[] = "--remote-debugging-port=" . $this->port;
}
$foundUserDataDir = false;
foreach ($args as $arg) {
if (strncmp($arg, "--user-data-dir=", 16 /* strlen("--user-data-dir=") */) === 0) {
$foundUserDataDir = true;
break;
}
}
$fs = new Filesystem();
$temporaryUserDataDir = null;
if (!$foundUserDataDir) {
$temporaryUserDataDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "chrome-profile-" . $this->port;
if ($this->port === 0) {
$temporaryUserDataDir .= "-" . bin2hex(random_bytes(8));
}
$fs->mkdir($temporaryUserDataDir);
$args[] = "--user-data-dir=" . $temporaryUserDataDir;
}
try {
$process = new Process(
array_merge([$executable], $args),
$this->workDir,
$this->env,
$this->input,
null
);
$process->start();
if ($this->port === 0) {
$process->waitUntil(function ($type, $buffer) {
if (preg_match('~DevTools listening on ws://.+:(\d+)/devtools~', $buffer, $m)) {
$this->port = (int)$m[1];
return true;
}
return false;
});
}
$instance = new ProcessInstance($process, $temporaryUserDataDir, $this->port);
for (; ;) {
try {
$instance->version($ctx);
return $instance;
} catch (ConnectException $e) {
if ($ctx->isAfterDeadline()) {
$instance->close();
throw $e;
}
usleep(10);
continue;
}
}
} catch (\Exception $e) {
if ($temporaryUserDataDir !== null) {
$fs->remove($temporaryUserDataDir);
}
throw $e;
}
}
}