Make downloader configurable

This commit is contained in:
crazywhalecc 2026-02-11 16:54:46 +08:00 committed by Jerry Ma
parent 508f635f01
commit 7a3f10bd77
2 changed files with 35 additions and 21 deletions

30
config/downloader.php Normal file
View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
use StaticPHP\Artifact\Downloader\Type\BitBucketTag;
use StaticPHP\Artifact\Downloader\Type\DownloadTypeInterface;
use StaticPHP\Artifact\Downloader\Type\FileList;
use StaticPHP\Artifact\Downloader\Type\Git;
use StaticPHP\Artifact\Downloader\Type\GitHubRelease;
use StaticPHP\Artifact\Downloader\Type\GitHubTarball;
use StaticPHP\Artifact\Downloader\Type\HostedPackageBin;
use StaticPHP\Artifact\Downloader\Type\LocalDir;
use StaticPHP\Artifact\Downloader\Type\PhpRelease;
use StaticPHP\Artifact\Downloader\Type\PIE;
use StaticPHP\Artifact\Downloader\Type\Url;
/* @return array<string, DownloadTypeInterface> */
return [
'bitbuckettag' => BitBucketTag::class,
'filelist' => FileList::class,
'git' => Git::class,
'ghrel' => GitHubRelease::class,
'ghtar' => GitHubTarball::class,
'ghtagtar' => GitHubTarball::class,
'local' => LocalDir::class,
'pie' => PIE::class,
'url' => Url::class,
'php-release' => PhpRelease::class,
'hosted' => HostedPackageBin::class,
];

View File

@ -6,16 +6,9 @@ namespace StaticPHP\Artifact;
use Psr\Log\LogLevel; use Psr\Log\LogLevel;
use StaticPHP\Artifact\Downloader\DownloadResult; use StaticPHP\Artifact\Downloader\DownloadResult;
use StaticPHP\Artifact\Downloader\Type\BitBucketTag;
use StaticPHP\Artifact\Downloader\Type\DownloadTypeInterface; use StaticPHP\Artifact\Downloader\Type\DownloadTypeInterface;
use StaticPHP\Artifact\Downloader\Type\FileList;
use StaticPHP\Artifact\Downloader\Type\Git; use StaticPHP\Artifact\Downloader\Type\Git;
use StaticPHP\Artifact\Downloader\Type\GitHubRelease;
use StaticPHP\Artifact\Downloader\Type\GitHubTarball;
use StaticPHP\Artifact\Downloader\Type\HostedPackageBin;
use StaticPHP\Artifact\Downloader\Type\LocalDir; use StaticPHP\Artifact\Downloader\Type\LocalDir;
use StaticPHP\Artifact\Downloader\Type\PhpRelease;
use StaticPHP\Artifact\Downloader\Type\PIE;
use StaticPHP\Artifact\Downloader\Type\Url; use StaticPHP\Artifact\Downloader\Type\Url;
use StaticPHP\Artifact\Downloader\Type\ValidatorInterface; use StaticPHP\Artifact\Downloader\Type\ValidatorInterface;
use StaticPHP\DI\ApplicationContext; use StaticPHP\DI\ApplicationContext;
@ -38,19 +31,7 @@ use ZM\Logger\ConsoleColor;
class ArtifactDownloader class ArtifactDownloader
{ {
/** @var array<string, class-string<DownloadTypeInterface>> */ /** @var array<string, class-string<DownloadTypeInterface>> */
public const array DOWNLOADERS = [ protected array $downloaders = [];
'bitbuckettag' => BitBucketTag::class,
'filelist' => FileList::class,
'git' => Git::class,
'ghrel' => GitHubRelease::class,
'ghtar' => GitHubTarball::class,
'ghtagtar' => GitHubTarball::class,
'local' => LocalDir::class,
'pie' => PIE::class,
'url' => Url::class,
'php-release' => PhpRelease::class,
'hosted' => HostedPackageBin::class,
];
/** @var array<string, Artifact> Artifact objects */ /** @var array<string, Artifact> Artifact objects */
protected array $artifacts = []; protected array $artifacts = [];
@ -214,6 +195,9 @@ class ArtifactDownloader
// read downloads dir // read downloads dir
$this->_before_files = FileSystem::scanDirFiles(DOWNLOAD_PATH, false, true, true) ?: []; $this->_before_files = FileSystem::scanDirFiles(DOWNLOAD_PATH, false, true, true) ?: [];
// load downloaders
$this->downloaders = require ROOT_DIR . '/config/downloader.php';
} }
/** /**
@ -371,7 +355,7 @@ class ArtifactDownloader
foreach ($queue as $item) { foreach ($queue as $item) {
try { try {
$instance = null; $instance = null;
$call = self::DOWNLOADERS[$item['config']['type']] ?? null; $call = $this->downloaders[$item['config']['type']] ?? null;
$type_display_name = match (true) { $type_display_name = match (true) {
$item['lock'] === 'source' && ($callback = $artifact->getCustomSourceCallback()) !== null => 'user defined source downloader', $item['lock'] === 'source' && ($callback = $artifact->getCustomSourceCallback()) !== null => 'user defined source downloader',
$item['lock'] === 'binary' && ($callback = $artifact->getCustomBinaryCallback()) !== null => 'user defined binary downloader', $item['lock'] === 'binary' && ($callback = $artifact->getCustomBinaryCallback()) !== null => 'user defined binary downloader',