Missing dependancies
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Console\SelfUpdate;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class GithubClient implements GithubClientInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTags(): array
|
||||
{
|
||||
$url = 'https://api.github.com/repos/PHP-CS-Fixer/PHP-CS-Fixer/tags';
|
||||
|
||||
$result = @file_get_contents(
|
||||
$url,
|
||||
false,
|
||||
stream_context_create([
|
||||
'http' => [
|
||||
'header' => 'User-Agent: PHP-CS-Fixer/PHP-CS-Fixer',
|
||||
],
|
||||
])
|
||||
);
|
||||
|
||||
if (false === $result) {
|
||||
throw new \RuntimeException(sprintf('Failed to load tags at "%s".', $url));
|
||||
}
|
||||
|
||||
$result = json_decode($result, true);
|
||||
if (JSON_ERROR_NONE !== json_last_error()) {
|
||||
throw new \RuntimeException(sprintf(
|
||||
'Failed to read response from "%s" as JSON: %s.',
|
||||
$url,
|
||||
json_last_error_msg()
|
||||
));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Console\SelfUpdate;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
interface GithubClientInterface
|
||||
{
|
||||
/**
|
||||
* @return list<array{
|
||||
* name: string,
|
||||
* zipball_url: string,
|
||||
* tarball_url: string,
|
||||
* commit: array{sha: string, url: string},
|
||||
* }>
|
||||
*/
|
||||
public function getTags(): array;
|
||||
}
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Console\SelfUpdate;
|
||||
|
||||
use Composer\Semver\Comparator;
|
||||
use Composer\Semver\Semver;
|
||||
use Composer\Semver\VersionParser;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class NewVersionChecker implements NewVersionCheckerInterface
|
||||
{
|
||||
private GithubClientInterface $githubClient;
|
||||
|
||||
private VersionParser $versionParser;
|
||||
|
||||
/**
|
||||
* @var null|string[]
|
||||
*/
|
||||
private $availableVersions;
|
||||
|
||||
public function __construct(GithubClientInterface $githubClient)
|
||||
{
|
||||
$this->githubClient = $githubClient;
|
||||
$this->versionParser = new VersionParser();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getLatestVersion(): string
|
||||
{
|
||||
$this->retrieveAvailableVersions();
|
||||
|
||||
return $this->availableVersions[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getLatestVersionOfMajor(int $majorVersion): ?string
|
||||
{
|
||||
$this->retrieveAvailableVersions();
|
||||
|
||||
$semverConstraint = '^'.$majorVersion;
|
||||
|
||||
foreach ($this->availableVersions as $availableVersion) {
|
||||
if (Semver::satisfies($availableVersion, $semverConstraint)) {
|
||||
return $availableVersion;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function compareVersions(string $versionA, string $versionB): int
|
||||
{
|
||||
$versionA = $this->versionParser->normalize($versionA);
|
||||
$versionB = $this->versionParser->normalize($versionB);
|
||||
|
||||
if (Comparator::lessThan($versionA, $versionB)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (Comparator::greaterThan($versionA, $versionB)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function retrieveAvailableVersions(): void
|
||||
{
|
||||
if (null !== $this->availableVersions) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->githubClient->getTags() as $tag) {
|
||||
$version = $tag['name'];
|
||||
|
||||
try {
|
||||
$this->versionParser->normalize($version);
|
||||
|
||||
if ('stable' === VersionParser::parseStability($version)) {
|
||||
$this->availableVersions[] = $version;
|
||||
}
|
||||
} catch (\UnexpectedValueException $exception) {
|
||||
// not a valid version tag
|
||||
}
|
||||
}
|
||||
|
||||
$this->availableVersions = Semver::rsort($this->availableVersions);
|
||||
}
|
||||
}
|
||||
Vendored
+37
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Console\SelfUpdate;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
interface NewVersionCheckerInterface
|
||||
{
|
||||
/**
|
||||
* Returns the tag of the latest version.
|
||||
*/
|
||||
public function getLatestVersion(): string;
|
||||
|
||||
/**
|
||||
* Returns the tag of the latest minor/patch version of the given major version.
|
||||
*/
|
||||
public function getLatestVersionOfMajor(int $majorVersion): ?string;
|
||||
|
||||
/**
|
||||
* Returns -1, 0, or 1 if the first version is respectively less than,
|
||||
* equal to, or greater than the second.
|
||||
*/
|
||||
public function compareVersions(string $versionA, string $versionB): int;
|
||||
}
|
||||
Reference in New Issue
Block a user