Skip to content

Commit

Permalink
rewrited self updater
Browse files Browse the repository at this point in the history
Took 29 minutes
  • Loading branch information
fabio-ivona committed May 19, 2021
1 parent c28b285 commit 484d625
Showing 1 changed file with 193 additions and 12 deletions.
205 changes: 193 additions & 12 deletions app/Updater/GithubStrategy.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,86 @@
<?php
<?php /** @noinspection PhpMissingParamTypeInspection */
/** @noinspection PhpMissingReturnTypeInspection */

/** @noinspection PhpMissingFieldTypeInspection */


namespace App\Updater;

use Humbug\SelfUpdate\Exception\JsonParsingException;
use Humbug\SelfUpdate\Strategy\StrategyInterface;
use Humbug\SelfUpdate\Updater;
use Humbug\SelfUpdate\VersionParser;
use Humbug\SelfUpdate\Exception\HttpRequestException;
use Humbug\SelfUpdate\Exception\InvalidArgumentException;
use Humbug\SelfUpdate\Exception\JsonParsingException;
use Illuminate\Support\Str;
use LaravelZero\Framework\Components\Updater\Strategy\StrategyInterface;

final class GithubStrategy extends \Humbug\SelfUpdate\Strategy\GithubStrategy implements StrategyInterface
class GithubStrategy implements StrategyInterface
{
const API_URL = 'https://packagist.org/packages/%s.json';

const STABLE = 'stable';

const UNSTABLE = 'unstable';

const ANY = 'any';

/**
* @var string
*/
private $localVersion;

/**
* @var string
*/
private $remoteVersion;

/**
* @var string
*/
private $remoteUrl;

/**
* @var string
*/
private $pharName;

/**
* @var string
*/
private $packageName;

/**
* @var string
*/
private $stability = self::STABLE;

/**
* Download the remote Phar file.
*
* @param Updater $updater
* @return void
*/
public function download(Updater $updater)
{
/** Switch remote request errors to HttpRequestExceptions */
set_error_handler(array($updater, 'throwHttpRequestException'));
$result = humbug_get_contents($this->remoteUrl);
restore_error_handler();
if (false === $result) {
throw new HttpRequestException(sprintf(
'Request to URL failed: %s', $this->remoteUrl
));
}

file_put_contents($updater->getTempPharFile(), $result);
}

/**
* Retrieve the current version available remotely.
*
* @param Updater $updater
* @return string|bool
*/
public function getCurrentRemoteVersion(Updater $updater)
{
/** Switch remote request errors to HttpRequestExceptions */
Expand All @@ -26,22 +96,133 @@ public function getCurrentRemoteVersion(Updater $updater)
);
}

$versions = array_keys($package['packages'][$this->getPackageName()]);
$versions = array_keys($package['package']['versions']);
$versionParser = new VersionParser($versions);
if ($this->getStability() === self::STABLE) {
echo 'remote version: ' . $versionParser->getMostRecentStable() . PHP_EOL. PHP_EOL;
$this->remoteVersion = $versionParser->getMostRecentStable();
} elseif ($this->getStability() === self::UNSTABLE) {
echo 'remote version: ' . $versionParser->getMostRecentUnstable() . PHP_EOL. PHP_EOL;
$this->remoteVersion = $versionParser->getMostRecentUnstable();
} else {
echo 'remote version: ' . $versionParser->getMostRecentAll() . PHP_EOL. PHP_EOL;
$this->remoteVersion = $versionParser->getMostRecentAll();
}

echo 'remote version: ' . $this->remoteVersion . PHP_EOL. PHP_EOL;

/**
* Setup remote URL if there's an actual version to download
*/
if (!empty($this->remoteVersion)) {
$this->remoteUrl = $this->getDownloadUrl($package);
}

return $this->remoteVersion;
}

/**
* Retrieve the current version of the local phar file.
*
* @param Updater $updater
* @return string
*/
public function getCurrentLocalVersion(Updater $updater)
{
return $this->localVersion;
}

/**
* Set version string of the local phar
*
* @param string $version
*/
public function setCurrentLocalVersion($version)
{
$this->localVersion = $version;
}

/**
* Set Package name
*
* @param string $name
*/
public function setPackageName($name)
{
$this->packageName = $name;
}

/**
* Get Package name
*
* @return string
*/
public function getPackageName()
{
return $this->packageName;
}

/**
* Set phar file's name
*
* @param string $name
*/
public function setPharName($name)
{
$this->pharName = $name;
}

/**
* Get phar file's name
*
* @return string
*/
public function getPharName()
{
return $this->pharName;
}

/**
* Set target stability
*
* @param string $stability
*/
public function setStability($stability)
{
if ($stability !== self::STABLE && $stability !== self::UNSTABLE && $stability !== self::ANY) {
throw new InvalidArgumentException(
'Invalid stability value. Must be one of "stable", "unstable" or "any".'
);
}
$this->stability = $stability;
}

/**
* Get target stability
*
* @return string
*/
public function getStability()
{
return $this->stability;
}

return parent::getCurrentRemoteVersion($updater);
protected function getApiUrl()
{
return sprintf(self::API_URL, $this->getPackageName());
}

protected function getDownloadUrl(array $package): string
protected function getDownloadUrl(array $package)
{
$url = parent::getDownloadUrl($package);
return Str::of($url)->append('dock');
$baseUrl = preg_replace(
'{\.git$}',
'',
$package['package']['versions'][$this->remoteVersion]['source']['url']
);
$downloadUrl = sprintf(
'%s/releases/download/%s/%s',
$baseUrl,
$this->remoteVersion,
$this->getPharName()
);

return Str::of($downloadUrl)->append('dock');
}
}

0 comments on commit 484d625

Please sign in to comment.