Selaa lähdekoodia

Support for comments, hasAdd, hasView

Vijayakrishnan Krishnan 5 vuotta sitten
vanhempi
commit
485fde4372

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

@@ -50,6 +50,10 @@ class GenerateTreeCommand extends Command
         $currentView = "";
 
         foreach ($lines as $line) {
+
+            // skip comments
+            if(trim($line)[0] === '#') continue;
+
             $lineType = null;
 
             // no leading space - root specifier
@@ -64,10 +68,13 @@ class GenerateTreeCommand extends Command
                 $tokens = explode("|", $line);
                 $line = $tokens[0];
                 $dbTable = null;
-                if(count($tokens) === 2) {
+                if(count($tokens) >= 2) {
                     $dbTable = $tokens[1];
                 }
 
+                $hasAdd = in_array("add", $tokens);
+                $hasView = in_array("view", $tokens);
+
                 switch($ls) {
 
                     case 4: // top level controller OR top level controller action
@@ -95,6 +102,8 @@ class GenerateTreeCommand extends Command
                             }
                             $currentController = new GenController($currentRoot, $line);
                             $currentController->dbTable = $dbTable;
+                            $currentController->hasAdd = $hasAdd;
+                            $currentController->hasView = $hasView;
                             $currentController->addMethod("index", "/$line");
                             if(!empty($currentSubController)) {
                                 $currentSubController->save();
@@ -171,6 +180,8 @@ class GenController {
     public $methods;
     public $parentRoute = "";
     public $dbTable = null;
+    public $hasAdd = false;
+    public $hasView = false;
     public $sub = false;
     public $parentControllerName = '';
     public function __construct($root = null, $name = null)
@@ -238,13 +249,13 @@ class GenController {
             $this->saveSubView($controller, $method);
         }
         else {
-            if($method->name === 'view') {
+            if($method->name === 'view' && $controller->hasView) {
                 $this->saveShowView($controller, $method);
             }
             else if($method->name === 'index') {
                 $this->saveIndexView($controller, $method);
             }
-            else if($method->name === 'add_new') {
+            else if($method->name === 'add_new' && $controller->hasAdd) {
                 $this->saveAddNewView($controller, $method);
             }
         }
@@ -254,16 +265,22 @@ class GenController {
     {
         $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);
+
+        if($controller->hasAdd) {
+            $addLink = "<a class='btn btn-primary btn-sm' href='/{$controller->name}/add_new'>" .
+                "<i class='fa fa-plus-circle' aria-hidden='true'></i>Add New</a>";
+            $text = str_replace("<!-- _ADD_NEW_LINK_ -->", $addLink, $text);
+        }
+
         $columns = DB::getSchemaBuilder()->getColumnListing($controller->dbTable);
         $ths = [];
         $tds = [];
         foreach ($columns as $column) {
             $ths[] = "<th>{$this->snakeToTitleCase($column)}</th>";
             $tds[] = "<td>" .
-                ($column === 'uid' ? '<a href="/' . $controller->name . '/view/<?= $record->uid ?>">' : '') .
+                ($controller->hasView && $column === 'uid' ? '<a href="/' . $controller->name . '/view/<?= $record->uid ?>">' : '') .
                 "<?= \$record->$column ?>" .
-                ($column === 'uid' ? '</a>' : '') .
+                ($controller->hasView && $column === 'uid' ? '</a>' : '') .
                 "</td>";
         }
         $text = str_replace("<!-- __SCAFFOLD_THS__ -->", implode("\n", $ths), $text);

+ 1 - 4
generatecv/tree-templates/index.template.blade.php

@@ -4,10 +4,7 @@
     <h2 class="d-flex mb-2">
         <div>_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>
+            <!-- _ADD_NEW_LINK_ -->
         </div>
     </h2>