diff --git a/config/ide-helper.php b/config/ide-helper.php index 1500783aa..ea12c5169 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -24,6 +24,17 @@ 'models_filename' => '_ide_helper_models.php', + /* + |-------------------------------------------------------------------------- + | Requests filename + |-------------------------------------------------------------------------- + | + | The default filename for the requests helper file. + | + */ + + 'requests_filename' => '_ide_helper_requests.php', + /* |-------------------------------------------------------------------------- | PhpStorm meta filename diff --git a/src/Console/EloquentCommand.php b/src/Console/EloquentCommand.php index cf2303f56..101b38259 100644 --- a/src/Console/EloquentCommand.php +++ b/src/Console/EloquentCommand.php @@ -32,7 +32,7 @@ class EloquentCommand extends Command /** * @var Filesystem $files */ - protected $files; + protected Filesystem $files; /** * The console command description. @@ -54,8 +54,9 @@ public function __construct(Filesystem $files) * Execute the console command. * * @return void + * @throws \ReflectionException|\Illuminate\Contracts\Filesystem\FileNotFoundException */ - public function handle() + public function handle(): void { Eloquent::writeEloquentModelHelper($this, $this->files); } diff --git a/src/Console/RequestCommand.php b/src/Console/RequestCommand.php new file mode 100644 index 000000000..14bd3e330 --- /dev/null +++ b/src/Console/RequestCommand.php @@ -0,0 +1,49 @@ +files = $files; + } + + /** + * Execute the console command. + * + * @return void + */ + public function handle(): void + { + $outputPath = base_path('_ide_helper_requests.php'); + Request::writeRequestHelper($this, $this->files, $outputPath); + } +} diff --git a/src/Eloquent.php b/src/Eloquent.php index 23f39dd98..aae26db9f 100644 --- a/src/Eloquent.php +++ b/src/Eloquent.php @@ -25,8 +25,9 @@ class Eloquent * @param Filesystem $files * * @return void + * @throws \ReflectionException|\Illuminate\Contracts\Filesystem\FileNotFoundException */ - public static function writeEloquentModelHelper(Command $command, Filesystem $files) + public static function writeEloquentModelHelper(Command $command, Filesystem $files): void { $class = 'Illuminate\Database\Eloquent\Model'; diff --git a/src/IdeHelperServiceProvider.php b/src/IdeHelperServiceProvider.php index acc8692f2..280e2f5df 100644 --- a/src/IdeHelperServiceProvider.php +++ b/src/IdeHelperServiceProvider.php @@ -15,6 +15,7 @@ use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand; use Barryvdh\LaravelIdeHelper\Console\MetaCommand; use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; +use Barryvdh\LaravelIdeHelper\Console\RequestCommand; use Barryvdh\LaravelIdeHelper\Listeners\GenerateModelHelper; use Illuminate\Console\Events\CommandFinished; use Illuminate\Contracts\Support\DeferrableProvider; @@ -28,7 +29,7 @@ class IdeHelperServiceProvider extends ServiceProvider implements DeferrableProv * * @return void */ - public function boot() + public function boot(): void { if (!$this->app->runningUnitTests() && $this->app['config']->get('ide-helper.post_migrate', [])) { $this->app['events']->listen(CommandFinished::class, GenerateModelHelper::class); @@ -56,7 +57,7 @@ public function boot() * * @return void */ - public function register() + public function register(): void { $configPath = __DIR__ . '/../config/ide-helper.php'; $this->mergeConfigFrom($configPath, 'ide-helper'); @@ -67,6 +68,7 @@ public function register() ModelsCommand::class, MetaCommand::class, EloquentCommand::class, + RequestCommand::class, ] ); } @@ -76,13 +78,14 @@ public function register() * * @return array */ - public function provides() + public function provides(): array { return [ GeneratorCommand::class, ModelsCommand::class, MetaCommand::class, EloquentCommand::class, + RequestCommand::class, ]; } } diff --git a/src/Request.php b/src/Request.php new file mode 100644 index 000000000..9797c3b80 --- /dev/null +++ b/src/Request.php @@ -0,0 +1,51 @@ +exists($requestsPath)) { + $filesList = $files->allFiles($requestsPath); + + foreach ($filesList as $file) { + $relativePath = $file->getRelativePathname(); // Get path relative to base directory + $className = 'App\\Http\\Requests\\' . str_replace(['/', '\\', '.php'], ['\\', '\\', ''], $relativePath); + + if (class_exists($className)) { + $reflection = new \ReflectionClass($className); + + if ($reflection->isSubclassOf('Illuminate\\Foundation\\Http\\FormRequest') && !$reflection->isAbstract()) { + $namespace = $reflection->getNamespaceName(); + $shortName = $reflection->getShortName(); + + $content .= "namespace {$namespace} {\n"; + $content .= " /**\n"; + $content .= " * @method static array rules()\n"; + $content .= " */\n"; + $content .= " class {$shortName} {}\n"; + $content .= "}\n\n"; + } + } + } + } + + $files->put($outputPath, $content); + $command->info('IDE helper file for requests generated successfully.'); + } +} diff --git a/tests/Console/RequestCommand/RequestCommandTest.php b/tests/Console/RequestCommand/RequestCommandTest.php new file mode 100644 index 000000000..e0de3af0a --- /dev/null +++ b/tests/Console/RequestCommand/RequestCommandTest.php @@ -0,0 +1,94 @@ +artisan('config:clear'); + $this->artisan('cache:clear'); + } + + /** + * Define environment setup. + * + * @param \Illuminate\Foundation\Application $app + * @return void + */ + protected function getEnvironmentSetUp($app) + { + // Setup the application environment + } + + /** + * Get package providers. + * + * @param \Illuminate\Foundation\Application $app + * @return array + */ + protected function getPackageProviders($app): array + { + return [ + \Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class, + ]; + } + + /** + * Test the ide-helper:request command with nested directories. + * + * @return void + */ + public function testIdeHelperRequestCommandWithNestedDirectories() + { + // Define paths + $basePath = app_path('Http/Requests'); + $nestedPath = app_path('Http/Requests/Nested'); + $filePath = $nestedPath . '/ExampleNestedRequest.php'; + $outputPath = base_path('_ide_helper_requests.php'); + + // Ensure directories exist + if (!is_dir($nestedPath)) { + mkdir($nestedPath, 0777, true); + } + + // Create a dummy nested request class + file_put_contents($filePath, 'shouldReceive('exists')->andReturn(true); + $filesystem->shouldReceive('allFiles')->andReturn([ + new \SplFileInfo($filePath) + ]); + $filesystem->shouldReceive('put')->once()->with($outputPath, Mockery::type('string'))->andReturn(true); + + $this->app->instance(Filesystem::class, $filesystem); + + // Run the command + Artisan::call('ide-helper:request'); + + // Assert the helper file was created + $this->assertFileExists($outputPath); + + // Read the content to ensure correct namespace handling + $generatedContent = file_get_contents($outputPath); + $this->assertStringContainsString('namespace App\Http\Requests\Nested {', $generatedContent); + $this->assertStringContainsString('class ExampleNestedRequest {}', $generatedContent); + + // Clean up + unlink($filePath); + rmdir($nestedPath); + rmdir($basePath); + unlink($outputPath); + } +}