Skip to content

Commit

Permalink
add new feature: allows to create scaffold from an existing table
Browse files Browse the repository at this point in the history
  • Loading branch information
Gravitano committed Jan 6, 2016
1 parent 3e15205 commit c32bb5d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions Console/ScaffoldCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function getOptions()
['fields', null, InputOption::VALUE_OPTIONAL, 'The fields of migration. Separated with comma (,).', null],
['prefix', null, InputOption::VALUE_OPTIONAL, 'The prefix path & routes.', null],
['no-question', null, InputOption::VALUE_NONE, 'Don\'t ask any question.', null],
['existing', 'e', InputOption::VALUE_NONE, 'Generate scaffold from an existing table.', null],
['force', 'f', InputOption::VALUE_NONE, 'Force the creation if file already exists.', null],
];
}
Expand Down
47 changes: 41 additions & 6 deletions ScaffoldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ public function getEntities()
return str_plural($this->getEntity());
}

/**
* Determine whether the creation is from an existing table.
*
* @return boolen
*/
public function existing()
{
return $this->console->option('existing');
}

/**
* Get schema fields.
*
* @return string
*/
public function getFields()
{
if ($this->existing()) {
return TableDumper::make($this->getEntities())->toSchema();
}

return $this->console->option('fields');
}

/**
* Get controller name.
*
Expand Down Expand Up @@ -112,7 +136,7 @@ public function generateModel()

$this->console->call('generate:model', [
'name' => $this->getEntity(),
'--fillable' => $this->console->option('fields'),
'--fillable' => $this->getFields(),
'--force' => $this->console->option('force'),
]);
}
Expand Down Expand Up @@ -141,10 +165,14 @@ public function generateMigration()
return;
}

$existing = $this->existing();
$table = $this->getEntities();

$this->console->call('generate:migration', [
'name' => "create_{$this->getEntities()}_table",
'name' => $existing ? $table : "create_{$table}_table",
'--fields' => $this->console->option('fields'),
'--force' => $this->console->option('force'),
'--existing' => $existing,
]);
}

Expand Down Expand Up @@ -205,7 +233,7 @@ public function getControllerScaffolder()
*/
public function getFormGenerator()
{
return new FormGenerator($this->getEntities(), $this->console->option('fields'));
return new FormGenerator($this->getEntities(), $this->getFields());
}

/**
Expand All @@ -219,7 +247,12 @@ public function getTableDumper()
return new TableDumper($this->getEntities());
}

return new FieldsDumper($this->console->option('fields'));
if ($this->existing()) {
return TableDumper::make($this->getEntities())
->except(['id', 'created_at', 'updated_at', 'deleted_at']);
}

return new FieldsDumper($fields);
}

/**
Expand Down Expand Up @@ -345,7 +378,7 @@ public function generateRequest()
'name' => $name,
'--scaffold' => true,
'--auth' => true,
'--rules' => $this->console->option('fields'),
'--rules' => $this->existing() ? $this->getTableDumper()->toSchema() : $this->getFields(),
'--force' => $this->console->option('force'),
]);
}
Expand All @@ -361,7 +394,9 @@ public function run()
$this->generateSeed();
$this->generateRequest();
$this->generateController();
$this->runMigration();
if (!$this->existing()) {
$this->runMigration();
}
$this->generateViews();
$this->appendRoute();
}
Expand Down

0 comments on commit c32bb5d

Please sign in to comment.