Skip to content

Commit 1a3b477

Browse files
sephyldLukeTowers
andauthored
Add create:factory command (#1209)
Co-authored-by: Luke Towers <github@luketowers.ca>
1 parent 5a1fa57 commit 1a3b477

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

modules/system/ServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ protected function registerConsole()
296296
$this->registerConsoleCommand('create.job', \System\Console\CreateJob::class);
297297
$this->registerConsoleCommand('create.migration', \System\Console\CreateMigration::class);
298298
$this->registerConsoleCommand('create.model', \System\Console\CreateModel::class);
299+
$this->registerConsoleCommand('create.factory', \System\Console\CreateFactory::class);
299300
$this->registerConsoleCommand('create.plugin', \System\Console\CreatePlugin::class);
300301
$this->registerConsoleCommand('create.settings', \System\Console\CreateSettings::class);
301302
$this->registerConsoleCommand('create.test', \System\Console\CreateTest::class);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php namespace System\Console;
2+
3+
use System\Console\BaseScaffoldCommand;
4+
5+
class CreateFactory extends BaseScaffoldCommand
6+
{
7+
/**
8+
* @var string|null The default command name for lazy loading.
9+
*/
10+
protected static $defaultName = 'create:factory';
11+
12+
/**
13+
* @var string The name and signature of this command.
14+
*/
15+
protected $signature = 'create:factory
16+
{plugin : The name of the plugin. <info>(eg: Winter.Blog)</info>}
17+
{factory : The name of the factory to generate. <info>(eg: PostFactory)</info>}
18+
{--m|model= : The name of the model. <info>(eg: Post)</info>}
19+
{--f|force : Overwrite existing files with generated files.}
20+
21+
{--uninspiring : Disable inspirational quotes}
22+
';
23+
24+
/**
25+
* @var string The console command description.
26+
*/
27+
protected $description = 'Creates a new factory.';
28+
29+
/**
30+
* @var array List of commands that this command replaces (aliases)
31+
*/
32+
protected $replaces = [
33+
'make:factory',
34+
];
35+
36+
/**
37+
* @var string The type of class being generated.
38+
*/
39+
protected $type = 'Factory';
40+
41+
/**
42+
* @var string The argument that the generated class name comes from
43+
*/
44+
protected $nameFrom = 'factory';
45+
46+
/**
47+
* @var array A mapping of stubs to generated files.
48+
*/
49+
protected $stubs = [
50+
'scaffold/factory/factory.stub' => 'database/factories/{{studly_name}}.php',
51+
];
52+
53+
protected function processVars($vars): array
54+
{
55+
$vars = parent::processVars($vars);
56+
57+
$vars['model'] = $this->option('model');
58+
59+
return $vars;
60+
}
61+
}

modules/system/console/CreateModel.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class CreateModel extends BaseScaffoldCommand
2121
{--a|all : Generate a controller, migration, & seeder for the model}
2222
{--c|controller : Create a new controller for the model}
2323
{--s|seed : Create a new seeder for the model}
24+
{--F|factory : Create a new factory for the model}
2425
{--p|pivot : Indicates if the generated model should be a custom intermediate table model}
2526
{--no-migration : Don\'t create a migration file for the model}
2627
{--uninspiring : Disable inspirational quotes}
@@ -71,6 +72,7 @@ public function handle()
7172
if ($this->option('all')) {
7273
$this->input->setOption('controller', true);
7374
$this->input->setOption('seed', true);
75+
$this->input->setOption('factory', true);
7476
}
7577

7678
if ($this->option('controller')) {
@@ -84,6 +86,10 @@ public function handle()
8486
if (!$this->option('no-migration')) {
8587
$this->createMigration();
8688
}
89+
90+
if ($this->option('factory')) {
91+
$this->createFactory();
92+
}
8793
}
8894

8995
/**
@@ -154,4 +160,18 @@ public function createController()
154160
'--uninspiring' => $this->option('uninspiring'),
155161
]);
156162
}
163+
164+
/**
165+
* Create a factory class for the model.
166+
*/
167+
public function createFactory(): void
168+
{
169+
$this->call('create:factory', [
170+
'plugin' => $this->getPluginIdentifier(),
171+
'factory' => "{$this->getNameInput()}Factory",
172+
'--model' => $this->getNameInput(),
173+
'--force' => $this->option('force'),
174+
'--uninspiring' => $this->option('uninspiring'),
175+
]);
176+
}
157177
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace {{ plugin_namespace }}\Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
7+
/**
8+
* {{ name }} Factory
9+
{% if model %}
10+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\{{ plugin_namespace }}\Models\{{ model }}>
11+
{% endif %}
12+
*/
13+
class {{ studly_name }} extends Factory
14+
{
15+
/**
16+
* Define the model's default state.
17+
*
18+
* @return array<string, mixed>
19+
*/
20+
public function definition()
21+
{
22+
return [
23+
//
24+
];
25+
}
26+
}

0 commit comments

Comments
 (0)