Skip to content

Commit

Permalink
add make:factory. add all option to make:model to generate entire res…
Browse files Browse the repository at this point in the history
…ource
  • Loading branch information
taylorotwell committed May 6, 2017
1 parent 5c48ed1 commit a6ffd8b
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
69 changes: 69 additions & 0 deletions src/Illuminate/Database/Console/Factories/FactoryMakeCommand.php
Original file line number Diff line number Diff line change
@@ -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";
}
}
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Console/Factories/stubs/factory.stub
Original file line number Diff line number Diff line change
@@ -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 [
//
];
});
31 changes: 29 additions & 2 deletions src/Illuminate/Foundation/Console/ModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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.
*
Expand Down Expand Up @@ -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'],
];
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit a6ffd8b

Please sign in to comment.