Skip to content

Commit

Permalink
Add create:factory command (#1209)
Browse files Browse the repository at this point in the history
Co-authored-by: Luke Towers <github@luketowers.ca>
  • Loading branch information
sephyld and LukeTowers authored Oct 3, 2024
1 parent 5a1fa57 commit 1a3b477
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/system/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
61 changes: 61 additions & 0 deletions modules/system/console/CreateFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php namespace System\Console;

use System\Console\BaseScaffoldCommand;

class CreateFactory extends BaseScaffoldCommand
{
/**
* @var string|null The default command name for lazy loading.
*/
protected static $defaultName = 'create:factory';

/**
* @var string The name and signature of this command.
*/
protected $signature = 'create:factory
{plugin : The name of the plugin. <info>(eg: Winter.Blog)</info>}
{factory : The name of the factory to generate. <info>(eg: PostFactory)</info>}
{--m|model= : The name of the model. <info>(eg: Post)</info>}
{--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;
}
}
20 changes: 20 additions & 0 deletions modules/system/console/CreateModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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')) {
Expand All @@ -84,6 +86,10 @@ public function handle()
if (!$this->option('no-migration')) {
$this->createMigration();
}

if ($this->option('factory')) {
$this->createFactory();
}
}

/**
Expand Down Expand Up @@ -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'),
]);
}
}
26 changes: 26 additions & 0 deletions modules/system/console/scaffold/factory/factory.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace {{ plugin_namespace }}\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
* {{ name }} Factory
{% if model %}
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\{{ plugin_namespace }}\Models\{{ model }}>
{% endif %}
*/
class {{ studly_name }} extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
//
];
}
}

0 comments on commit 1a3b477

Please sign in to comment.