Skip to content

Commit

Permalink
[8.x] Extract chromedriver regardless of position in archive (#1115)
Browse files Browse the repository at this point in the history
* [8.x] Extract chromedriver regardless of position in archive

* [8.x] Style fix

* Update doc-block

* Update ChromeDriverCommand.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
aleahy and taylorotwell authored Aug 13, 2024
1 parent 5f98f3c commit 24561fd
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/Console/ChromeDriverCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 24561fd

Please sign in to comment.