-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
toby7002
committed
Oct 1, 2023
1 parent
bfc31a7
commit 23c7a42
Showing
8 changed files
with
267 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace thebigcrafter\omp\cache; | ||
|
||
class PluginCache | ||
{ | ||
/** | ||
* @param string $name | ||
* @param PluginVersion[] $versions | ||
* @param string $tagline | ||
* @param int $downloads | ||
* @param int $score | ||
* @param string $license | ||
*/ | ||
public function __construct(private string $name, private array $versions, private string $tagline, private int $downloads, private int $score, private string $license) | ||
{ | ||
} | ||
|
||
public function getName(): string { | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* Return a string list (["1.0.0", "1.0.1"]) | ||
* @return string[] | ||
*/ | ||
public function getVersions(): array { | ||
$versions = []; | ||
|
||
foreach($this->versions as $version) { | ||
$versions[] = $version->getVersion(); | ||
} | ||
|
||
return $versions; | ||
} | ||
|
||
public function getShortDescription(): string { | ||
return $this->tagline; | ||
} | ||
|
||
public function getDownloads(): int { | ||
return $this->downloads; | ||
} | ||
|
||
public function getScore(): int { | ||
return $this->score; | ||
} | ||
|
||
public function getLicense(): string { | ||
return $this->license; | ||
} | ||
|
||
/** | ||
* @param string $version Use "latest" to get the latest version | ||
* @return PluginVersion|null | ||
*/ | ||
public function getVersion(string $version) : ?PluginVersion { | ||
if($version === "latest") { | ||
$version = $this->getLatestVersion(); | ||
} | ||
|
||
if(isset($this->versions[$version])) { | ||
return $this->versions[$version]; | ||
} | ||
return null; | ||
} | ||
|
||
public function getLatestVersion() : string { | ||
$versions = $this->versions; | ||
|
||
sort($versions); | ||
|
||
return $versions[count($versions) - 1]->getVersion(); | ||
} | ||
|
||
public function getHomepageByVersion(string $version) : string { | ||
return "https://poggit.pmmp.io/p/" . $this->getName() . "/" . $version; | ||
} | ||
|
||
public function pushVersion(PluginVersion $version) : void { | ||
$this->versions[$version->getVersion()] = $version; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace thebigcrafter\omp\cache; | ||
|
||
use thebigcrafter\Iodine\Future; | ||
use thebigcrafter\omp\utils\Internet; | ||
|
||
class PluginVersion | ||
{ | ||
public function __construct(private string $version, private string $artifact_url,private string $description_url, private array $api, private array $deps, private string $changelog_url) | ||
{ | ||
} | ||
|
||
public function getVersion(): string | ||
{ | ||
return $this->version; | ||
} | ||
|
||
public function getArtifactUrl(): string | ||
{ | ||
return $this->artifact_url; | ||
} | ||
|
||
public function getApi(): array | ||
{ | ||
return $this->api; | ||
} | ||
|
||
public function getDepends(): array | ||
{ | ||
return $this->deps; | ||
} | ||
|
||
/** | ||
* @return Future<mixed> | ||
*/ | ||
public function getSize(): Future { | ||
return Internet::getRemoteFileSize($this->getArtifactUrl()); | ||
} | ||
|
||
public function getDescriptionUrl(): string { | ||
return $this->description_url; | ||
} | ||
|
||
public function getChangelogUrl(): string { | ||
return $this->changelog_url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace thebigcrafter\omp\cache; | ||
|
||
use Generator; | ||
use SplObjectStorage; | ||
|
||
final class PluginsPool { | ||
/** @var ?SplObjectStorage<PluginCache, mixed> $storage */ | ||
private static ?SplObjectStorage $storage = null; | ||
|
||
/** | ||
* @return SplObjectStorage<PluginCache, mixed> | ||
*/ | ||
public static function getStorage() : SplObjectStorage{ | ||
if(self::$storage === null){ | ||
self::$storage = new SplObjectStorage(); /** @phpstan-ignore-line */ | ||
} | ||
|
||
return self::$storage; /** @phpstan-ignore-line */ | ||
} | ||
|
||
/** | ||
* @param SplObjectStorage<PluginCache, mixed> $storage | ||
* @return void | ||
*/ | ||
public static function setStorage(SplObjectStorage $storage) : void{ | ||
self::$storage = $storage; | ||
} | ||
|
||
public static function add(PluginCache $pluginCache) : void { | ||
self::getStorage()->attach($pluginCache); | ||
} | ||
|
||
/** | ||
* @param PluginCache[] $array | ||
*/ | ||
public static function addMultiple(array $array) : void { | ||
foreach ($array as $plugin) { | ||
if ($plugin instanceof PluginCache) { | ||
self::getStorage()->attach($plugin); | ||
} | ||
} | ||
} | ||
|
||
public static function getNamePlugins() : Generator { | ||
foreach (self::$storage as $pluginCache) { | ||
yield $pluginCache->getName(); | ||
} | ||
} | ||
|
||
public static function getPluginCacheByName(string $name) : ?PluginCache { | ||
/** @var SplObjectStorage<PluginCache, mixed> $pluginCache */ | ||
foreach(self::getStorage() as $pluginCache) { | ||
if($pluginCache->getName() == $name) { | ||
return $pluginCache; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace thebigcrafter\omp\tasks; | ||
|
||
use thebigcrafter\Iodine\Future; | ||
use thebigcrafter\Iodine\Iodine; | ||
use thebigcrafter\omp\cache\PluginCache; | ||
use thebigcrafter\omp\cache\PluginsPool; | ||
use thebigcrafter\omp\cache\PluginVersion; | ||
use thebigcrafter\omp\OhMyPMMP; | ||
use thebigcrafter\omp\utils\Internet; | ||
use thebigcrafter\omp\Vars; | ||
|
||
class CachePlugins | ||
{ | ||
private static bool $hasCached = false; | ||
|
||
public static function hasCached() : bool { | ||
return self::$hasCached; | ||
} | ||
|
||
public static function setHasCached(bool $status) : void { | ||
self::$hasCached = $status; | ||
} | ||
|
||
public static function run() : Future { | ||
return Iodine::async(function() { | ||
$res = Internet::fetch(Vars::POGGIT_REPO_URL)->await(); | ||
$plugins = json_decode($res, true); | ||
|
||
sort($plugins); | ||
$pluginCaches = []; | ||
|
||
foreach ($plugins as $plugin) { | ||
$name = $plugin["name"]; | ||
$license = $plugin["license"] ?? "None"; | ||
$downloads = (int) $plugin["downloads"]; | ||
$artifactUrl = $plugin["artifact_url"]; | ||
/** @var array<array{from: string, to: string}> $api */ | ||
$api = (array) $plugin["api"]; | ||
/** @var array{from: string, to: string} $apiShift */ | ||
$apiShift = array_merge(...$api); | ||
/** @var array<array{name: string, version: string, depRelId: int, isHard: bool}> $deps */ | ||
$deps = (array) $plugin["deps"]; | ||
$score = (int) $plugin["score"]; | ||
|
||
if (!isset($pluginCaches[$name])) { | ||
$pluginCaches[$name] = new PluginCache($name, [], $plugin["tagline"], $downloads, $score, $license); | ||
} | ||
$description_url = $plugin["description_url"]; | ||
$changelog_url = $plugin["changelog_url"]; | ||
$version = new PluginVersion($plugin["version"], $artifactUrl, $description_url, $apiShift, $deps, (string) $changelog_url); | ||
$pluginCache = $pluginCaches[$name]; | ||
$pluginCache->pushVersion($version); | ||
} | ||
|
||
PluginsPool::addMultiple($pluginCaches); | ||
self::setHasCached(true); | ||
OhMyPMMP::getInstance()->getLogger()->info(OhMyPMMP::getLanguage()->translate("text.cache.successfully", ["count" => count($pluginCaches)])); | ||
}); | ||
} | ||
} |