From b6fbfcd6dc95a522c3dcbdd8bea628a95ed3d7e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Mon, 25 Sep 2023 13:31:20 +0200 Subject: [PATCH] resource stub --- src/Console/Commands/ResourceMake.php | 38 +++++++++++++++++++++++++++ stubs/Resource.stub | 5 ++++ 2 files changed, 43 insertions(+) diff --git a/src/Console/Commands/ResourceMake.php b/src/Console/Commands/ResourceMake.php index 5506aae0e..5ee57d674 100644 --- a/src/Console/Commands/ResourceMake.php +++ b/src/Console/Commands/ResourceMake.php @@ -3,6 +3,8 @@ namespace Cone\Root\Console\Commands; use Illuminate\Console\GeneratorCommand; +use Illuminate\Support\Str; +use Symfony\Component\Console\Input\InputOption; class ResourceMake extends GeneratorCommand { @@ -44,4 +46,40 @@ protected function getDefaultNamespace($rootNamespace): string { return $rootNamespace.'\\Root\\Resources'; } + + /** + * Build the class with the given name. + * + * @param string $name + */ + protected function buildClass($name): string + { + $class = parent::buildClass($name); + + return $this->replaceModel($class); + } + + /** + * Create a new method. + */ + public function replaceModel(string $class): string + { + $model = $this->option('model'); + + $model = str_replace('/', '\\', $model); + + $model = sprintf('\\%s::class', trim($model, '\\')); + + return str_replace('{{ model }}', $model, $class); + } + + /** + * Get the console command options. + */ + protected function getOptions(): array + { + return [ + ['model', null, InputOption::VALUE_OPTIONAL, 'The Eloquent Model'], + ]; + } } diff --git a/stubs/Resource.stub b/stubs/Resource.stub index cf1c57e69..def29d755 100644 --- a/stubs/Resource.stub +++ b/stubs/Resource.stub @@ -13,6 +13,11 @@ use Illuminate\Http\Request; class {{ class }} extends Resource { + /** + * The model class. + */ + protected string $model = {{ model }}; + /** * Define the widgets for the resource. */