Author: Shivam Mathur (shivammathur) Date: 2024-11-25T05:17:08+05:30 Commit: https://github.com/php/web-downloads/commit/c093f257746aa79d8921065663f280be3329e605 Raw diff: https://github.com/php/web-downloads/commit/c093f257746aa79d8921065663f280be3329e605.diff
Refactor for code style Changed paths: M bootstrap.php M src/Actions/GetArtifacts.php M src/Console/Command.php M src/Console/Command/PeclCommand.php M src/Console/Command/PhpCommand.php M src/Console/Command/WinlibsCommand.php M src/Http/BaseController.php M src/Http/Controllers/IndexController.php M src/Http/Controllers/PeclController.php M src/Http/Controllers/PhpController.php M src/Http/Controllers/WinlibsController.php M src/Validator.php Diff: diff --git a/bootstrap.php b/bootstrap.php index 4c219e9..f2eb322 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -1,3 +1,3 @@ <?php -require_once __DIR__.'/routes.php'; +require_once __DIR__ . '/routes.php'; diff --git a/src/Actions/GetArtifacts.php b/src/Actions/GetArtifacts.php index b1f03d5..140480e 100644 --- a/src/Actions/GetArtifacts.php +++ b/src/Actions/GetArtifacts.php @@ -11,7 +11,7 @@ public static function handle($workflow_run_id, $token): void $base_url = "https://api.github.com/repos/"; $repo = "winlibs/winlib-builder"; - + curl_setopt_array($ch, [ CURLOPT_URL => "$base_url/$repo/actions/runs/$workflow_run_id/artifacts", CURLOPT_RETURNTRANSFER => true, @@ -22,11 +22,11 @@ public static function handle($workflow_run_id, $token): void "User-Agent: PHP Web Downloads", ], ]); - + $response = curl_exec($ch); $err = curl_error($ch); curl_close($ch); - + if ($err) { echo "cURL Error #:" . $err; } else { diff --git a/src/Console/Command.php b/src/Console/Command.php index 84e83d3..fcb12eb 100644 --- a/src/Console/Command.php +++ b/src/Console/Command.php @@ -4,9 +4,9 @@ abstract class Command { - public const SUCCESS = 0; - public const FAILURE = 1; - public const INVALID = 2; + public const int SUCCESS = 0; + public const int FAILURE = 1; + public const int INVALID = 2; protected string $signature = ''; diff --git a/src/Console/Command/PeclCommand.php b/src/Console/Command/PeclCommand.php index cd813e0..807a785 100644 --- a/src/Console/Command/PeclCommand.php +++ b/src/Console/Command/PeclCommand.php @@ -12,10 +12,11 @@ class PeclCommand extends Command protected string $description = 'Add pecl extensions'; - public function handle(): int { + public function handle(): int + { try { $baseDirectory = $this->getOption('base-directory'); - if(!$baseDirectory) { + if (!$baseDirectory) { throw new Exception('Base directory is required'); } @@ -26,7 +27,7 @@ public function handle(): int { $filteredFiles = []; foreach ($files as $filepath) { $lockFile = $filepath . '.lock'; - if(!file_exists($lockFile)) { + if (!file_exists($lockFile)) { touch($lockFile); $filteredFiles[] = $filepath; } diff --git a/src/Console/Command/PhpCommand.php b/src/Console/Command/PhpCommand.php index def12e1..005febc 100644 --- a/src/Console/Command/PhpCommand.php +++ b/src/Console/Command/PhpCommand.php @@ -14,10 +14,11 @@ class PhpCommand extends Command protected ?string $baseDirectory = null; - public function handle(): int { + public function handle(): int + { try { $this->baseDirectory = $this->getOption('base-directory'); - if(!$this->baseDirectory) { + if (!$this->baseDirectory) { throw new Exception('Base directory is required'); } @@ -28,7 +29,7 @@ public function handle(): int { $filteredFiles = []; foreach ($files as $filepath) { $lockFile = $filepath . '.lock'; - if(!file_exists($lockFile)) { + if (!file_exists($lockFile)) { touch($lockFile); $filteredFiles[] = $filepath; } @@ -38,7 +39,7 @@ public function handle(): int { $hash = hash('sha256', $filepath) . strtotime('now'); $tempDirectory = "/tmp/php-" . $hash; - if(is_dir($tempDirectory)) { + if (is_dir($tempDirectory)) { rmdir($tempDirectory); } mkdir($tempDirectory, 0755, true); @@ -46,7 +47,7 @@ public function handle(): int { $zip = new ZipArchive(); if ($zip->open($filepath) === TRUE) { - if($zip->extractTo($tempDirectory) === FALSE) { + if ($zip->extractTo($tempDirectory) === FALSE) { throw new Exception('Failed to extract the extension build'); } $zip->close(); @@ -87,7 +88,7 @@ private function getDestinationDirectory(string $tempDirectory): string private function moveBuild(string $tempDirectory, string $destinationDirectory): void { $files = glob($tempDirectory . '/*'); - if($files) { + if ($files) { $version = $this->getFileVersion($files[0]); foreach ($files as $file) { $fileName = basename($file); @@ -107,9 +108,9 @@ private function copyBuildsToArchive(string $directory, string $version): void $files = glob($directory . '/php*-' . $version_short . '-*.zip'); foreach ($files as $file) { $fileVersion = $this->getFileVersion($file); - if($fileVersion) { + if ($fileVersion) { copy($directory . '/' . basename($file), $directory . '/archive/' . $file); - if(version_compare($fileVersion, $version) < 0) { + if (version_compare($fileVersion, $version) < 0) { unlink($file); } } @@ -158,7 +159,7 @@ private function generateListing(string $directory): void 'installer' => 'php-' . $namingPattern . '.msi', 'test_pack' => 'php-test-pack-' . $parts['version'] . '.zip', ]; - foreach($build_types as $type => $fileName) { + foreach ($build_types as $type => $fileName) { $filePath = $directory . '/' . $fileName; if (file_exists($filePath)) { $releases[$version_short][$type] = [ @@ -170,7 +171,7 @@ private function generateListing(string $directory): void } $this->updateReleasesJson($releases, $directory); - if($directory === $this->baseDirectory . '/releases') { + if ($directory === $this->baseDirectory . '/releases') { $this->updateLatestBuilds($releases, $directory); } } @@ -182,7 +183,7 @@ private function updateReleasesJson(array $releases, string $directory): void { foreach ($releases as &$release) { foreach ($release as &$build_type) { - if (! is_array($build_type) || ! isset($build_type['mtime'])) { + if (!is_array($build_type) || !isset($build_type['mtime'])) { continue; } @@ -234,7 +235,7 @@ private function bytes2string(int $size): float $total = count($sizes); - while($total-- && $size > 1024) $size /= 1024; + while ($total-- && $size > 1024) $size /= 1024; return round($size, 2) . $sizes[$total]; } @@ -273,12 +274,12 @@ private function parseFileName($fileName): array $ts = is_numeric($parts[$t]) ? $parts[$t] : false; return [ - 'version' => $version, - 'version_short' => substr($version, 0, 3), - 'nts' => $nts, - 'vc' => $vc, - 'arch' => $arch, - 'ts' => $ts + 'version' => $version, + 'version_short' => substr($version, 0, 3), + 'nts' => $nts, + 'vc' => $vc, + 'arch' => $arch, + 'ts' => $ts ]; } } \ No newline at end of file diff --git a/src/Console/Command/WinlibsCommand.php b/src/Console/Command/WinlibsCommand.php index b0bbcb9..92543cc 100644 --- a/src/Console/Command/WinlibsCommand.php +++ b/src/Console/Command/WinlibsCommand.php @@ -12,10 +12,11 @@ class WinlibsCommand extends Command protected ?string $baseDirectory = null; - public function handle(): int { + public function handle(): int + { try { $this->baseDirectory = $this->getOption('base-directory'); - if(!$this->baseDirectory) { + if (!$this->baseDirectory) { throw new Exception('Base directory is required'); } @@ -26,18 +27,18 @@ public function handle(): int { $filteredDirectories = []; foreach ($buildDirectories as $directoryPath) { $lockFile = $directoryPath . '.lock'; - if(!file_exists($lockFile)) { + if (!file_exists($lockFile)) { touch($lockFile); $filteredDirectories[] = $directoryPath; } } - foreach($filteredDirectories as $directoryPath) { + foreach ($filteredDirectories as $directoryPath) { $data = json_decode(file_get_contents($directoryPath . '/data.json'), true); extract($data); $files = glob($directoryPath . '/*.zip'); $files = $this->parseFiles($files); - if($files) { + if ($files) { $this->copyFiles($files, $library, $ref, $vs_version_targets); $this->updateSeriesFiles($files, $library, $ref, $php_versions, $vs_version_targets, $stability); } @@ -72,14 +73,14 @@ private function parseFiles(array $files): array return $data; } - private function copyFiles(array $files, $library, $ref, $vs_version_targets): void + private function copyFiles(array $files, string $library, string $ref, string $vs_version_targets): void { $baseDirectory = $this->baseDirectory . "/php-sdk/deps"; - if(!is_dir($baseDirectory)) { + if (!is_dir($baseDirectory)) { mkdir($baseDirectory, 0755, true); } $vs_version_targets = explode(',', $vs_version_targets); - foreach($files as $file) { + foreach ($files as $file) { foreach ($vs_version_targets as $vs_version_target) { $destinationDirectory = $baseDirectory . '/' . $vs_version_target . '/' . $file['arch']; $destinationFileName = str_replace($file['artifact_name'], $library . '-' . $ref, $file['file_name']); @@ -88,7 +89,14 @@ private function copyFiles(array $files, $library, $ref, $vs_version_targets): v } } - private function updateSeriesFiles($files, $library, $ref, $php_versions, $vs_version_targets, $stability): void + private function updateSeriesFiles( + array $files, + string $library, + string $ref, + string $php_versions, + string $vs_version_targets, + string $stability + ): void { $php_versions = explode(',', $php_versions); $vs_version_targets = explode(',', $vs_version_targets); @@ -104,8 +112,8 @@ private function updateSeriesFiles($files, $library, $ref, $php_versions, $vs_ve $arch = $file['arch']; $seriesFile = $baseDirectory . "/packages-$php_version-$vs_version_target-$arch-$stability_value.txt"; $file_lines = file($seriesFile, FILE_IGNORE_NEW_LINES); - foreach($file_lines as $no => $line) { - if(str_starts_with($line, $library)) { + foreach ($file_lines as $no => $line) { + if (str_starts_with($line, $library)) { $file_lines[$no] = $fileName; } } diff --git a/src/Http/BaseController.php b/src/Http/BaseController.php index e0495b7..9bc8f35 100644 --- a/src/Http/BaseController.php +++ b/src/Http/BaseController.php @@ -4,13 +4,16 @@ abstract class BaseController implements ControllerInterface { - public function handle(): void { + public function handle(): void + { $data = json_decode(file_get_contents('php://input'), true); - if($this->validate($data)) { + if ($this->validate($data)) { $this->execute($data); } } + protected abstract function validate(array $data): bool; + protected abstract function execute(array $data): void; } \ No newline at end of file diff --git a/src/Http/Controllers/IndexController.php b/src/Http/Controllers/IndexController.php index 2a08444..c80478a 100644 --- a/src/Http/Controllers/IndexController.php +++ b/src/Http/Controllers/IndexController.php @@ -1,6 +1,7 @@ <?php namespace App\Http\Controllers; + use App\Http\BaseController; class IndexController extends BaseController diff --git a/src/Http/Controllers/PeclController.php b/src/Http/Controllers/PeclController.php index 511130f..25423d0 100644 --- a/src/Http/Controllers/PeclController.php +++ b/src/Http/Controllers/PeclController.php @@ -21,7 +21,7 @@ protected function validate(mixed $data): bool $valid = $validator->isValid(); - if(!$valid) { + if (!$valid) { http_response_code(400); echo 'Invalid request: ' . $validator; } @@ -49,7 +49,7 @@ private function fetchExtension(string $extension, string $ref, string $url, str FetchArtifact::handle($url, $filepath, $token); - if(!file_exists($filepath) || mime_content_type($filepath) !== 'application/zip') { + if (!file_exists($filepath) || mime_content_type($filepath) !== 'application/zip') { throw new Exception('Failed to fetch the extension'); } } diff --git a/src/Http/Controllers/PhpController.php b/src/Http/Controllers/PhpController.php index 0c120c0..3711a47 100644 --- a/src/Http/Controllers/PhpController.php +++ b/src/Http/Controllers/PhpController.php @@ -20,7 +20,7 @@ protected function validate(array $data): bool $valid = $validator->isValid(); - if(!$valid) { + if (!$valid) { http_response_code(400); echo 'Invalid request: ' . $validator; } @@ -50,7 +50,7 @@ private function fetchPhpBuild(string $url, string $token): void FetchArtifact::handle($url, $filepath, $token); - if(!file_exists($filepath) || mime_content_type($filepath) !== 'application/zip') { + if (!file_exists($filepath) || mime_content_type($filepath) !== 'application/zip') { throw new Exception('Failed to fetch the PHP build'); } } diff --git a/src/Http/Controllers/WinlibsController.php b/src/Http/Controllers/WinlibsController.php index 89d7a55..363e681 100644 --- a/src/Http/Controllers/WinlibsController.php +++ b/src/Http/Controllers/WinlibsController.php @@ -25,7 +25,7 @@ protected function validate(array $data): bool $valid = $validator->isValid(); - if(!$valid) { + if (!$valid) { http_response_code(400); echo 'Invalid request: ' . $validator; } @@ -37,7 +37,7 @@ protected function execute(array $data): void { extract($data); GetArtifacts::handle($workflow_run_id, $token); - $directory = getenv('BUILDS_DIRECTORY') . '/winlibs/'. $workflow_run_id; - file_put_contents( $directory . '/data.json', json_encode($data)); + $directory = getenv('BUILDS_DIRECTORY') . '/winlibs/' . $workflow_run_id; + file_put_contents($directory . '/data.json', json_encode($data)); } } \ No newline at end of file diff --git a/src/Validator.php b/src/Validator.php index 297b45d..1330f95 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -8,14 +8,13 @@ function __construct ( protected array $rules, protected array $errors = [], - protected bool $valid = false + protected bool $valid = false ) { // } - public function validate(array $data): void { $this->errors = [];