Skip to content

Commit a6ffd8b

Browse files
committed
add make:factory. add all option to make:model to generate entire resource
1 parent 5c48ed1 commit a6ffd8b

File tree

4 files changed

+132
-2
lines changed

4 files changed

+132
-2
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Illuminate\Database\Console\Factories;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
7+
class FactoryMakeCommand extends GeneratorCommand
8+
{
9+
/**
10+
* The console command name.
11+
*
12+
* @var string
13+
*/
14+
protected $name = 'make:factory';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Create a new model factory';
22+
23+
/**
24+
* The type of class being generated.
25+
*
26+
* @var string
27+
*/
28+
protected $type = 'Factory';
29+
30+
/**
31+
* Get the stub file for the generator.
32+
*
33+
* @return string
34+
*/
35+
protected function getStub()
36+
{
37+
return __DIR__.'/stubs/factory.stub';
38+
}
39+
40+
/**
41+
* Build the class with the given name.
42+
*
43+
* @param string $name
44+
* @return string
45+
*/
46+
protected function buildClass($name)
47+
{
48+
$model = $this->qualifyClass($this->argument('name'));
49+
50+
return str_replace(
51+
'DummyModel', $model, parent::buildClass($name)
52+
);
53+
}
54+
55+
/**
56+
* Get the destination class path.
57+
*
58+
* @param string $name
59+
* @return string
60+
*/
61+
protected function getPath($name)
62+
{
63+
$name = str_replace(
64+
['\\', '/'], '', $this->argument('name')
65+
).'Factory';
66+
67+
return $this->laravel->databasePath()."/factories/{$name}.php";
68+
}
69+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use Fake\Generator as Faker;
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Model Factories
8+
|--------------------------------------------------------------------------
9+
|
10+
| This directory should contain each of the model factory definitions for
11+
| your application. Factories provide a convenient way to generate new
12+
| model instances for testing / seeding your application's database.
13+
|
14+
*/
15+
16+
$factory->define(DummyModel::class, function (Faker $faker) {
17+
return [
18+
//
19+
];
20+
});

src/Illuminate/Foundation/Console/ModelMakeCommand.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ public function fire()
4040
return;
4141
}
4242

43+
if ($this->option('all')) {
44+
$this->input->setOption('factory', true);
45+
$this->input->setOption('migration', true);
46+
$this->input->setOption('controller', true);
47+
$this->input->setOption('resource', true);
48+
}
49+
50+
if ($this->option('factory')) {
51+
$this->createFactory();
52+
}
53+
4354
if ($this->option('migration')) {
4455
$this->createMigration();
4556
}
@@ -49,6 +60,18 @@ public function fire()
4960
}
5061
}
5162

63+
/**
64+
* Create a model factory for the model.
65+
*
66+
* @return void
67+
*/
68+
protected function createFactory()
69+
{
70+
$this->call('make:factory', [
71+
'name' => $this->argument('name'),
72+
]);
73+
}
74+
5275
/**
5376
* Create a migration file for the model.
5477
*
@@ -110,9 +133,13 @@ protected function getDefaultNamespace($rootNamespace)
110133
protected function getOptions()
111134
{
112135
return [
113-
['migration', 'm', InputOption::VALUE_NONE, 'Create a new migration file for the model.'],
136+
['all', 'a', InputOption::VALUE_NONE, 'Generate a migration, factory, and resource controller for the model'],
137+
138+
['controller', 'c', InputOption::VALUE_NONE, 'Create a new controller for the model'],
139+
140+
['factory', 'f', InputOption::VALUE_NONE, 'Create a new factory for the model'],
114141

115-
['controller', 'c', InputOption::VALUE_NONE, 'Create a new controller for the model.'],
142+
['migration', 'm', InputOption::VALUE_NONE, 'Create a new migration file for the model'],
116143

117144
['resource', 'r', InputOption::VALUE_NONE, 'Indicates if the generated controller should be a resource controller'],
118145
];

src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use Illuminate\Database\Console\Seeds\SeederMakeCommand;
4646
use Illuminate\Database\Console\Migrations\MigrateCommand;
4747
use Illuminate\Foundation\Console\NotificationMakeCommand;
48+
use Illuminate\Database\Console\Factories\FactoryMakeCommand;
4849
use Illuminate\Queue\Console\WorkCommand as QueueWorkCommand;
4950
use Illuminate\Database\Console\Migrations\MigrateMakeCommand;
5051
use Illuminate\Notifications\Console\NotificationTableCommand;
@@ -127,6 +128,7 @@ class ArtisanServiceProvider extends ServiceProvider
127128
'ControllerMake' => 'command.controller.make',
128129
'EventGenerate' => 'command.event.generate',
129130
'EventMake' => 'command.event.make',
131+
'FactoryMake' => 'command.factory.make',
130132
'JobMake' => 'command.job.make',
131133
'ListenerMake' => 'command.listener.make',
132134
'MailMake' => 'command.mail.make',
@@ -330,6 +332,18 @@ protected function registerEventMakeCommand()
330332
});
331333
}
332334

335+
/**
336+
* Register the command.
337+
*
338+
* @return void
339+
*/
340+
protected function registerFactoryMakeCommand()
341+
{
342+
$this->app->singleton('command.factory.make', function ($app) {
343+
return new FactoryMakeCommand($app['files']);
344+
});
345+
}
346+
333347
/**
334348
* Register the command.
335349
*

0 commit comments

Comments
 (0)