Skip to content

Commit 4b3bbb4

Browse files
committed
fix: don't check github for a new version every command
1 parent 1dcd25e commit 4b3bbb4

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/CliConfiguration.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ public function getActiveTeamId(): int
8585
return (int) $this->get('active_team');
8686
}
8787

88+
/**
89+
* Get the CLI version on GitHub.
90+
*/
91+
public function getGitHubCliVersion(): string
92+
{
93+
return (string) $this->get('github_cli_version');
94+
}
95+
96+
/**
97+
* Get the timestamp when GitHub was last checked for a CLI update.
98+
*/
99+
public function getGitHubLastCheckedTimestamp(): int
100+
{
101+
return (int) $this->get('github_last_checked');
102+
}
103+
88104
/**
89105
* Check if the global configuration has an access token.
90106
*/
@@ -109,6 +125,22 @@ public function setActiveTeamId(int $teamId)
109125
$this->set('active_team', $teamId);
110126
}
111127

128+
/**
129+
* Set the CLI version on GitHub.
130+
*/
131+
public function setGitHubCliVersion(string $version)
132+
{
133+
$this->set('github_cli_version', $version);
134+
}
135+
136+
/**
137+
* Set the timestamp when GitHub was last checked for a CLI update.
138+
*/
139+
public function setGitHubLastCheckedTimestamp(int $timestamp)
140+
{
141+
$this->set('github_last_checked', $timestamp);
142+
}
143+
112144
/**
113145
* Get the given configuration option or return the default.
114146
*/

src/EventListener/VersionCheckSubscriber.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@
1717
use Symfony\Component\Console\Event\ConsoleCommandEvent;
1818
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1919
use Ymir\Cli\Application;
20+
use Ymir\Cli\CliConfiguration;
2021
use Ymir\Cli\GitHubClient;
2122

2223
class VersionCheckSubscriber implements EventSubscriberInterface
2324
{
25+
/**
26+
* The global Ymir CLI configuration.
27+
*
28+
* @var CliConfiguration
29+
*/
30+
private $cliConfiguration;
31+
2432
/**
2533
* The API client that interacts with the GitHub API.
2634
*
@@ -38,8 +46,9 @@ class VersionCheckSubscriber implements EventSubscriberInterface
3846
/**
3947
* Constructor.
4048
*/
41-
public function __construct(GitHubClient $gitHubClient, string $version)
49+
public function __construct(CliConfiguration $cliConfiguration, GitHubClient $gitHubClient, string $version)
4250
{
51+
$this->cliConfiguration = $cliConfiguration;
4352
$this->gitHubClient = $gitHubClient;
4453
$this->version = $version;
4554
}
@@ -59,9 +68,14 @@ public static function getSubscribedEvents()
5968
*/
6069
public function onConsoleCommand(ConsoleCommandEvent $event)
6170
{
62-
$latestVersion = ltrim($this->gitHubClient->getTags('ymirapp/cli')->pluck('name')->first(), 'v');
71+
$time = time();
72+
73+
if ($time - $this->cliConfiguration->getGitHubLastCheckedTimestamp() > 3600) {
74+
$this->cliConfiguration->setGitHubCliVersion(ltrim($this->gitHubClient->getTags('ymirapp/cli')->pluck('name')->first(), 'v'));
75+
$this->cliConfiguration->setGitHubLastCheckedTimestamp($time);
76+
}
6377

64-
if (version_compare($latestVersion, $this->version, '>')) {
78+
if (version_compare($this->cliConfiguration->getGitHubCliVersion(), $this->version, '>')) {
6579
$event->getOutput()->writeln('<comment>A new version of the Ymir CLI is available</comment>');
6680
}
6781
}

0 commit comments

Comments
 (0)