Jelajahi Sumber

Generate controllers from tree

Vijayakrishnan Krishnan 5 tahun lalu
induk
melakukan
f3e26597b7

+ 56 - 17
app/Console/Commands/GenerateTreeCommand.php

@@ -58,6 +58,13 @@ class GenerateTreeCommand extends Command
                 $ls = $this->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
@@ -70,6 +77,7 @@ class GenerateTreeCommand extends Command
                                 // 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";
                                 }
@@ -81,6 +89,7 @@ class GenerateTreeCommand extends Command
                                 $currentController->save();
                             }
                             $currentController = new GenController($currentRoot, $line);
+                            $currentController->dbTable = $dbTable;
                             $currentController->addMethod("index", "/$line");
                             if(!empty($currentSubController)) {
                                 $currentSubController->save();
@@ -113,7 +122,6 @@ class GenerateTreeCommand extends Command
 
                 }
 
-
             }
         }
 
@@ -125,6 +133,8 @@ class GenerateTreeCommand extends Command
             $currentController->save();
         }
 
+        echo "Saved " . base_path("routes/generated.php") . "\n";
+
     }
 
     private function numLS($line) {
@@ -148,6 +158,8 @@ class GenController {
     public $name;
     public $methods;
     public $parentRoute = "";
+    public $single = false;
+    public $dbTable = null;
     public function __construct($root = null, $name = null)
     {
         $this->root = $root;
@@ -164,29 +176,56 @@ class GenController {
     }
     public function save() {
         if(!$this->saved && !empty($this->root) && !empty($this->name)) {
-            // todo: save controller
-            // todo: save views
-
+            $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' : '') . ') {';
 
-            // save routes
-            $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}');";
+            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'));";
+                }
             }
-            $lines[] = '';
-            $lines[] = '';
-            file_put_contents(base_path("routes/generated.php"), implode("\n", $lines), FILE_APPEND);
-            // echo "Saved " . base_path("routes/generated.php") . "\n";
 
-            $this->saved = true;
-//            $this->log();
+            $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);
@@ -215,7 +254,7 @@ class GenControllerMethod {
     {
         $this->name = $name;
         $this->route = $route;
-        if(strpos($this->route, ":uid") !== FALSE) {
+        if(strpos($this->route, "{uid}") !== FALSE) {
             $this->hasUID = true;
         }
     }

+ 25 - 0
generatecv/tree-lite.txt

@@ -0,0 +1,25 @@
+PRO
+    my_teams|team
+    my_teams/add_new
+    my_teams/view/{uid}|team
+        ACTIONS
+            updateTeamNumber
+        SUB
+            dashboard
+            clients
+            audit_log
+    my_clients|client
+    my_clients/add_new
+    my_clients/view/{uid}|client
+        ACTIONS
+            sendCellNumberConfirmationMessage
+            confirmCellNumberWithConfirmationToken
+            putNewCellNumber
+            sendEmailAddressConfirmationMessage
+            confirmEmailAddressWithConfirmationToken
+            putNewEmailAddress
+        SUB
+            dashboard
+            med_profile
+            med_profile_log
+            pro_access

+ 11 - 0
generatecv/tree-templates/controller.template.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\DB;
+
+class _NAME_ extends Controller
+{
+// __METHODS__
+}