Author: Shivam Mathur (shivammathur) Date: 2024-08-12T06:33:43+05:30 Commit: https://github.com/php/web-downloads/commit/04ec14efc46712b4526b298c185778aa45a6c387 Raw diff: https://github.com/php/web-downloads/commit/04ec14efc46712b4526b298c185778aa45a6c387.diff
Replace script with PHP code Changed paths: A src/FetchArtifact.php D src/scripts/common.sh D src/scripts/pecl.sh D src/scripts/php.sh D src/scripts/winlibs.sh M .env.example M composer.json M composer.lock M src/PeclHandler.php Diff: diff --git a/.env.example b/.env.example index e64e685..68c7f6f 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,2 @@ AUTH_TOKEN= -BUILDS_DIRECTORY= -SCRIPTS_USER= \ No newline at end of file +BUILDS_DIRECTORY= \ No newline at end of file diff --git a/composer.json b/composer.json index 89846d4..13c6367 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ }, "require": { "php": "^8.2", - "vlucas/phpdotenv": "^5.6", - "symfony/process": "^7.1" + "vlucas/phpdotenv": "^5.6.1", + "ext-curl": "*", + "ext-zip": "*" } } diff --git a/composer.lock b/composer.lock index 6fc5314..4cd79e8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,28 +4,28 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "4e249ba8959f6a2c56b273e97dedc5a5", + "content-hash": "2232ff6e8dc2b9f9eef60c1cd337ae70", "packages": [ { "name": "graham-campbell/result-type", - "version": "v1.1.2", + "version": "v1.1.3", "source": { "type": "git", "url": "https://github.com/GrahamCampbell/Result-Type.git", - "reference": "fbd48bce38f73f8a4ec8583362e732e4095e5862" + "reference": "3ba905c11371512af9d9bdd27d99b782216b6945" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/GrahamCampbell/Result-Type/zipball/fbd48bce38f73f8a4ec8583362e732e4095e5862", - "reference": "fbd48bce38f73f8a4ec8583362e732e4095e5862", + "url": "https://api.github.com/repos/GrahamCampbell/Result-Type/zipball/3ba905c11371512af9d9bdd27d99b782216b6945", + "reference": "3ba905c11371512af9d9bdd27d99b782216b6945", "shasum": "" }, "require": { "php": "^7.2.5 || ^8.0", - "phpoption/phpoption": "^1.9.2" + "phpoption/phpoption": "^1.9.3" }, "require-dev": { - "phpunit/phpunit": "^8.5.34 || ^9.6.13 || ^10.4.2" + "phpunit/phpunit": "^8.5.39 || ^9.6.20 || ^10.5.28" }, "type": "library", "autoload": { @@ -54,7 +54,7 @@ ], "support": { "issues": "https://github.com/GrahamCampbell/Result-Type/issues", - "source": "https://github.com/GrahamCampbell/Result-Type/tree/v1.1.2" + "source": "https://github.com/GrahamCampbell/Result-Type/tree/v1.1.3" }, "funding": [ { @@ -66,20 +66,20 @@ "type": "tidelift" } ], - "time": "2023-11-12T22:16:48+00:00" + "time": "2024-07-20T21:45:45+00:00" }, { "name": "phpoption/phpoption", - "version": "1.9.2", + "version": "1.9.3", "source": { "type": "git", "url": "https://github.com/schmittjoh/php-option.git", - "reference": "80735db690fe4fc5c76dfa7f9b770634285fa820" + "reference": "e3fac8b24f56113f7cb96af14958c0dd16330f54" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/schmittjoh/php-option/zipball/80735db690fe4fc5c76dfa7f9b770634285fa820", - "reference": "80735db690fe4fc5c76dfa7f9b770634285fa820", + "url": "https://api.github.com/repos/schmittjoh/php-option/zipball/e3fac8b24f56113f7cb96af14958c0dd16330f54", + "reference": "e3fac8b24f56113f7cb96af14958c0dd16330f54", "shasum": "" }, "require": { @@ -87,13 +87,13 @@ }, "require-dev": { "bamarni/composer-bin-plugin": "^1.8.2", - "phpunit/phpunit": "^8.5.34 || ^9.6.13 || ^10.4.2" + "phpunit/phpunit": "^8.5.39 || ^9.6.20 || ^10.5.28" }, "type": "library", "extra": { "bamarni-bin": { "bin-links": true, - "forward-command": true + "forward-command": false }, "branch-alias": { "dev-master": "1.9-dev" @@ -129,7 +129,7 @@ ], "support": { "issues": "https://github.com/schmittjoh/php-option/issues", - "source": "https://github.com/schmittjoh/php-option/tree/1.9.2" + "source": "https://github.com/schmittjoh/php-option/tree/1.9.3" }, "funding": [ { @@ -141,7 +141,7 @@ "type": "tidelift" } ], - "time": "2023-11-12T21:59:55+00:00" + "time": "2024-07-20T21:41:07+00:00" }, { "name": "symfony/polyfill-ctype", @@ -382,86 +382,25 @@ ], "time": "2024-05-31T15:07:36+00:00" }, - { - "name": "symfony/process", - "version": "v7.1.1", - "source": { - "type": "git", - "url": "https://github.com/symfony/process.git", - "reference": "febf90124323a093c7ee06fdb30e765ca3c20028" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/process/zipball/febf90124323a093c7ee06fdb30e765ca3c20028", - "reference": "febf90124323a093c7ee06fdb30e765ca3c20028", - "shasum": "" - }, - "require": { - "php": ">=8.2" - }, - "type": "library", - "autoload": { - "psr-4": { - "Symfony\\Component\\Process\\": "" - }, - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fab...@symfony.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Executes commands in sub-processes", - "homepage": "https://symfony.com", - "support": { - "source": "https://github.com/symfony/process/tree/v7.1.1" - }, - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2024-05-31T14:57:53+00:00" - }, { "name": "vlucas/phpdotenv", - "version": "v5.6.0", + "version": "v5.6.1", "source": { "type": "git", "url": "https://github.com/vlucas/phpdotenv.git", - "reference": "2cf9fb6054c2bb1d59d1f3817706ecdb9d2934c4" + "reference": "a59a13791077fe3d44f90e7133eb68e7d22eaff2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/2cf9fb6054c2bb1d59d1f3817706ecdb9d2934c4", - "reference": "2cf9fb6054c2bb1d59d1f3817706ecdb9d2934c4", + "url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/a59a13791077fe3d44f90e7133eb68e7d22eaff2", + "reference": "a59a13791077fe3d44f90e7133eb68e7d22eaff2", "shasum": "" }, "require": { "ext-pcre": "*", - "graham-campbell/result-type": "^1.1.2", + "graham-campbell/result-type": "^1.1.3", "php": "^7.2.5 || ^8.0", - "phpoption/phpoption": "^1.9.2", + "phpoption/phpoption": "^1.9.3", "symfony/polyfill-ctype": "^1.24", "symfony/polyfill-mbstring": "^1.24", "symfony/polyfill-php80": "^1.24" @@ -478,7 +417,7 @@ "extra": { "bamarni-bin": { "bin-links": true, - "forward-command": true + "forward-command": false }, "branch-alias": { "dev-master": "5.6-dev" @@ -513,7 +452,7 @@ ], "support": { "issues": "https://github.com/vlucas/phpdotenv/issues", - "source": "https://github.com/vlucas/phpdotenv/tree/v5.6.0" + "source": "https://github.com/vlucas/phpdotenv/tree/v5.6.1" }, "funding": [ { @@ -525,7 +464,7 @@ "type": "tidelift" } ], - "time": "2023-11-12T22:43:29+00:00" + "time": "2024-07-20T21:52:34+00:00" } ], "packages-dev": [], @@ -535,7 +474,9 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": "^8.2" + "php": "^8.2", + "ext-curl": "*", + "ext-zip": "*" }, "platform-dev": [], "plugin-api-version": "2.6.0" diff --git a/src/FetchArtifact.php b/src/FetchArtifact.php new file mode 100644 index 0000000..9279783 --- /dev/null +++ b/src/FetchArtifact.php @@ -0,0 +1,28 @@ +<?php + +namespace App; + +class FetchArtifact +{ + public static function handle($url, $filepath, $token = null): void + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + $fp = fopen($filepath, 'w+'); + curl_setopt($ch, CURLOPT_FILE, $fp); + if (str_contains($url, 'api.github.com')) { + $headers = [ + 'Accept: application/vnd.github+json', + ]; + + if ($token) { + $headers[] = 'Authorization: token ' . $token; + } + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + } + curl_exec($ch); + curl_close($ch); + fclose($fp); + } +} \ No newline at end of file diff --git a/src/PeclHandler.php b/src/PeclHandler.php index 4bcab51..9ed320a 100644 --- a/src/PeclHandler.php +++ b/src/PeclHandler.php @@ -2,14 +2,12 @@ namespace App; -use Symfony\Component\Process\Exception\ProcessFailedException; -use Symfony\Component\Process\Process; +use Exception; +use ZipArchive; class PeclHandler { - protected string $script = 'pecl.sh'; - public function handle(): void { $data = json_decode(file_get_contents('php://input'), true); @@ -41,18 +39,42 @@ private function validate(mixed $data): bool private function execute(array $data): void { - extract($data); - $SCRIPTS_USER = $_ENV['SCRIPTS_USER']; - $this->script = __DIR__ . "/scripts/$this->script"; - $process = new Process(['sudo', '-u', $SCRIPTS_USER, 'bash', $this->script, $extension, $ref, $url, $token ?? '']); - try { - $process->mustRun(function ($type, $buffer): void { - echo $buffer; - }); - } catch (ProcessFailedException $exception) { + extract($data); + $this->fetchExtension($extension, $ref, $url, $token ?? ''); + } catch (Exception $exception) { http_response_code(500); - echo 'Failed to add extension: ' . $exception->getMessage(); + echo 'Error: ' . $exception->getMessage(); + } + } + + /** + * @throws Exception + */ + private function fetchExtension(string $extension, string $ref, string $url, string $token): void + { + $filepath = "/tmp/$extension-$ref.zip"; + FetchArtifact::handle($url, $filepath, $token); + + if(!file_exists($filepath)) { + throw new Exception('Failed to fetch the extension'); } + + $destinationDirectory = $_ENV['BUILDS_DIRECTORY'] . "/pecl/releases"; + + if(!is_dir($destinationDirectory)) { + mkdir($destinationDirectory, 0755, true); + } + + $zip = new ZipArchive(); + + if ($zip->open($filepath) === TRUE) { + $zip->extractTo($destinationDirectory); + $zip->close(); + } else { + throw new Exception('Failed to extract the extension'); + } + + unlink($filepath); } } \ No newline at end of file diff --git a/src/scripts/common.sh b/src/scripts/common.sh deleted file mode 100644 index b9c4c94..0000000 --- a/src/scripts/common.sh +++ /dev/null @@ -1,10 +0,0 @@ -function fetch_artifact() { - local filepath=$1 - local url=$2 - local token=$3 - if [[ "$url" == *api.github.com* ]]; then - curl -H "Accept: application/vnd.github+json" -H "Authorization: token $token" -L -o "$filepath" "$url" - else - curl -L -o "$filepath" "$url" - fi -} \ No newline at end of file diff --git a/src/scripts/pecl.sh b/src/scripts/pecl.sh deleted file mode 100644 index 32c4832..0000000 --- a/src/scripts/pecl.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env bash - -extension=$1 -ref=$2 -url=$3 -token=$4 - -SCRIPT_DIRECTORY="$(cd "$(dirname "$0")" && pwd)" - -source "$SCRIPT_DIRECTORY/../../.env" -source "$SCRIPT_DIRECTORY/common.sh" - -for tool in curl unzip; do - if ! command -v "$tool" &> /dev/null; then - echo "$tool not found" - exit 1 - fi -done - -zip_file="/tmp/$extension-$ref.zip" -dest_dir="${BUILDS_DIRECTORY:?}/pecl/releases" -fetch_artifact "$zip_file" "$url" "$token" -if [[ -e "$zip_file" && "$(file --mime-type -b "$zip_file")" = "application/zip" ]]; then - mkdir -p "$dest_dir" - if ! unzip -o "$zip_file" -d "$dest_dir"; then - echo "Failed to unzip the build" - exit 1 - fi -else - echo "Failed to fetch the build" - exit 1 -fi diff --git a/src/scripts/php.sh b/src/scripts/php.sh deleted file mode 100644 index cbb8080..0000000 --- a/src/scripts/php.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash - -# TODO: Fetch PHP builds diff --git a/src/scripts/winlibs.sh b/src/scripts/winlibs.sh deleted file mode 100644 index 609b940..0000000 --- a/src/scripts/winlibs.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash - -# TODO: Fetch libraries builds