From b673601dcd56a51ae27002670c9502959ecb61c5 Mon Sep 17 00:00:00 2001 From: Sam Maosa Date: Sat, 6 Apr 2024 15:28:32 +0300 Subject: [PATCH] New Feature: Generate Models - Can correctly generate a model - Can specify to generate a migration with it - TODO: override the creation of other classes alongside the model once these implementations are in place, e.g factory, controller etc. --- src/Commands/ModelMakeCommand.php | 125 ++++++++++++++++++++++++++++++ src/Module.php | 4 + 2 files changed, 129 insertions(+) create mode 100644 src/Commands/ModelMakeCommand.php diff --git a/src/Commands/ModelMakeCommand.php b/src/Commands/ModelMakeCommand.php new file mode 100644 index 0000000..c40d754 --- /dev/null +++ b/src/Commands/ModelMakeCommand.php @@ -0,0 +1,125 @@ +argument('module')); + } + + protected function getDefaultNamespace($rootNamespace): string + { + return $rootNamespace.'\\Models'; + } + + protected function rootNamespace(): string + { + return $this->getModule()->getRootNamespace(); + } + protected function getPath($name): string + { + $name = Str::replaceFirst($this->rootNamespace(), '', $name); + return $this->getModule()->srcPath(str_replace('\\', '/', $name).'.php'); + } + + protected function createFactory(): void + { + $factory = Str::studly($this->argument('name')); + + $this->call('make:factory', [ + 'name' => "{$factory}Factory", + '--model' => $this->qualifyClass($this->getNameInput()), + ]); + } + + /** + * Create a migration file for the model. + * + * @return void + */ + protected function createMigration(): void + { + $table = Str::snake(Str::pluralStudly(class_basename($this->argument('name')))); + + if ($this->option('pivot')) { + $table = Str::singular($table); + } + + $this->call('modular:make-migration', [ + 'name' => "create_{$table}_table", + 'module' => $this->getModule()->name(), + '--create' => $table, + ]); + } + + /** + * Create a seeder file for the model. + * + * @return void + */ + protected function createSeeder(): void + { + $seeder = Str::studly(class_basename($this->argument('name'))); + + $this->call('make:seeder', [ + 'name' => "{$seeder}Seeder", + ]); + } + + /** + * Create a controller for the model. + * + * @return void + */ + protected function createController(): void + { + $controller = Str::studly(class_basename($this->argument('name'))); + + $modelName = $this->qualifyClass($this->getNameInput()); + + $this->call('make:controller', array_filter([ + 'name' => "{$controller}Controller", + '--model' => $this->option('resource') || $this->option('api') ? $modelName : null, + '--api' => $this->option('api'), + '--requests' => $this->option('requests') || $this->option('all'), + '--test' => $this->option('test'), + '--pest' => $this->option('pest'), + ])); + } + + /** + * Create a policy file for the model. + * + * @return void + */ + protected function createPolicy(): void + { + $policy = Str::studly(class_basename($this->argument('name'))); + + $this->call('make:policy', [ + 'name' => "{$policy}Policy", + '--model' => $this->qualifyClass($this->getNameInput()), + ]); + } +} diff --git a/src/Module.php b/src/Module.php index 9a719b5..aab13d6 100644 --- a/src/Module.php +++ b/src/Module.php @@ -37,6 +37,10 @@ public function path(string $path = '', bool $relative = false): string return $basePath . ($path ? DIRECTORY_SEPARATOR . trim($path, DIRECTORY_SEPARATOR) : ''); } + public function getRootNamespace(): string + { + return $this->namespace.'\\'; + } public function makeNamespace(string $relativeNamespace = ''): string { return $this->namespace . ($relativeNamespace ? '\\' . ltrim($relativeNamespace, '\\') : '');