Author: Shivam Mathur (shivammathur)
Date: 2024-07-08T06:55:01+05:30

Commit: 
https://github.com/php/web-downloads/commit/132705467bf3098fccff16fedc57a4c075bdfe0e
Raw diff: 
https://github.com/php/web-downloads/commit/132705467bf3098fccff16fedc57a4c075bdfe0e.diff

Fix calling pecl script

Fix routing and validator

Changed paths:
  M  routes.php
  M  src/PeclHandler.php
  M  src/PhpHandler.php
  M  src/Validator.php
  M  src/WinlibsHandler.php
  M  src/scripts/pecl.sh


Diff:

diff --git a/routes.php b/routes.php
index dbd8441..624f643 100644
--- a/routes.php
+++ b/routes.php
@@ -1,6 +1,9 @@
 <?php
 
 use App\IndexHandler;
+use App\PeclHandler;
+use App\PhpHandler;
+use App\WinlibsHandler;
 use App\Router;
 
 $router = new Router();
diff --git a/src/PeclHandler.php b/src/PeclHandler.php
index 9dadf7f..4bcab51 100644
--- a/src/PeclHandler.php
+++ b/src/PeclHandler.php
@@ -1,6 +1,7 @@
 <?php
 
-use App\Validator;
+namespace App;
+
 use Symfony\Component\Process\Exception\ProcessFailedException;
 use Symfony\Component\Process\Process;
 
@@ -13,28 +14,37 @@ 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 {
+        if($this->validate($data)) {
             $this->execute($data);
         }
     }
 
     private function validate(mixed $data): bool
     {
-        return (new Validator)->validate($data, [
+        $validator = new Validator([
             'url' => 'required|url',
             'extension' => 'required|string',
             'ref' => 'required|string',
         ]);
+
+        $validator->validate($data);
+
+        $valid = $validator->isValid();
+
+        if(!$valid) {
+            http_response_code(400);
+            echo 'Invalid request: ' . $validator;
+        }
+
+        return $valid;
     }
 
     private function execute(array $data): void
     {
         extract($data);
-
-        $process = new Process(['sudo -u $SCRIPTS_USER bash', $this->script, 
$url, $extension, $ref, $token ?? '']);
+        $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 {
diff --git a/src/PhpHandler.php b/src/PhpHandler.php
index 61ffbc3..320eb76 100644
--- a/src/PhpHandler.php
+++ b/src/PhpHandler.php
@@ -1,5 +1,7 @@
 <?php
 
+namespace App;
+
 class PhpHandler
 {
 
diff --git a/src/Validator.php b/src/Validator.php
index 345ac19..d63db75 100644
--- a/src/Validator.php
+++ b/src/Validator.php
@@ -4,13 +4,23 @@
 
 class Validator
 {
-    protected array $errors = [];
+    function __construct
+    (
+        protected array $rules,
+        protected array $errors = [],
+        protected bool $valid = false
+    )
+    {
+        //
+    }
+
 
-    public function validate(array $data, array $rules): bool
+
+    public function validate(array $data): void
     {
         $this->errors = [];
 
-        foreach ($rules as $field => $ruleString) {
+        foreach ($this->rules as $field => $ruleString) {
             $rulesArray = explode('|', $ruleString);
             foreach ($rulesArray as $rule) {
                 $ruleParts = explode(':', $rule);
@@ -23,7 +33,12 @@ public function validate(array $data, array $rules): bool
             }
         }
 
-        return empty($this->errors);
+        $this->valid = empty($this->errors);
+    }
+
+    public function isValid(): bool
+    {
+        return $this->valid;
     }
 
     public function errors(): array
@@ -31,6 +46,17 @@ public function errors(): array
         return $this->errors;
     }
 
+    public function __toString(): string
+    {
+        $string = PHP_EOL;
+        foreach ($this->errors as $field => $errors) {
+            foreach ($errors as $error) {
+                $string .= "$field: $error" . PHP_EOL;
+            }
+        }
+        return $string;
+    }
+
     protected function required($value): bool
     {
         return !is_null($value) && $value !== '';
diff --git a/src/WinlibsHandler.php b/src/WinlibsHandler.php
index 8af13c8..ee0b918 100644
--- a/src/WinlibsHandler.php
+++ b/src/WinlibsHandler.php
@@ -1,5 +1,7 @@
 <?php
 
+namespace App;
+
 class WinlibsHandler
 {
 
diff --git a/src/scripts/pecl.sh b/src/scripts/pecl.sh
index 520430c..32c4832 100644
--- a/src/scripts/pecl.sh
+++ b/src/scripts/pecl.sh
@@ -18,13 +18,15 @@ for tool in curl unzip; do
 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
-  if ! unzip "$zip_file" -d "${BUILDS_DIRECTORY:?}"/pecl/releases/; 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
\ No newline at end of file
+fi

Reply via email to