From c32bb5da3ddf8944e9ad4293fa2f309108521220 Mon Sep 17 00:00:00 2001 From: Gravitano Date: Wed, 6 Jan 2016 15:28:33 +0700 Subject: [PATCH] add new feature: allows to create scaffold from an existing table --- Console/ScaffoldCommand.php | 1 + ScaffoldGenerator.php | 47 ++++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/Console/ScaffoldCommand.php b/Console/ScaffoldCommand.php index 41dcbb7..4023e05 100644 --- a/Console/ScaffoldCommand.php +++ b/Console/ScaffoldCommand.php @@ -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], ]; } diff --git a/ScaffoldGenerator.php b/ScaffoldGenerator.php index 8c095a3..f55c203 100644 --- a/ScaffoldGenerator.php +++ b/ScaffoldGenerator.php @@ -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. * @@ -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'), ]); } @@ -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, ]); } @@ -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()); } /** @@ -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); } /** @@ -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'), ]); } @@ -361,7 +394,9 @@ public function run() $this->generateSeed(); $this->generateRequest(); $this->generateController(); - $this->runMigration(); + if (!$this->existing()) { + $this->runMigration(); + } $this->generateViews(); $this->appendRoute(); }