From c387994e8fe233ca2cda830e490d13261c1947e4 Mon Sep 17 00:00:00 2001 From: Daniel Haven <49914607+danielh-official@users.noreply.github.com> Date: Tue, 4 Nov 2025 16:10:47 -0500 Subject: [PATCH] Add Laravel IDE Helper option - Option is false by default - If accepted, adds barryvdh/laravel-ide-helper to the dev dependencies - Adds the following to the root .gitignore ``` _ide_helper.php _ide_helper_models.php .phpstorm.meta.php ``` - Generates the gitignored files using the scripts provided laravel-ide-helper --- src/NewCommand.php | 40 +++++++++++++++++++++++++++++++++++++++- tests/NewCommandTest.php | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/NewCommand.php b/src/NewCommand.php index aa5ff393..950fcb4d 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -70,7 +70,8 @@ protected function configure() ->addOption('yarn', null, InputOption::VALUE_NONE, 'Install and build NPM dependencies via Yarn') ->addOption('boost', null, InputOption::VALUE_NONE, 'Install Laravel Boost to improve AI assisted coding') ->addOption('using', null, InputOption::VALUE_OPTIONAL, 'Install a custom starter kit from a community maintained package') - ->addOption('force', 'f', InputOption::VALUE_NONE, 'Forces install even if the directory already exists'); + ->addOption('force', 'f', InputOption::VALUE_NONE, 'Forces install even if the directory already exists') + ->addOption('ide-helper', null, InputOption::VALUE_NONE, 'Install Laravel ide-helper'); } /** @@ -181,6 +182,13 @@ protected function interact(InputInterface $input, OutputInterface $output) label: 'Do you want to install Laravel Boost to improve AI assisted coding?', )); } + + if (! $input->getOption('ide-helper')) { + $input->setOption('ide-helper', confirm( + label: 'Do you want to install Laravel IDE Helper?', + default: false, + )); + } } /** @@ -543,6 +551,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->commitChanges('Configure Boost post-update script', $directory, $input, $output); } + if ($input->getOption('ide-helper')) { + $this->installIdeHelper($directory, $input, $output); + } + $output->writeln(" INFO Application ready in [{$name}]. You can start your local development using:".PHP_EOL); $output->writeln('cd '.$name.''); @@ -565,6 +577,32 @@ protected function execute(InputInterface $input, OutputInterface $output): int return $process->getExitCode(); } + protected function installIdeHelper(string $directory, InputInterface $input, OutputInterface $output): void + { + $composerBinary = $this->findComposer(); + + $gitignoreContents = file_get_contents($directory.'/.gitignore'); + + file_put_contents( + $directory.'/.gitignore', + $gitignoreContents. + PHP_EOL. + '# Laravel IDE Helper' + .PHP_EOL.'_ide_helper.php' + .PHP_EOL.'_ide_helper_models.php' + .PHP_EOL.'.phpstorm.meta.php' + ); + + $commands = [ + $composerBinary.' require --dev barryvdh/laravel-ide-helper', + $this->phpBinary().' artisan ide-helper:generate', + $this->phpBinary().' artisan ide-helper:models --nowrite', + $this->phpBinary().' artisan ide-helper:meta', + ]; + + $this->runCommands($commands, $input, $output, workingPath: $directory); + } + /** * Determine the Node package manager to use. * diff --git a/tests/NewCommandTest.php b/tests/NewCommandTest.php index 614ac80a..07b298ca 100644 --- a/tests/NewCommandTest.php +++ b/tests/NewCommandTest.php @@ -85,4 +85,37 @@ public function test_on_at_least_laravel_11() $this->assertTrue($onLaravel11); $this->assertTrue($onLaravel12); } + + public function test_it_can_add_laravel_ide_helper() + { + $scaffoldDirectoryName = 'tests-output/ide-helper-app'; + $scaffoldDirectory = __DIR__.'/../'.$scaffoldDirectoryName; + + if (file_exists($scaffoldDirectory)) { + if (PHP_OS_FAMILY == 'Windows') { + exec("rd /s /q \"$scaffoldDirectory\""); + } else { + exec("rm -rf \"$scaffoldDirectory\""); + } + } + + $app = new Application('Laravel Installer'); + $app->add(new NewCommand); + + $tester = new CommandTester($app->find('new')); + + $statusCode = $tester->execute( + ['name' => $scaffoldDirectoryName, '--ide-helper' => true], + ['interactive' => false] + ); + + $this->assertSame(0, $statusCode); + $this->assertDirectoryExists($scaffoldDirectory.'/vendor/barryvdh/laravel-ide-helper'); + + $gitignoreContents = file_get_contents($scaffoldDirectory.'/.gitignore'); + $this->assertStringContainsString('# Laravel IDE Helper', $gitignoreContents); + $this->assertStringContainsString('_ide_helper.php', $gitignoreContents); + $this->assertStringContainsString('_ide_helper_models.php', $gitignoreContents); + $this->assertStringContainsString('phpstorm.meta.php', $gitignoreContents); + } }