Skip to content

Commit

Permalink
Remove symfony/http-client dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Feb 26, 2024
1 parent ba5693a commit 2703a73
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: apcu, memcached
extensions: apcu, memcached, curl
ini-values: |
apc.enable_cli=1
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"psr/simple-cache": "^3.0",
"symfony/console": "^6.3|^7.0",
"symfony/finder": "^6.3|^7.0",
"symfony/http-client": "^6.3|^7.0",
"symfony/process": "^6.3|^7.0",
"symfony/property-access": "^6.3|^7.0",
"symfony/serializer": "^6.3|^7.0"
Expand All @@ -31,6 +30,7 @@
},
"suggest": {
"ext-apcu": "To use the APCu cache backend",
"ext-curl": "To be able to install Pkl CLI tool with the `install` command",
"ext-memcached": "To use the Memcached cache backend"
},
"bin": [
Expand Down
69 changes: 47 additions & 22 deletions src/Internal/PklDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@
use Phikl\Exception\PklCliAlreadyDownloadedException;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @internal
*/
final class PklDownloader
{
private const PKL_CLI_VERSION = '0.25.2';
private HttpClientInterface $httpClient;

public function __construct()
{
$this->httpClient = HttpClient::create();
if (!\extension_loaded('curl')) {
throw new \RuntimeException('The curl extension is required to download the Pkl CLI. You can either install it or download the Pkl CLI manually.');
}
}

public function alreadyDownloaded(string $location = 'vendor/bin'): bool
Expand All @@ -43,25 +42,10 @@ public function download(SymfonyStyle $io, string $location = 'vendor/bin', bool
throw new \RuntimeException('32-bit systems are not supported by Pkl CLI.');
}

$progressBar = new ProgressBar($io);
// set the progress bar format to display how many megabytes are downloaded
$progressBar->setFormat('verbose');

$downloadUrl = $this->buildDownloadUrl();
$response = $this->httpClient->request('GET', $downloadUrl, [
'on_progress' => function ($dlNow, $dlSize) use ($progressBar) {
// set the progress bar format to display how many megabytes are downloaded
$progressBar->setProgress((int) ($dlNow / 1e+6));
$progressBar->setMaxSteps((int) ($dlSize / 1e+6));
},
]);

if (!is_writable($location) && !mkdir($location, 0755, true) && !is_dir($location)) {
throw new \RuntimeException(sprintf('Pkl CLI could not be installed to %s, ensure the directory is writable.', $location));
}
$pklCliPath = $location.\DIRECTORY_SEPARATOR.'pkl';

$pklCliPath = $location.'/pkl';
file_put_contents($pklCliPath, $response->getContent());
$this->curlUrlToFile($downloadUrl, $location, 'pkl', $io);

if ($this->isMacOs() || $this->isLinux()) {
chmod($pklCliPath, 0755);
Expand All @@ -78,6 +62,47 @@ public function download(SymfonyStyle $io, string $location = 'vendor/bin', bool
}
}

private function curlUrlToFile(string $url, string $location, string $fileName, SymfonyStyle $io): void
{
$curlHandle = \curl_init($url);
\assert($curlHandle !== false);

$file = \fopen($location.\DIRECTORY_SEPARATOR.$fileName, 'w');

if (!is_writable($location) && !mkdir($location, 0755, true) && !is_dir($location) || $file === false) {
throw new \RuntimeException(sprintf('Pkl CLI could not be installed to %s, ensure the location is writable.', $location));
}

$progressBar = new ProgressBar($io);

\curl_setopt($curlHandle, \CURLOPT_FILE, $file);
\curl_setopt($curlHandle, \CURLOPT_FOLLOWLOCATION, true);
\curl_setopt($curlHandle, \CURLOPT_NOPROGRESS, false);
\curl_setopt($curlHandle, \CURLOPT_PROGRESSFUNCTION, function (
mixed $resource,
int $downloadSize,
int $downloaded,
int $uploadSize,
int $uploaded
) use ($progressBar): void {
if ($downloadSize > 0) {
$progressBar->setMaxSteps($downloadSize);
$progressBar->setProgress($downloaded);
}
});

\curl_exec($curlHandle);

if (\curl_errno($curlHandle)) {
\fclose($file);

throw new \RuntimeException(\curl_error($curlHandle));
}

\fclose($file);
\curl_close($curlHandle);
}

private function buildDownloadUrl(): string
{
$downloadUrl = 'https://github.com/apple/pkl/releases/download/'.self::PKL_CLI_VERSION.'/pkl-';
Expand All @@ -87,7 +112,7 @@ private function buildDownloadUrl(): string
return $downloadUrl.($this->isArmArch() ? 'linux-aarch64' : 'linux-amd64');
}

return 'https://repo1.maven.org/maven2/org/pkl-lang/pkl-cli-java/0.25.2/pkl-cli-java-'.self::PKL_CLI_VERSION.'.jar';
return 'https://repo1.maven.org/maven2/org/pkl-lang/pkl-cli-java/'.self::PKL_CLI_VERSION.'/pkl-cli-java-'.self::PKL_CLI_VERSION.'.jar';
}

private function isArmArch(): bool
Expand Down

0 comments on commit 2703a73

Please sign in to comment.