diff --git a/src/NewCommand.php b/src/NewCommand.php index a203c87..56a713b 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -82,6 +82,35 @@ protected function interact(InputInterface $input, OutputInterface $output) | |___| (_| | | | (_| |\ V / __/ | |______\__,_|_| \__,_| \_/ \___|_|'.PHP_EOL.PHP_EOL); + if ($this->existsNewerInstallerVersion()) { + $output->writeln(PHP_EOL.' NEW VERSION A newer version of Laravel Installer is available.'.PHP_EOL); + + $shouldUpdate = select( + label: 'Do you want to update your Laravel Installer before continuing?', + options: [ + 'yes' => 'Yes, please update the Laravel Installer', + 'no' => 'No, continue with the current version', + ], + default: 'yes', + ); + + if ($shouldUpdate === 'yes') { + $this->composer = new Composer(new Filesystem()); + + $output->writeln(' NEW VERSION Updating Laravel Installer...'.PHP_EOL); + + $this->runCommands([ + $this->findComposer().' global update laravel/installer', + ], $input, $output); + + $output->writeln(PHP_EOL.' NEW VERSION The Laravel Installer was sucessfully updated.'.PHP_EOL); + + $output->writeln(' ➜ Please run laravel new again.'.PHP_EOL); + + exit(0); + } + } + if (! $input->getArgument('name')) { $input->setArgument('name', text( label: 'What is the name of your project?', @@ -894,4 +923,54 @@ protected function pregReplaceInFile(string $pattern, string $replace, string $f preg_replace($pattern, $replace, file_get_contents($file)) ); } + + /** + * Check if there is a newer version of Laravel Installer. + */ + protected function existsNewerInstallerVersion(): bool + { + $appVersion = $this->getApplication()->getVersion(); + $latestVersion = $this->getLatestVersion(); + + return version_compare($appVersion, $latestVersion, '<'); + } + + /** + * Get latest version without leading "v". + */ + protected function getLatestVersion(): string + { + $latestRelease = $this->getLatestRelease(); + + $tag = $latestRelease['tag_name']; + + if (str_starts_with($tag, 'v')) { + $tag = substr($tag, 1); + } + + return $tag; + } + + /** + * Get latest release information. + */ + protected function getLatestRelease(): array + { + // Required for GitHub API, otherwise the request will be 403 Forbidden + $context = stream_context_create([ + 'http' => [ + 'header' => [ + 'User-Agent: Laravel Installer', + ], + ], + ]); + + $json = file_get_contents( + 'https://api.github.com/repos/laravel/installer/releases/latest', + false, + $context + ); + + return json_decode($json, true); + } }