Skip to content

Commit

Permalink
Update checkForUpdates function
Browse files Browse the repository at this point in the history
  • Loading branch information
toby7002 committed Sep 27, 2023
1 parent d1a9e96 commit 5e7b870
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 5 deletions.
43 changes: 38 additions & 5 deletions src/thebigcrafter/Hydrogen/Hydrogen.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,63 @@

use pocketmine\plugin\Plugin;
use pocketmine\Server;
use pocketmine\utils\InternetException;
use thebigcrafter\Hydrogen\future\Future;
use thebigcrafter\Hydrogen\future\FutureState;
use thebigcrafter\Hydrogen\tasks\CheckUpdatesTask;
use thebigcrafter\Hydrogen\utils\Internet;

class Hydrogen
{

/**
* Notify if an update is available on Poggit.
*/
public static function checkForUpdates(Plugin $plugin) : void
public static function checkForUpdates(Plugin $plugin): void
{
Server::getInstance()->getAsyncPool()->submitTask(new CheckUpdatesTask($plugin->getName(), $plugin->getDescription()->getVersion()));

$logger = Server::getInstance()->getLogger();
$highestVersion = $plugin->getDescription()->getVersion();
$artifactUrl = "";

try {
$res = Internet::fetch("https://poggit.pmmp.io/releases.min.json?name=" . $plugin->getName())->await();
} catch (InternetException $e) {
Server::getInstance()->getLogger()->debug($e);

Check failure on line 38 in src/thebigcrafter/Hydrogen/Hydrogen.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)

Parameter #1 $message of method Logger::debug() expects string, pocketmine\utils\InternetException given.

Check failure on line 38 in src/thebigcrafter/Hydrogen/Hydrogen.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)

Parameter #1 $message of method Logger::debug() expects string, pocketmine\utils\InternetException given.
}

$releases = (array) json_decode($res, true);

Check failure on line 41 in src/thebigcrafter/Hydrogen/Hydrogen.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)

Variable $res might not be defined.

Check failure on line 41 in src/thebigcrafter/Hydrogen/Hydrogen.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)

Variable $res might not be defined.

if ($releases !== null) {
/**
* @var array{'version': string, 'artifact_url': string} $release
*/
foreach ($releases as $release) {
if (version_compare($highestVersion, $release["version"], ">=")) {
continue;
}

$highestVersion = $release["version"];
$artifactUrl = $release["artifact_url"];
}
}

if ($highestVersion !== $plugin->getDescription()->getVersion()) {
$artifactUrl .= "/{$plugin->getDescription()->getName()}_{$highestVersion}.phar";
$logger->notice("{$plugin->getDescription()->getName()} v{$highestVersion} is available for download at {$artifactUrl}");
}

}

/**
* Creates a new fiber asynchronously using the given closure, returning a Future that is completed with the
* eventual return value of the passed function or will fail if the closure throws an exception.
*/
public static function async(\Closure $closure, mixed ...$args) : Future
public static function async(\Closure $closure, mixed ...$args): Future
{
static $run = null;

$run ??= static function (FutureState $state, \Closure $closure, array $args) : void {
$run ??= static function (FutureState $state, \Closure $closure, array $args): void {
$s = $state;
$c = $closure;

Expand All @@ -56,4 +89,4 @@ public static function async(\Closure $closure, mixed ...$args) : Future
return new Future($state);
}

}
}
59 changes: 59 additions & 0 deletions src/thebigcrafter/Hydrogen/future/DeferredFuture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace thebigcrafter\Hydrogen\future;
use thebigcrafter\Hydrogen\trait\ForbidCloning;
use thebigcrafter\Hydrogen\trait\ForbidSerialization;

class DeferredFuture
{
use ForbidCloning;
use ForbidSerialization;

private readonly FutureState $state;

private readonly Future $future;

public function __construct()
{
$this->state = new FutureState();
$this->future = new Future($this->state);
}

/**
* Completes the operation with a result value.
*
* @param T $value Result of the operation.
*/
public function complete(mixed $value = null): void

Check failure on line 29 in src/thebigcrafter/Hydrogen/future/DeferredFuture.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)

Parameter $value of method thebigcrafter\Hydrogen\future\DeferredFuture::complete() has invalid type thebigcrafter\Hydrogen\future\T.

Check failure on line 29 in src/thebigcrafter/Hydrogen/future/DeferredFuture.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)

Parameter $value of method thebigcrafter\Hydrogen\future\DeferredFuture::complete() has invalid type thebigcrafter\Hydrogen\future\T.
{
$this->state->complete($value);
}

/**
* Marks the operation as failed.
*
* @param \Throwable $throwable Throwable to indicate the error.
*/
public function error(\Throwable $throwable): void
{
$this->state->error($throwable);
}

/**
* @return bool True if the operation has completed.
*/
public function isComplete(): bool
{
return $this->state->isComplete();
}

/**
* @return Future<T> The future associated with this Deferred.
*/
public function getFuture(): Future

Check failure on line 55 in src/thebigcrafter/Hydrogen/future/DeferredFuture.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)

Method thebigcrafter\Hydrogen\future\DeferredFuture::getFuture() has invalid return type thebigcrafter\Hydrogen\future\T.

Check failure on line 55 in src/thebigcrafter/Hydrogen/future/DeferredFuture.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)

PHPDoc tag @return contains generic type thebigcrafter\Hydrogen\future\Future<thebigcrafter\Hydrogen\future\T> but class thebigcrafter\Hydrogen\future\Future is not generic.

Check failure on line 55 in src/thebigcrafter/Hydrogen/future/DeferredFuture.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)

Method thebigcrafter\Hydrogen\future\DeferredFuture::getFuture() has invalid return type thebigcrafter\Hydrogen\future\T.

Check failure on line 55 in src/thebigcrafter/Hydrogen/future/DeferredFuture.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)

PHPDoc tag @return contains generic type thebigcrafter\Hydrogen\future\Future<thebigcrafter\Hydrogen\future\T> but class thebigcrafter\Hydrogen\future\Future is not generic.
{
return $this->future;
}
}
29 changes: 29 additions & 0 deletions src/thebigcrafter/Hydrogen/utils/Internet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace thebigcrafter\Hydrogen\utils;

use pocketmine\utils\InternetException;
use thebigcrafter\Hydrogen\EventLoop;
use thebigcrafter\Hydrogen\future\DeferredFuture;

class Internet
{
public static function fetch(string $url)
{
$deferred = new DeferredFuture();

EventLoop::defer(function () use($deferred, $url) {
$res = \pocketmine\utils\Internet::getURL($url);

if ($res instanceof InternetException) {
throw $res;
}

$deferred->complete($res->getBody());
});

return $deferred->getFuture();
}
}

0 comments on commit 5e7b870

Please sign in to comment.