Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ide-helper:request command to generate request helper files #1680

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config/ide-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/Console/EloquentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class EloquentCommand extends Command
/**
* @var Filesystem $files
*/
protected $files;
protected Filesystem $files;

/**
* The console command description.
Expand All @@ -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);
}
Expand Down
49 changes: 49 additions & 0 deletions src/Console/RequestCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Barryvdh\LaravelIdeHelper\Console;

use Barryvdh\LaravelIdeHelper\Request;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;

class RequestCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'ide-helper:request';

/**
* @var Filesystem $files
*/
protected Filesystem $files;

/**
* The console command description.
*
* @var string
*/
protected $description = 'Add \Request helper to \Illuminate\Http\Request';

/**
* @param Filesystem $files
*/
public function __construct(Filesystem $files)
{
parent::__construct();
$this->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);
}
}
3 changes: 2 additions & 1 deletion src/Eloquent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
9 changes: 6 additions & 3 deletions src/IdeHelperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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');
Expand All @@ -67,6 +68,7 @@ public function register()
ModelsCommand::class,
MetaCommand::class,
EloquentCommand::class,
RequestCommand::class,
]
);
}
Expand All @@ -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,
];
}
}
51 changes: 51 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Barryvdh\LaravelIdeHelper;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;

class Request
{
/**
* Write the request helper file.
*
* @param Command $command
* @param Filesystem $files
* @param string $outputPath
* @return void
*/
public static function writeRequestHelper(Command $command, Filesystem $files, string $outputPath): void
{
$requestsPath = app_path('Http/Requests');
$content = "<?php\n\n";

if ($files->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.');
}
}
94 changes: 94 additions & 0 deletions tests/Console/RequestCommand/RequestCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Barryvdh\LaravelIdeHelper\Tests\Console;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Artisan;
use Orchestra\Testbench\TestCase;
use Mockery;

class RequestCommandTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->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, '<?php namespace App\Http\Requests\Nested; use Illuminate\Foundation\Http\FormRequest; class ExampleNestedRequest extends FormRequest { public function rules() { return []; } }');

// Ensure the class is autoloaded
require_once $filePath;

// Mock Filesystem
$filesystem = Mockery::mock(Filesystem::class);
$filesystem->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);
}
}