Initial commit

This commit is contained in:
2025-12-28 15:26:33 +01:00
parent 078f2bf6a7
commit 13b61ce3a8
18 changed files with 652 additions and 19 deletions

View File

@@ -0,0 +1,113 @@
<?php
namespace NoccyLabs\Composer\PackagePlugin\Registry\Gitea;
use NoccyLabs\Composer\PackagePlugin\Project\ProjectInfo;
use NoccyLabs\Composer\PackagePlugin\Registry\RegistryInterface;
use NoccyLabs\Composer\PackagePlugin\Registry\RegistryTrait;
class GiteaRegistry implements RegistryInterface
{
use RegistryTrait;
public function publishPackageVersion(ProjectInfo $project): void
{
$url = sprintf("https://%s/api/packages/%s/composer?version=%s", $this->server, $this->owner, $project->version);
$request = [
'method' => 'PUT',
'url' => $url,
'auth' => $this->token,
'filename' => $project->filename,
];
$this->invoke($request);
}
public function unpublishPackageVersion(ProjectInfo $project): void
{
$url = sprintf("https://%s/api/packages/%s/composer?version=%s", $this->server, $this->owner, $project->version);
$request = [
'method' => 'PUT',
'url' => $url,
'auth' => $this->token,
'filename' => $project->filename,
];
$this->invoke($request);
}
public function unpublishPackage(ProjectInfo $project): void
{
}
private function invoke(array $request): mixed
{
if (extension_loaded("curl")) {
return $this->invokeExt($request);
}
return $this->invokeCurl($request);
}
/**
* Invoke using the curl php extension
*
* @return void
*/
private function invokeExt(array $request): mixed
{
$fd = fopen($request['filename'], "rb");
$fdlen = filesize($request['filename']);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $request['url']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if ($request['method'] === 'PUT') {
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $this->token);
curl_setopt($curl, CURLOPT_INFILE, $fd);
curl_setopt($curl, CURLOPT_INFILESIZE, $fdlen);
}
$response = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
fclose($fd);
switch ($code) {
case 201:
return true;
case 400:
throw new \Exception("Bad request");
case 409:
throw new \Exception("Package with this version already exists");
default:
if ($code < 200 && $code >= 300)
throw new \Exception("Request failed: {$code}");
return true;
}
}
/**
* Invoke using the curl cli utility
*
* @return void
*/
private function invokeCurl(array $request): mixed
{
$cmd = trim(exec("which curl"));
$args = [ "--user", $this->token ];
if ($request['method'] === 'PUT') {
$args[] = '--upload-file';
$args[] = $request['filename'];
}
$args[] = $request['url'];
$cmdl = escapeshellcmd($cmd)." ".join(" ", array_map(escapeshellarg(...), $args));
passthru($cmdl);
return true;
}
}