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; $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(); } 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"; } 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) { $code[] = "\t\treturn redirect('$method->redirect');"; } 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'));"; } } $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 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"; } } 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; } } }