diff --git a/app/Updater/GithubStrategy.php b/app/Updater/GithubStrategy.php index 6d8a7b8..2f7be84 100644 --- a/app/Updater/GithubStrategy.php +++ b/app/Updater/GithubStrategy.php @@ -1,16 +1,86 @@ -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 */ @@ -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'); } }