Author: Shivam Mathur (shivammathur)
Date: 2024-07-08T00:28:32+05:30

Commit: 
https://github.com/php/web-downloads/commit/8c61d7dc67313a70d4c48f0bd45a02862f0ad468
Raw diff: 
https://github.com/php/web-downloads/commit/8c61d7dc67313a70d4c48f0bd45a02862f0ad468.diff

Add support for updating PECL extensions

Changed paths:
  M  src/PeclHandler.php
  M  src/scripts/pecl.sh


Diff:

diff --git a/src/PeclHandler.php b/src/PeclHandler.php
index 0310c1d..9dadf7f 100644
--- a/src/PeclHandler.php
+++ b/src/PeclHandler.php
@@ -1,9 +1,48 @@
 <?php
 
+use App\Validator;
+use Symfony\Component\Process\Exception\ProcessFailedException;
+use Symfony\Component\Process\Process;
+
 class PeclHandler
 {
 
-    public function handle()
+    protected string $script = 'pecl.sh';
+
+    public function handle(): void
+    {
+        $data = json_decode(file_get_contents('php://input'), true);
+
+        if(!$this->validate($data)) {
+            http_response_code(400);
+            echo 'Invalid request';
+        } else {
+            $this->execute($data);
+        }
+    }
+
+    private function validate(mixed $data): bool
     {
+        return (new Validator)->validate($data, [
+            'url' => 'required|url',
+            'extension' => 'required|string',
+            'ref' => 'required|string',
+        ]);
+    }
+
+    private function execute(array $data): void
+    {
+        extract($data);
+
+        $process = new Process(['sudo -u $SCRIPTS_USER bash', $this->script, 
$url, $extension, $ref, $token ?? '']);
+
+        try {
+            $process->mustRun(function ($type, $buffer): void {
+                echo $buffer;
+            });
+        } catch (ProcessFailedException $exception) {
+            http_response_code(500);
+            echo 'Failed to add extension: ' . $exception->getMessage();
+        }
     }
 }
\ No newline at end of file
diff --git a/src/scripts/pecl.sh b/src/scripts/pecl.sh
index 222a175..520430c 100644
--- a/src/scripts/pecl.sh
+++ b/src/scripts/pecl.sh
@@ -1,3 +1,30 @@
 #!/usr/bin/env bash
 
-# TODO: Fetch PECL builds
+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"
+fetch_artifact "$zip_file" "$url" "$token"
+if [[ -e "$zip_file" && "$(file --mime-type -b "$zip_file")" = 
"application/zip" ]]; then
+  if ! unzip "$zip_file" -d "${BUILDS_DIRECTORY:?}"/pecl/releases/; then
+    echo "Failed to unzip the build"
+    exit 1
+  fi
+else
+  echo "Failed to fetch the build"
+  exit 1
+fi
\ No newline at end of file

Reply via email to