1 | <?php |
||||||
2 | |||||||
3 | namespace Staudenmeir\DuskUpdater; |
||||||
4 | |||||||
5 | use Illuminate\Support\Str; |
||||||
6 | use Symfony\Component\Process\Process; |
||||||
7 | |||||||
8 | trait DetectsChromeVersion |
||||||
9 | { |
||||||
10 | /** |
||||||
11 | * The default commands to detect the installed Chrome/Chromium version. |
||||||
12 | * |
||||||
13 | * @var array |
||||||
14 | */ |
||||||
15 | protected static $platforms = [ |
||||||
16 | 'linux' => [ |
||||||
17 | 'slug' => 'linux64', |
||||||
18 | 'commands' => [ |
||||||
19 | '/usr/bin/google-chrome --version', |
||||||
20 | '/usr/bin/chromium-browser --version', |
||||||
21 | '/usr/bin/chromium --version', |
||||||
22 | '/usr/bin/google-chrome-stable --version', |
||||||
23 | ], |
||||||
24 | ], |
||||||
25 | 'mac' => [ |
||||||
26 | 'slug' => 'mac-x64', |
||||||
27 | 'commands' => [ |
||||||
28 | '/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing --version', |
||||||
29 | '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version', |
||||||
30 | ], |
||||||
31 | ], |
||||||
32 | 'mac-intel' => [ |
||||||
33 | 'slug' => 'mac-x64', |
||||||
34 | 'commands' => [ |
||||||
35 | '/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing --version', |
||||||
36 | '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version', |
||||||
37 | ], |
||||||
38 | ], |
||||||
39 | 'mac-arm' => [ |
||||||
40 | 'slug' => 'mac-arm64', |
||||||
41 | 'commands' => [ |
||||||
42 | '/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing --version', |
||||||
43 | '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version', |
||||||
44 | ], |
||||||
45 | ], |
||||||
46 | 'win' => [ |
||||||
47 | 'slug' => 'win32', |
||||||
48 | 'commands' => [ |
||||||
49 | 'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version', |
||||||
50 | ], |
||||||
51 | ], |
||||||
52 | ]; |
||||||
53 | |||||||
54 | /** |
||||||
55 | * Detect the installed Chrome/Chromium version. |
||||||
56 | * |
||||||
57 | * @param string $os |
||||||
58 | * @return int|bool |
||||||
59 | */ |
||||||
60 | 3 | protected function chromeVersion($os) |
|||||
61 | { |
||||||
62 | 3 | $path = $this->option('detect'); |
|||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
63 | |||||||
64 | 3 | if ($path) { |
|||||
65 | 2 | if ($os === 'win') { |
|||||
66 | $this->error('Chrome version cannot be detected in custom installation path on Windows.'); |
||||||
0 ignored issues
–
show
It seems like
error() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
67 | |||||||
68 | return false; |
||||||
69 | } |
||||||
70 | |||||||
71 | 2 | $commands = [$path.' --version']; |
|||||
72 | } else { |
||||||
73 | 1 | $commands = static::$platforms[$os]['commands']; |
|||||
74 | } |
||||||
75 | |||||||
76 | 3 | foreach ($commands as $command) { |
|||||
77 | 3 | $process = Process::fromShellCommandline($command); |
|||||
78 | |||||||
79 | 3 | $process->run(); |
|||||
80 | |||||||
81 | 3 | preg_match('/(\d+)(\.\d+){3}/', $process->getOutput(), $matches); |
|||||
82 | |||||||
83 | 3 | if (!isset($matches[1])) { |
|||||
84 | 1 | continue; |
|||||
85 | } |
||||||
86 | |||||||
87 | 2 | $this->comment( |
|||||
0 ignored issues
–
show
It seems like
comment() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
88 | 2 | sprintf('Chrome version %s detected.', $matches[0]) |
|||||
89 | 2 | ); |
|||||
90 | |||||||
91 | 2 | return (int) $matches[1]; |
|||||
92 | } |
||||||
93 | |||||||
94 | 1 | $this->error('Chrome version could not be detected. Please submit an issue: https://github.com/staudenmeir/dusk-updater'); |
|||||
95 | |||||||
96 | 1 | return false; |
|||||
97 | } |
||||||
98 | |||||||
99 | /** |
||||||
100 | * Resolve the ChromeDriver slug for the given operating system. |
||||||
101 | * |
||||||
102 | * @param string $operatingSystem |
||||||
103 | * @param string|null $version |
||||||
104 | * @return string |
||||||
105 | */ |
||||||
106 | 6 | public static function chromeDriverSlug($operatingSystem, $version = null) |
|||||
107 | { |
||||||
108 | 6 | $slug = static::$platforms[$operatingSystem]['slug'] ?? null; |
|||||
109 | |||||||
110 | 6 | if (!is_null($version) && version_compare($version, '115.0', '<')) { |
|||||
111 | 3 | if ($slug === 'mac-arm64') { |
|||||
112 | return version_compare($version, '106.0.5249', '<') ? 'mac64_m1' : 'mac_arm64'; |
||||||
113 | 3 | } elseif ($slug === 'mac-x64') { |
|||||
114 | 3 | return 'mac64'; |
|||||
115 | } |
||||||
116 | } |
||||||
117 | |||||||
118 | 6 | return $slug; |
|||||
119 | } |
||||||
120 | |||||||
121 | /** |
||||||
122 | * Get all supported operating systems. |
||||||
123 | * |
||||||
124 | * @return array |
||||||
125 | */ |
||||||
126 | 6 | public static function all() |
|||||
127 | { |
||||||
128 | 6 | return array_keys(static::$platforms); |
|||||
129 | } |
||||||
130 | |||||||
131 | /** |
||||||
132 | * Get the current operating system identifier. |
||||||
133 | * |
||||||
134 | * @return string |
||||||
135 | */ |
||||||
136 | 8 | public static function os() |
|||||
137 | { |
||||||
138 | 8 | if (static::onWindows()) { |
|||||
139 | return 'win'; |
||||||
140 | 8 | } elseif (static::onMac()) { |
|||||
141 | return static::macArchitectureId(); |
||||||
142 | } |
||||||
143 | |||||||
144 | 8 | return 'linux'; |
|||||
145 | } |
||||||
146 | |||||||
147 | /** |
||||||
148 | * Determine if the operating system is Windows or Windows Subsystem for Linux. |
||||||
149 | * |
||||||
150 | * @return bool |
||||||
151 | */ |
||||||
152 | 8 | public static function onWindows() |
|||||
153 | { |
||||||
154 | 8 | return PHP_OS === 'WINNT' || Str::contains(php_uname(), 'Microsoft'); |
|||||
155 | } |
||||||
156 | |||||||
157 | /** |
||||||
158 | * Determine if the operating system is macOS. |
||||||
159 | * |
||||||
160 | * @return bool |
||||||
161 | */ |
||||||
162 | 8 | public static function onMac() |
|||||
163 | { |
||||||
164 | 8 | return PHP_OS === 'Darwin'; |
|||||
165 | } |
||||||
166 | |||||||
167 | /** |
||||||
168 | * Get the current macOS platform architecture. |
||||||
169 | * |
||||||
170 | * @return string |
||||||
171 | */ |
||||||
172 | public static function macArchitectureId() |
||||||
173 | { |
||||||
174 | switch (php_uname('m')) { |
||||||
175 | case 'arm64': |
||||||
176 | return 'mac-arm'; |
||||||
177 | case 'x86_64': |
||||||
178 | return 'mac-intel'; |
||||||
179 | default: |
||||||
180 | return 'mac'; |
||||||
181 | } |
||||||
182 | } |
||||||
183 | } |
||||||
184 |