-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathAutoDiscover.php
More file actions
89 lines (74 loc) · 2.29 KB
/
AutoDiscover.php
File metadata and controls
89 lines (74 loc) · 2.29 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
<?php
/*
* This file is part of Chrome PHP.
*
* (c) Soufiane Ghzal <sghzal@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace HeadlessChromium;
class AutoDiscover
{
/**
* @var callable(): string
*/
private $osFamily;
/**
* @param (callable(): string)|null $osFamily
*/
public function __construct(?callable $osFamily = null)
{
$this->osFamily = $osFamily ?? function (): string {
return \PHP_OS_FAMILY;
};
}
public function guessChromeBinaryPath(): string
{
if (\array_key_exists('CHROME_PATH', $_SERVER)) {
return $_SERVER['CHROME_PATH'];
}
switch (($this->osFamily)()) {
case 'Darwin':
return '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
case 'Windows':
return self::getFromRegistry() ?? '%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe';
default:
return \rtrim(\explode("\n", (string) self::shellExec('command -v google-chrome chromium-browser chrome chromium'), 2)[0]) ?: 'chrome';
}
}
private static function getFromRegistry(): ?string
{
$registryKey = self::shellExec(
'reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe" /ve'
);
if (null === $registryKey) {
return null;
}
\preg_match('/.:(?!.*:).*/', $registryKey, $matches);
return $matches[0] ?? null;
}
private static function shellExec(string $command): ?string
{
try {
$result = @\shell_exec($command);
return \is_string($result) ? $result : null;
} catch (\Throwable $e) {
return null;
}
}
/**
* Get default browser options from environment variables.
*
* @return array<string, mixed>
*/
public function getDefaultOptions(): array
{
$options = [];
if (\array_key_exists('CHROME_NO_SANDBOX', $_SERVER)
&& \filter_var($_SERVER['CHROME_NO_SANDBOX'], \FILTER_VALIDATE_BOOLEAN)) {
$options['noSandbox'] = true;
}
return $options;
}
}