From 1a3b4776bc5641201ac1eb3d681447e83d22594a Mon Sep 17 00:00:00 2001 From: gseph <85199411+gseph@users.noreply.github.com> Date: Thu, 3 Oct 2024 19:52:48 +0200 Subject: [PATCH] Add create:factory command (#1209) Co-authored-by: Luke Towers --- modules/system/ServiceProvider.php | 1 + modules/system/console/CreateFactory.php | 61 +++++++++++++++++++ modules/system/console/CreateModel.php | 20 ++++++ .../console/scaffold/factory/factory.stub | 26 ++++++++ 4 files changed, 108 insertions(+) create mode 100644 modules/system/console/CreateFactory.php create mode 100644 modules/system/console/scaffold/factory/factory.stub diff --git a/modules/system/ServiceProvider.php b/modules/system/ServiceProvider.php index 7867a54f24..3fd2313ce6 100644 --- a/modules/system/ServiceProvider.php +++ b/modules/system/ServiceProvider.php @@ -296,6 +296,7 @@ protected function registerConsole() $this->registerConsoleCommand('create.job', \System\Console\CreateJob::class); $this->registerConsoleCommand('create.migration', \System\Console\CreateMigration::class); $this->registerConsoleCommand('create.model', \System\Console\CreateModel::class); + $this->registerConsoleCommand('create.factory', \System\Console\CreateFactory::class); $this->registerConsoleCommand('create.plugin', \System\Console\CreatePlugin::class); $this->registerConsoleCommand('create.settings', \System\Console\CreateSettings::class); $this->registerConsoleCommand('create.test', \System\Console\CreateTest::class); diff --git a/modules/system/console/CreateFactory.php b/modules/system/console/CreateFactory.php new file mode 100644 index 0000000000..25bffba7fa --- /dev/null +++ b/modules/system/console/CreateFactory.php @@ -0,0 +1,61 @@ +(eg: Winter.Blog)} + {factory : The name of the factory to generate. (eg: PostFactory)} + {--m|model= : The name of the model. (eg: Post)} + {--f|force : Overwrite existing files with generated files.} + + {--uninspiring : Disable inspirational quotes} + '; + + /** + * @var string The console command description. + */ + protected $description = 'Creates a new factory.'; + + /** + * @var array List of commands that this command replaces (aliases) + */ + protected $replaces = [ + 'make:factory', + ]; + + /** + * @var string The type of class being generated. + */ + protected $type = 'Factory'; + + /** + * @var string The argument that the generated class name comes from + */ + protected $nameFrom = 'factory'; + + /** + * @var array A mapping of stubs to generated files. + */ + protected $stubs = [ + 'scaffold/factory/factory.stub' => 'database/factories/{{studly_name}}.php', + ]; + + protected function processVars($vars): array + { + $vars = parent::processVars($vars); + + $vars['model'] = $this->option('model'); + + return $vars; + } +} diff --git a/modules/system/console/CreateModel.php b/modules/system/console/CreateModel.php index 7d884a8c32..0b4fa6de39 100644 --- a/modules/system/console/CreateModel.php +++ b/modules/system/console/CreateModel.php @@ -21,6 +21,7 @@ class CreateModel extends BaseScaffoldCommand {--a|all : Generate a controller, migration, & seeder for the model} {--c|controller : Create a new controller for the model} {--s|seed : Create a new seeder for the model} + {--F|factory : Create a new factory for the model} {--p|pivot : Indicates if the generated model should be a custom intermediate table model} {--no-migration : Don\'t create a migration file for the model} {--uninspiring : Disable inspirational quotes} @@ -71,6 +72,7 @@ public function handle() if ($this->option('all')) { $this->input->setOption('controller', true); $this->input->setOption('seed', true); + $this->input->setOption('factory', true); } if ($this->option('controller')) { @@ -84,6 +86,10 @@ public function handle() if (!$this->option('no-migration')) { $this->createMigration(); } + + if ($this->option('factory')) { + $this->createFactory(); + } } /** @@ -154,4 +160,18 @@ public function createController() '--uninspiring' => $this->option('uninspiring'), ]); } + + /** + * Create a factory class for the model. + */ + public function createFactory(): void + { + $this->call('create:factory', [ + 'plugin' => $this->getPluginIdentifier(), + 'factory' => "{$this->getNameInput()}Factory", + '--model' => $this->getNameInput(), + '--force' => $this->option('force'), + '--uninspiring' => $this->option('uninspiring'), + ]); + } } diff --git a/modules/system/console/scaffold/factory/factory.stub b/modules/system/console/scaffold/factory/factory.stub new file mode 100644 index 0000000000..8695bd361c --- /dev/null +++ b/modules/system/console/scaffold/factory/factory.stub @@ -0,0 +1,26 @@ + +{% endif %} + */ +class {{ studly_name }} extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + // + ]; + } +}