From 24561fdc1d7032742fb4d5bf83a695eed355a9fb Mon Sep 17 00:00:00 2001 From: aleahy Date: Wed, 14 Aug 2024 03:20:49 +1000 Subject: [PATCH] [8.x] Extract chromedriver regardless of position in archive (#1115) * [8.x] Extract chromedriver regardless of position in archive * [8.x] Style fix * Update doc-block * Update ChromeDriverCommand.php --------- Co-authored-by: Taylor Otwell --- src/Console/ChromeDriverCommand.php | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/Console/ChromeDriverCommand.php b/src/Console/ChromeDriverCommand.php index 8005034a7..445439006 100644 --- a/src/Console/ChromeDriverCommand.php +++ b/src/Console/ChromeDriverCommand.php @@ -95,7 +95,7 @@ public function handle() if ($all || ($os === $currentOS)) { $archive = $this->download($version, $os); - $binary = $this->extract($version, $archive); + $binary = $this->extract($archive); $this->rename($binary, $os); } @@ -213,30 +213,37 @@ protected function download($version, $os) /** * Extract the ChromeDriver binary from the archive and delete the archive. * - * @param string $version * @param string $archive * @return string */ - protected function extract($version, $archive) + protected function extract($archive) { $zip = new ZipArchive; $zip->open($archive); - $zip->extractTo($this->directory); + $binary = null; + + for ($fileIndex = 0; $fileIndex < $zip->numFiles; $fileIndex++) { + $filename = $zip->getNameIndex($fileIndex); - $index = match (true) { - version_compare($version, '115.0', '<') => 0, - version_compare($version, '127.0', '<') => 1, - default => 2, - }; + if (Str::startsWith(basename($filename), 'chromedriver')) { + $binary = $filename; - $binary = $zip->getNameIndex($index); + $zip->extractTo($this->directory, $binary); + + break; + } + } $zip->close(); unlink($archive); + if (! $binary) { + throw new Exception('Could not extract the ChromeDriver binary.'); + } + return $binary; }