diff --git a/cli/Valet/Site.php b/cli/Valet/Site.php index 56e5121..79dd564 100644 --- a/cli/Valet/Site.php +++ b/cli/Valet/Site.php @@ -3,6 +3,7 @@ namespace Valet; use DateTime; +use DateInterval; use DomainException; use Illuminate\Support\Collection; use PhpFpm; @@ -501,6 +502,29 @@ public function secure(string $url, string $siteConf = null, int $certificateExp $this->files->putAsUser($this->nginxPath($url), $siteConf); } + /** + * Renews expired or expiring (within 60 days) domains with a trusted TLS certificate. + */ + public function renew($expireIn = 368, $days = 60): void + { + $now = (new DateTime())->add(new DateInterval('P' . $days . 'D')); + // Update anything expiring in the next 60 days + $sites = collect(Site::securedWithDates()) + ->filter(fn ($row) => $row['exp'] < $now) + ->values(); + if ($sites->isEmpty()) { + info('No sites need renewing.'); + exit; + } + $sites->each(function ($row) use ($expireIn) { + $url = Site::domain($row['site']); + + $this->secure($url, null, $expireIn); + + info('The [' . $url . '] site has been secured with a fresh TLS certificate.'); + }); + } + /** * If CA and root certificates are nonexistent, create them and trust the root cert. * diff --git a/cli/app.php b/cli/app.php index 8b53e03..ba3b360 100644 --- a/cli/app.php +++ b/cli/app.php @@ -300,23 +300,7 @@ function (ConsoleCommandEvent $event) { * Renews expired or expiring (within 60 days) domains with a trusted TLS certificate. */ $app->command('renew [--expireIn=] [--days=]', function (OutputInterface $output, $expireIn = 368, $days = 60) { - $now = (new DateTime())->add(new DateInterval('P' . $days . 'D')); - // Update anything expiring in the next 60 days - $sites = collect(Site::securedWithDates()) - ->filter(fn ($row) => $row['exp'] < $now) - ->values(); - if ($sites->isEmpty()) { - info('No sites need renewing.'); - exit; - } - $sites->each(function ($row) use ($expireIn) { - $url = Site::domain($row['site']); - - Site::secure($url, null, $expireIn); - - info('The [' . $url . '] site has been secured with a fresh TLS certificate.'); - }); - + Site::renew($expireIn, $days); Nginx::restart(); })->descriptions('Renews expired or expiring (within 60 days) domains with a trusted TLS certificate.', [ '--expireIn' => 'The amount of days the self signed certificate is valid for. Default is set to "368"',