wn:migration command added

This commit is contained in:
Amine Ben hammou 2015-09-26 02:43:35 +01:00
parent df6abe636c
commit 3fb6c053a9
6 changed files with 126 additions and 4 deletions

View File

@ -0,0 +1,63 @@
<?php
$I = new AcceptanceTester($scenario);
$I->wantTo('generate a migration without schema');
$I->runShellCommand('php artisan wn:migration tasks');
$I->seeInShellOutput('tasks migration generated');
$I->seeFileFound('./database/migrations/create_tasks.php');
$I->openFile('./database/migrations/create_tasks.php');
$I->seeFileContentsEqual('<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTasksMigration extends Migration
{
public function up()
{
Schema::create(\'tasks\', function(Blueprint $table) {
$table->increments(\'id\');
// Schema declaration
$table->timestamps();
});
}
public function down()
{
Schema::drop(\'tasks\');
}
}
');
$I->deleteFile('./database/migrations/create_tasks.php');
$I->wantTo('generate a migration without schema');
$I->runShellCommand('php artisan wn:migration tasks --schema="amount:decimal.5,2:after.\'size\':default.8 title:string:nullable"');
$I->seeInShellOutput('tasks migration generated');
$I->seeFileFound('./database/migrations/create_tasks.php');
$I->openFile('./database/migrations/create_tasks.php');
$I->seeFileContentsEqual('<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTasksMigration extends Migration
{
public function up()
{
Schema::create(\'tasks\', function(Blueprint $table) {
$table->increments(\'id\');
$table->decimal(\'amount\', 5, 2)->after(\'size\')->default(8);
$table->string(\'title\')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::drop(\'tasks\');
}
}
');
$I->deleteFile('./database/migrations/create_tasks.php');

View File

@ -0,0 +1,61 @@
<?php namespace Wn\Generators\Commands;
class MigrationCommand extends BaseCommand {
protected $signature = 'wn:migration
{table : The table name.}
{--schema= : the schema.}';
// {action : One of create, add, remove or drop options.}
// The action is only create for the moment
protected $description = 'Generates a migration to create a table with schema';
public function handle()
{
$table = $this->argument('table');
$name = 'Create' . ucwords(camel_case($table));
$content = $this->getTemplate('migration')
->with([
'table' => $table,
'name' => $name,
'schema' => $this->getSchema()
])
->get();
$name = snake_case($name);
$this->save($content, "./database/migrations/{$name}.php");
$this->info("{$table} migration generated !");
}
protected function getSchema()
{
$schema = $this->option('schema');
if(! $schema){
return "\t\t\t// Schema declaration";
}
$items = $this->getArgumentParser('schema')->parse($schema);
$fields = [];
foreach ($items as $item) {
$fields[] = $this->getFieldDeclaration($item);
}
return implode(PHP_EOL, $fields);
}
protected function getFieldDeclaration($parts)
{
$name = $parts[0]['name'];
$parts[1]['args'] = array_merge(["'{$name}'"], $parts[1]['args']);
unset($parts[0]);
$parts = array_map(function($part){
return '->' . $part['name'] . '(' . implode(', ', $part['args']) . ')';
}, $parts);
return "\t\t\t\$table" . implode('', $parts) . ';';
}
}

View File

@ -15,8 +15,6 @@ class ModelCommand extends BaseCommand {
protected $description = 'Generates a model class for a RESTfull resource'; protected $description = 'Generates a model class for a RESTfull resource';
protected $fields = [];
public function handle() public function handle()
{ {
$name = $this->argument('name'); $name = $this->argument('name');

View File

@ -11,7 +11,7 @@ class CommandsServiceProvider extends ServiceProvider
$this->registerControllerRestActionsCommand(); $this->registerControllerRestActionsCommand();
$this->registerControllerCommand(); $this->registerControllerCommand();
$this->registerRouteCommand(); $this->registerRouteCommand();
// $this->registerMigrationCommand(); $this->registerMigrationCommand();
// $this->registerSeedCommand(); // $this->registerSeedCommand();
// $this->registerTestCommand(); // $this->registerTestCommand();
// $this->registerResourceCommand(); // $this->registerResourceCommand();

View File

@ -10,7 +10,7 @@ class {{name}}Migration extends Migration
{ {
Schema::create('{{table}}', function(Blueprint $table) { Schema::create('{{table}}', function(Blueprint $table) {
$table->increments('id'); $table->increments('id');
{{fields}} {{schema}}
$table->timestamps(); $table->timestamps();
}); });
} }