Skip to content

Instantly share code, notes, and snippets.

Created November 20, 2017 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4ef2b362c9344195450f60cb964806c8 to your computer and use it in GitHub Desktop.
Save anonymous/4ef2b362c9344195450f60cb964806c8 to your computer and use it in GitHub Desktop.
<?php
namespace App\Console\Commands;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
use Illuminate\Console\Command;
class ViewScaffold extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'view:scaffold';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Read database schema and create CRUD Blade templates';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$config = new Configuration();
//..
$connectionParams = array(
'dbname' => env('DB_DATABASE'),
'user' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'host' => 'localhost',
'driver' => 'pdo_mysql',
);
$conn = DriverManager::getConnection($connectionParams, $config);
$sm = $conn->getSchemaManager();
$platform = $sm->getDatabasePlatform();
//doctrine/dbal doesnt support enum yet lmao, so have to map it to string for now
//and you'll manually write the html for your enum types (which is a rare type anyway)
$platform->registerDoctrineTypeMapping("enum","string");
foreach ($sm->listTables() as $table) {
//pass table name and data to view creating functions
$name = $table->getName();
$data = array();
foreach ($table->getColumns() as $column) {
$data[] = array("name" => $column->getName(),"type" => $column->getType());
}
//create a folder for table, if possible
$folder_path = base_path() . "/resources/views/" . $table->getName();
if(mkdir($folder_path)) {
//create views
file_put_contents($folder_path . "/index.blade.php",$this->create_index($table->getName(),$data));
file_put_contents($folder_path . "/show.blade.php",$this->create_show($table->getName(),$data));
file_put_contents($folder_path . "/edit.blade.php",$this->create_edit($table->getName(),$data));
file_put_contents($folder_path . "/create.blade.php",$this->create_create($table->getName(),$data));
$this->info("Created views for " . $table->getName() . "\n");
} else {
$this->error("Failed creating folders for views, exiting...");
return;
}
}
}
public function create_index($table_name,$data) {
$top = <<<EOT
@extends('layouts.app')
@section('content')
EOT;
$bottom = <<<EOT
@endsection
EOT;
$doc = new \DOMDocument();
$container = $doc->createElement('div');
$container->setAttribute('class','container');
$row = $doc->createElement('div');
$row->setAttribute('class','row');
$heading = $doc->createElement("h1",ucfirst($table_name));
$table = $doc->createElement("table");
$table->setAttribute('class','table table-responsive');
$thead = $doc->createElement("thead");
$tr = $doc->createElement('tr');
$table->appendChild($tr);
foreach ($data as $col) {
$th = $doc->createElement('th',$col['name']);
$tr->appendChild($th);
}
$thead->appendChild($tr);
$singular = str_singular($table_name);
$tbody = $doc->createElement('tbody',"foreach(\$$table_name as \$$singular)");
$tbodytr = $doc->createElement('tr');
foreach ($data as $col) {
$tbodytd = $doc->createElement('td',"{{ \$$singular" . '["' . $col['name'] . '"] }}');
$tbodytr->appendChild($tbodytd);
}
$tbody->appendChild($tbodytr);
$table->appendChild($thead);
$table->appendChild($tbody);
$col = $doc->createElement('div');
$col->setAttribute('class','col-md-12');
$col->appendChild($heading);
$col->appendChild($table);
$row->appendChild($col);
$container->appendChild($row);
$doc->appendChild($container);
$raw = $doc->saveHTML();
$lessthan = preg_replace("/&lt;/","<",$raw);
$output = preg_replace("/&gt;/",">",$lessthan);
return $top . $output . $bottom;
}
public function create_show($table_name,$data) {
$top = <<<EOT
@extends('layouts.app')
@section('content')
EOT;
$bottom = <<<EOT
@endsection
EOT;
//simply show a list of columns with data next to it
$doc = new \DOMDocument();
$title = ucfirst(str_singular($table_name)) . " Details";
$container = $doc->createElement('div');
$row = $doc->createElement('div');
$col = $doc->createElement('div');
$title = $doc->createElement('h1',$title);
$col->appendChild($title);
$container->setAttribute('class','container');
$row->setAttribute('class','row');
$col->setAttribute('class','col-md-12');
foreach ($data as $column) {
$h1 = $doc->createElement('h1',ucfirst($column['name']));
$title = $column['name'];
$singular = str_singular($table_name);
$h3 = $doc->createElement('h3',"{{ \$$singular" . "['" . $column['name'] . "']" . " }}");
$col->appendChild($h1);
$col->appendChild($h3);
}
$row->appendChild($col);
$container->appendChild($row);
$doc->appendChild($container);
return $top . $doc->saveHTML() . $bottom;
}
public function create_edit($table_name,$data) {
//skip datetime and timestamp types
$top = <<<EOT
@extends('layouts.app')
@section('content')
EOT;
$bottom = <<<EOT
@endsection
EOT;
$doc = new \DOMDocument();
$container = $doc->createElement('div');
$container->setAttribute('class','container');
$row = $doc->createElement('div');
$row->setAttribute('class','row');
$title = $doc->createElement('h1','Edit ' . str_singular($table_name));
$form = $doc->createElement('form',"{{ csrf_field() }} {{ method_put() }}");
$form->setAttribute('method','POST');
$form->setAttribute('action',"/" . $table_name . "/update");
foreach($data as $col) {
//create input divs based on
$fg = $doc->createElement('div');
$fg->setAttribute('class','form-group');
$label = $doc->createElement('label',ucfirst($col['name']));
$input = $doc->createElement('input');
switch($col['type']) {
case "String":
$input->setAttribute('type','text');
break;
case "Date":
$input->setAttribute('type','date');
break;
case "Integer":
$input->setAttribute('type','number');
break;
default:
$input->setAttribute('type','text');
break;
}
$input->setAttribute('class','form-control');
$input->setAttribute('name',$col['name']);
$filtered = str_singular($table_name);
$input->setAttribute('value',"{{ \$$filtered" . "['" . $col['name'] . "']" . " }}");
$fg->appendChild($label);
$fg->appendChild($input);
$form->appendChild($fg);
}
$submit = $doc->createElement('button');
$submit->setAttribute('type','submit');
$submit->setAttribute('class','btn btn-success');
$form->appendChild($submit);
$col = $doc->createElement('div');
$col->setAttribute('class','col-md-12');
$col->appendChild($title);
$col->appendChild($form);
$row->appendChild($col);
$container->appendChild($row);
$doc->appendChild($container);
return $top . $doc->saveHTML() . $bottom;
}
public function create_create($table_name,$data) {
$top = <<<EOT
@extends('layouts.app')
@section('content')
EOT;
$bottom = <<<EOT
@endsection
EOT;
$doc = new \DOMDocument();
$container = $doc->createElement('div');
$container->setAttribute('class','container');
$row = $doc->createElement('div');
$row->setAttribute('class','row');
$title = $doc->createElement('h1','Edit ' . str_singular($table_name));
$form = $doc->createElement('form',"{{ csrf_field() }}");
$form->setAttribute('method','POST');
$form->setAttribute('action',"/" . $table_name);
foreach($data as $col) {
//create input divs based on
$fg = $doc->createElement('div');
$fg->setAttribute('class','form-group');
$label = $doc->createElement('label',ucfirst($col['name']));
$input = $doc->createElement('input');
switch($col['type']) {
case "String":
$input->setAttribute('type','text');
break;
case "Date":
$input->setAttribute('type','date');
break;
case "Integer":
$input->setAttribute('type','number');
break;
default:
$input->setAttribute('type','text');
break;
}
$input->setAttribute('class','form-control');
$input->setAttribute('name',$col['name']);
$filtered = str_singular($table_name);
$fg->appendChild($label);
$fg->appendChild($input);
$form->appendChild($fg);
}
$submit = $doc->createElement('button');
$submit->setAttribute('type','submit');
$submit->setAttribute('class','btn btn-success');
$form->appendChild($submit);
$col = $doc->createElement('div');
$col->setAttribute('class','col-md-12');
$col->appendChild($title);
$col->appendChild($form);
$row->appendChild($col);
$container->appendChild($row);
$doc->appendChild($container);
return $top . $doc->saveHTML() . $bottom;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment