Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create:factory command #1209

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/system/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -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);
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
@@ -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'),
]);
}
}
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 [
//
];
}
}
Loading