瀏覽代碼

Generate listing views from tree

Vijayakrishnan Krishnan 5 年之前
父節點
當前提交
b56dfd0f35
共有 2 個文件被更改,包括 104 次插入6 次删除
  1. 73 6
      app/Console/Commands/GenerateTreeCommand.php
  2. 31 0
      generatecv/tree-templates/index.template.blade.php

+ 73 - 6
app/Console/Commands/GenerateTreeCommand.php

@@ -3,6 +3,7 @@
 namespace App\Console\Commands;
 
 use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
 
 class GenerateTreeCommand extends Command
 {
@@ -79,6 +80,7 @@ class GenerateTreeCommand extends Command
                                     $currentSubController = new GenController($currentRoot, $currentController->name . "_SINGLE");
                                     $currentSubController->dbTable = $currentController->dbTable;
                                     $currentSubController->parentRoute = "/" . $line;
+                                    $currentSubController->sub = true;
                                     $newMethod->redirect = "/" . $line . "/SUB_dashboard";
                                 }
                             }
@@ -146,10 +148,6 @@ class GenerateTreeCommand extends Command
         return $count;
     }
 
-    private function initRoutesFile() {
-
-    }
-
 }
 
 class GenController {
@@ -158,8 +156,8 @@ class GenController {
     public $name;
     public $methods;
     public $parentRoute = "";
-    public $single = false;
     public $dbTable = null;
+    public $sub = false;
     public function __construct($root = null, $name = null)
     {
         $this->root = $root;
@@ -179,7 +177,7 @@ class GenController {
             $this->saveController();
             $this->saveRoutes();
             $this->saved = true;
-            $this->log();
+//            $this->log();
         }
     }
     public function saveController() {
@@ -202,6 +200,7 @@ class GenController {
                     $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}";
@@ -210,6 +209,58 @@ class GenController {
         $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_", $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[] = "<th>{$this->snakeToTitleCase($column)}</th>";
+            $tds[] = "<td><?= \$record->$column ?></td>";
+        }
+        $text = str_replace("<!-- __SCAFFOLD_THS__ -->", implode("\n", $ths), $text);
+        $text = str_replace("<!-- __SCAFFOLD_TDS__ -->", 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)
+    {
+
+    }
+    public function saveSubView(GenController $controller, GenControllerMethod $method)
+    {
+
+    }
+    public function saveAddNewView(GenController $controller, GenControllerMethod $method)
+    {
+
     }
     public function saveRoutes() {
         $lines = ["// --- {$this->root}: {$this->name} --- //"];
@@ -243,6 +294,22 @@ class GenController {
         for($i=0; $i<$level; $i++) echo "\t";
         echo "$line\n";
     }
+    private function snakeToTitleCase($text) {
+        return ucwords(str_replace("_", " ", $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 {

+ 31 - 0
generatecv/tree-templates/index.template.blade.php

@@ -0,0 +1,31 @@
+@extends('layouts.pro-logged-in')
+@section('content')
+
+    <div class="d-flex mb-2">
+        <div><i class="fa fa-users" aria-hidden="true"></i> _NAME_ List</div>
+        <div class="ml-auto">
+            <a class="btn btn-primary btn-sm" href="{{route('_ADD_NEW_ROUTE_')}}">
+                <i class="fa fa-plus-circle" aria-hidden="true"></i>
+                Add New
+            </a>
+        </div>
+    </div>
+
+    <div class="table-responsive p-0">
+        <table class="table table-hover text-nowrap">
+            <thead>
+            <tr>
+<!-- __SCAFFOLD_THS__ -->
+            </tr>
+            </thead>
+            <tbody>
+            @foreach($records as $record)
+                <tr>
+<!-- __SCAFFOLD_TDS__ -->
+                </tr>
+            @endforeach
+            </tbody>
+        </table>
+    </div>
+
+@endsection