numLS($line);
$line = trim($line);
$tokens = explode("|", $line);
$line = $tokens[0];
$dbTable = null;
if(count($tokens) === 2) {
$dbTable = $tokens[1];
}
switch($ls) {
case 4: // top level controller OR top level controller action
// top level controller-action
if(strpos($line, "/") !== FALSE) {
if(!empty($currentController)) {
$method = explode("/", $line)[1];
$newMethod = $currentController->addMethod($method, "/" . $line);
// create _SINGLE_ controller if view
if($method === "view") {
$currentSubController = new GenController($currentRoot, $currentController->name . "_SINGLE");
$currentSubController->dbTable = $currentController->dbTable;
$currentSubController->parentRoute = "/" . $line;
$currentSubController->parentControllerName = $currentController->name;
$currentSubController->sub = true;
$newMethod->redirect = "/" . $line . "/SUB_dashboard";
}
}
}
// new top level controller
else if(empty($currentController) || $line !== $currentController->name) {
if(!empty($currentController)) {
$currentController->save();
}
$currentController = new GenController($currentRoot, $line);
$currentController->dbTable = $dbTable;
$currentController->addMethod("index", "/$line");
if(!empty($currentSubController)) {
$currentSubController->save();
}
$currentSubType = '';
$currentSubController = new GenController();
$sideLinks[] = "
route()->getController()->selfName) && strpos(request()->route()->getController()->selfName, '{$currentController->name}') === 0 ? 'active' : '') }}" . " '>" .
"" .
"" . $currentController->snakeToTitleCase($currentController->name) . "
" .
"";
}
break;
case 8: // sub-type declaration
$currentSubType = $line;
break;
case 12: // subs and actions
if($currentSubType === 'ACTIONS') {
$currentSubController->addMethod(
"ACTION_" . $line,
"/ACTION_" . $line
);
}
else if($currentSubType === 'SUB') {
$currentSubController->addMethod(
"SUB_" . $line,
"/SUB_" . $line
);
}
break;
}
}
}
// do any pending saves
if(!empty($currentSubController)) {
$currentSubController->save();
}
if(!empty($currentController)) {
$currentController->save();
}
echo "Saved " . base_path("routes/generated.php") . "\n";
// save side links
file_put_contents(resource_path("views/layouts/generated-links.blade.php"), implode("\n", $sideLinks));
echo "Saved " . resource_path("views/layouts/generated-links.blade.php") . "\n";
}
private function numLS($line) {
$count = 0;
for ($i=0; $iroot = $root;
$this->name = $name;
$this->methods = [];
}
public function addMethod($method, $route) {
if($this->parentRoute) {
$route = $this->parentRoute . $route;
}
$method = new GenControllerMethod($method, $route);
$this->methods[] = $method;
return $method;
}
public function save() {
if(!$this->saved && !empty($this->root) && !empty($this->name)) {
$this->saveController();
$this->saveRoutes();
$this->saved = true;
// $this->log();
}
}
public function saveController() {
$text = file_get_contents(base_path('generatecv/tree-templates/controller.template.php'));
$code = [];
foreach ($this->methods as $method) {
$code[] = "";
$code[] = "\t// GET {$method->route}";
$code[] = "\t" . 'public function ' . $method->name . '(Request $request' . ($method->hasUID ? ', $uid' : '') . ') {';
if($method->redirect) {
$target = str_replace('{uid}', '$uid', $method->redirect);
$code[] = "\t\t" . 'return redirect("' . $target . '");';
}
else {
if($method->hasUID) {
$code[] = "\t\t\$record = DB::table('{$this->dbTable}')->where('uid', \$uid)->first();";
$code[] = "\t\treturn view('{$this->root}/{$this->name}/{$method->name}', compact('record'));";
}
else {
$code[] = "\t\t\$records = DB::table('{$this->dbTable}')->get();";
$code[] = "\t\treturn view('{$this->root}/{$this->name}/{$method->name}', compact('records'));";
}
}
$this->saveView($this, $method);
$code[] = "\t}";
}
$text = str_replace("_NAME_", "{$this->name}_Controller", $text);
$text = str_replace("// __METHODS__", implode("\n", $code), $text);
file_put_contents(app_path("Http/Controllers/{$this->name}_Controller.php"), $text);
echo "Generated " . app_path("Http/Controllers/{$this->name}_Controller.php") . "\n";
}
public function saveView(GenController $controller, GenControllerMethod $method) {
/*
$x = DB::getSchemaBuilder()->getColumnListing('client');
print_r($x);
*/
if($controller->sub) {
$this->saveSubView($controller, $method);
}
else {
if($method->name === 'view') {
$this->saveShowView($controller, $method);
}
else if($method->name === 'index') {
$this->saveIndexView($controller, $method);
}
else if($method->name === 'add_new') {
$this->saveAddNewView($controller, $method);
}
}
}
public function saveIndexView(GenController $controller, GenControllerMethod $method)
{
$text = file_get_contents(base_path('generatecv/tree-templates/index.template.blade.php'));
$text = str_replace("_NAME_", $this->snakeToTitleCase($controller->name), $text);
$text = str_replace("_ADD_NEW_ROUTE_", $controller->name . '-add_new', $text);
$columns = DB::getSchemaBuilder()->getColumnListing($controller->dbTable);
$ths = [];
$tds = [];
foreach ($columns as $column) {
$ths[] = "{$this->snakeToTitleCase($column)} | ";
$tds[] = "" .
($column === 'uid' ? '' : '') .
"= \$record->$column ?>" .
($column === 'uid' ? '' : '') .
" | ";
}
$text = str_replace("", implode("\n", $ths), $text);
$text = str_replace("", implode("\n", $tds), $text);
$this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
}
public function saveShowView(GenController $controller, GenControllerMethod $method)
{
// delete sub links and action links
if(file_exists(resource_path("views/{$controller->root}/{$controller->parentControllerName}/subs.blade.php"))) {
unlink(resource_path("views/{$controller->root}/{$controller->parentControllerName}/subs.blade.php"));
}
if(file_exists(resource_path("views/{$controller->root}/{$controller->parentControllerName}/actions.blade.php"))) {
unlink(resource_path("views/{$controller->root}/{$controller->parentControllerName}/actions.blade.php"));
}
$text = file_get_contents(base_path('generatecv/tree-templates/show.template.blade.php'));
$text = str_replace("_NAME_", $this->snakeToTitleCase($controller->name), $text);
$text = str_replace("_UID_", '= $record->uid ?>', $text);
$text = str_replace("_INDEX_ROUTE_", $controller->name . '-index', $text);
$text = str_replace("_SUB_LINKS_VIEW_", "{$controller->root}/{$controller->name}/subs", $text);
$this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
}
public function saveSubView(GenController $controller, GenControllerMethod $method)
{
// generate sub links if not existing
$subLinksView = resource_path("views/{$controller->root}/{$controller->parentControllerName}/subs.blade.php");
//if(!file_exists($subLinksView)) {
$subLinks = [];
foreach ($controller->methods as $meth) {
echo $meth->name . "\n";
if(strpos($meth->name, "SUB_") !== 0) continue;
$display = $this->snakeToTitleCase(substr($meth->name, 4));
$subLinks[] = "parentControllerName}/view/= \$record->uid ?>/{$meth->name}' " .
"class='d-block p-3 py-2 border-bottom'>$display";
}
file_put_contents($subLinksView, implode("\n", $subLinks));
echo "Generated " . $subLinksView . "\n";
//}
// generate action links if not existing
$actionLinksView = resource_path("views/{$controller->root}/{$controller->parentControllerName}/actions.blade.php");
//if(!file_exists($actionLinksView)) {
$actionLinks = [];
foreach ($controller->methods as $meth) {
echo $meth->name . "\n";
if(strpos($meth->name, "ACTION_") !== 0) continue;
$display = $this->camelToTitleCase(substr($meth->name, 7));
$actionLinks[] = "parentControllerName}/view/= \$record->uid ?>/{$meth->name}' " .
"class='d-block btn btn-sm btn-default m-3'>$display";
}
file_put_contents($actionLinksView, implode("\n", $actionLinks));
echo "Generated " . $actionLinksView . "\n";
//}
$text = file_get_contents(base_path('generatecv/tree-templates/sub.template.blade.php'));
$text = str_replace("_LAYOUT_", "{$controller->root}.{$controller->parentControllerName}.view", $text);
$text = $this->generateSubContent($controller, $method, $text);
$this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
}
public function generateSubContent(GenController $controller, GenControllerMethod $method, $text) {
if($method->name === 'SUB_dashboard') {
$html = file_get_contents(base_path('generatecv/tree-templates/dashboard.template.blade.php'));
$text = str_replace("_SUB_VIEW_", $html, $text);
$text = str_replace("_ACTION_LINKS_VIEW_", "{$controller->root}/{$controller->parentControllerName}/actions", $text);
}
else {
$text = str_replace("_SUB_VIEW_", "Content from
{$controller->root}/{$controller->name}/{$method->name}.blade.php", $text);
}
return $text;
}
public function saveAddNewView(GenController $controller, GenControllerMethod $method)
{
}
public function saveRoutes() {
$lines = ["// --- {$this->root}: {$this->name} --- //"];
foreach ($this->methods as $method) {
// FORMAT:
// Route::get('/foo/bar/{uid}', 'FooController@bar')->name('foo-action');
$lines[] = "Route::get('{$method->route}', '{$this->name}_Controller@{$method->name}')->name('{$this->name}-{$method->name}');";
}
$lines[] = '';
$lines[] = '';
file_put_contents(base_path("routes/generated.php"), implode("\n", $lines), FILE_APPEND);
}
public function log() {
$this->w('');
$this->w("Controller: app/Http/Controllers/{$this->name}_Controller");
$this->w("Table: {$this->dbTable}");
$this->w('---------------------------------------------');
foreach ($this->methods as $method) {
$this->w('Rout: ' . $method->route, 1);
$this->w('Meth: ' . $method->name . '($request' . ($method->hasUID ? ', $uid' : '') . ')', 1);
if(!$method->redirect) {
$this->w('View: ' . resource_path("views/{$this->root}/{$this->name}/{$method->name}.blade.php"), 1);
}
else {
$this->w('Redi: ' . $method->redirect, 1);
}
$this->w('---------------------------------------------');
}
}
public function w($line, $level = -1) {
for($i=0; $i<$level; $i++) echo "\t";
echo "$line\n";
}
public function snakeToTitleCase($text) {
return ucwords(str_replace("_", " ", $text));
}
public function camelToTitleCase($text) {
$text = preg_replace("/([A-Z])/", " $1", $text);
return ucwords($text);
}
private function file_force_contents($dir, $contents){
$dir = str_replace("\\", "/", $dir);
$dir = str_replace( '//', '/', $dir);
$parts = explode('/', $dir);
$file = array_pop($parts);
$dir = '';
foreach($parts as $part) {
if($part[strlen($part) - 1] !== ':') {
if(!is_dir($dir .= "/$part")) mkdir($dir);
}
}
file_put_contents("$dir/$file", $contents);
}
}
class GenControllerMethod {
public $name;
public $route;
public $hasUID = false;
public $redirect = false;
public function __construct($name, $route)
{
$this->name = $name;
$this->route = $route;
if(strpos($this->route, "{uid}") !== FALSE) {
$this->hasUID = true;
}
}
}