Permalink
Browse files

add make:factory. add all option to make:model to generate entire res…

…ource
  • Loading branch information...
taylorotwell committed May 6, 2017
1 parent 5c48ed1 commit a6ffd8bfa896844fee4b4c83cc6aed9d0c33fd9d
@@ -0,0 +1,69 @@
<?php
namespace Illuminate\Database\Console\Factories;
use Illuminate\Console\GeneratorCommand;
class FactoryMakeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:factory';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new model factory';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Factory';
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/stubs/factory.stub';
}
/**
* Build the class with the given name.
*
* @param string $name
* @return string
*/
protected function buildClass($name)
{
$model = $this->qualifyClass($this->argument('name'));
return str_replace(
'DummyModel', $model, parent::buildClass($name)
);
}
/**
* Get the destination class path.
*
* @param string $name
* @return string
*/
protected function getPath($name)
{
$name = str_replace(
['\\', '/'], '', $this->argument('name')
).'Factory';
return $this->laravel->databasePath()."/factories/{$name}.php";
}
}
@@ -0,0 +1,20 @@
<?php
use Fake\Generator as Faker;
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/
$factory->define(DummyModel::class, function (Faker $faker) {
return [
//
];
});
@@ -40,6 +40,17 @@ public function fire()
return;
}
if ($this->option('all')) {
$this->input->setOption('factory', true);
$this->input->setOption('migration', true);
$this->input->setOption('controller', true);
$this->input->setOption('resource', true);
}
if ($this->option('factory')) {
$this->createFactory();
}
if ($this->option('migration')) {
$this->createMigration();
}
@@ -49,6 +60,18 @@ public function fire()
}
}
/**
* Create a model factory for the model.
*
* @return void
*/
protected function createFactory()
{
$this->call('make:factory', [
'name' => $this->argument('name'),
]);
}
/**
* Create a migration file for the model.
*
@@ -110,9 +133,13 @@ protected function getDefaultNamespace($rootNamespace)
protected function getOptions()
{
return [
['migration', 'm', InputOption::VALUE_NONE, 'Create a new migration file for the model.'],
['all', 'a', InputOption::VALUE_NONE, 'Generate a migration, factory, and resource controller for the model'],
['controller', 'c', InputOption::VALUE_NONE, 'Create a new controller for the model'],
['factory', 'f', InputOption::VALUE_NONE, 'Create a new factory for the model'],
['controller', 'c', InputOption::VALUE_NONE, 'Create a new controller for the model.'],
['migration', 'm', InputOption::VALUE_NONE, 'Create a new migration file for the model'],
['resource', 'r', InputOption::VALUE_NONE, 'Indicates if the generated controller should be a resource controller'],
];
@@ -45,6 +45,7 @@
use Illuminate\Database\Console\Seeds\SeederMakeCommand;
use Illuminate\Database\Console\Migrations\MigrateCommand;
use Illuminate\Foundation\Console\NotificationMakeCommand;
use Illuminate\Database\Console\Factories\FactoryMakeCommand;
use Illuminate\Queue\Console\WorkCommand as QueueWorkCommand;
use Illuminate\Database\Console\Migrations\MigrateMakeCommand;
use Illuminate\Notifications\Console\NotificationTableCommand;
@@ -127,6 +128,7 @@ class ArtisanServiceProvider extends ServiceProvider
'ControllerMake' => 'command.controller.make',
'EventGenerate' => 'command.event.generate',
'EventMake' => 'command.event.make',
'FactoryMake' => 'command.factory.make',
'JobMake' => 'command.job.make',
'ListenerMake' => 'command.listener.make',
'MailMake' => 'command.mail.make',
@@ -330,6 +332,18 @@ protected function registerEventMakeCommand()
});
}
/**
* Register the command.
*
* @return void
*/
protected function registerFactoryMakeCommand()
{
$this->app->singleton('command.factory.make', function ($app) {
return new FactoryMakeCommand($app['files']);
});
}
/**
* Register the command.
*

0 comments on commit a6ffd8b

Please sign in to comment.