generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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.
- Loading branch information
1 parent
5a9fe43
commit b673601
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
namespace Savannabits\Modular\Commands; | ||
|
||
use Illuminate\Foundation\Console\ModelMakeCommand as BaseModelMakeCommand; | ||
use Illuminate\Support\Str; | ||
use Savannabits\Modular\Facades\Modular; | ||
use Savannabits\Modular\Module; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
|
||
#[AsCommand(name: 'modular:make-model')] | ||
class ModelMakeCommand extends BaseModelMakeCommand | ||
{ | ||
protected $name = 'modular:make-model'; | ||
|
||
protected $description = 'Create a new Eloquent model class in a modular package'; | ||
|
||
protected function getArguments(): array | ||
{ | ||
return array_merge(parent::getArguments(),[ | ||
['module', InputArgument::REQUIRED, 'The name of the module in which this should be installed'], | ||
]); | ||
} | ||
|
||
public function getModule(): Module | ||
{ | ||
return Modular::module($this->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()), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters