Created
October 27, 2018 17:46
-
-
Save tajulasri/05477f70661db4727ad7dc0caff83125 to your computer and use it in GitHub Desktop.
Laravel command for generate form bootstrap from tables attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Support\Facades\File; | |
class MakeFormCommand extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'make:form {table} {--path= : View path} {--route=/create : form create route}'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Generate create form for each table or models assigned.'; | |
/** | |
* @var array | |
*/ | |
protected $fields = []; | |
/** | |
* @var string | |
*/ | |
private $filename = 'create.blade.php'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
$attributes = json_decode(json_encode($this->scanAttributes()), true); | |
$formFields = $this->mapToDataType($attributes); | |
$formsHtml = ''; | |
foreach ($this->fields as $field) { | |
$formsHtml .= '<div class="form-group">'.PHP_EOL; | |
$formsHtml .= sprintf('<label for="%s">%s</label>', $field['name'], ucfirst(str_replace('_', ' ', $field['name']))); | |
$formsHtml .= PHP_EOL; | |
$formsHtml .= sprintf( | |
'<input type="%s" name="%s" placeholder="%s" class="form-control" />', | |
$field['type'], $field['name'], ucfirst(str_replace('_', ' ', $field['name'])) | |
).PHP_EOL; | |
$formsHtml .= '</div>'; | |
$formsHtml .= PHP_EOL; | |
} | |
$formContent = file_get_contents(__DIR__.'/stubs/create.stub'); | |
$replace = [ | |
'Page' => 'Create', | |
'Route' => $this->option('route') ?? '/create', | |
'FormField' => $formsHtml, | |
]; | |
//$this->info($formsHtml); | |
$subtituteContents = str_replace( | |
array_keys($replace), array_values($replace), $formContent | |
); | |
$this->info($subtituteContents); | |
if ($this->doesViewExists()) { | |
if ($this->confirm("Form path {$this->getViewPath()} already exists. Overwrite views?")) { | |
file_put_contents($this->getViewPath(), $subtituteContents); | |
$this->info("create form for view {$this->getViewPath()}"); | |
} | |
} else { | |
file_put_contents($this->getViewPath(), $subtituteContents); | |
$this->info("create form for view {$this->getViewPath()}"); | |
} | |
} | |
/** | |
* @return mixed | |
*/ | |
protected function getViewPath() | |
{ | |
$path = $this->normalizedPathSeperator($this->option('path')); | |
return $formPath = resource_path('views/'.$path.$this->filename); | |
} | |
protected function doesViewExists() | |
{ | |
if ($this->option('path')) { | |
$formPath = $this->getViewPath(); | |
return File::exists($formPath); | |
} | |
return false; | |
} | |
/** | |
* @param $path | |
*/ | |
private function normalizedPathSeperator($path) | |
{ | |
return sprintf(str_replace('.', DIRECTORY_SEPARATOR, $path)."%s", DIRECTORY_SEPARATOR); | |
} | |
/** | |
* @param $attributes | |
*/ | |
protected function mapToDataType($attributes) | |
{ | |
foreach ($attributes as $attribute) { | |
$this->fields[] = [ | |
'name' => $attribute['Field'], | |
'type' => 'text', | |
]; | |
} | |
} | |
protected function scanAttributes() | |
{ | |
return DB::select('DESCRIBE '.$this->argument('table')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment