diff --git a/src/Commands/ModelMakeCommand.php b/src/Commands/ModelMakeCommand.php new file mode 100644 index 0000000..a3f038f --- /dev/null +++ b/src/Commands/ModelMakeCommand.php @@ -0,0 +1,119 @@ +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. + */ + 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. + */ + 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. + */ + 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. + */ + 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 27601f0..08ed944 100644 --- a/src/Module.php +++ b/src/Module.php @@ -42,6 +42,11 @@ 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, '\\') : '');