Procházet zdrojové kódy

Generate routes, links and controllers command

Vijayakrishnan před 5 roky
rodič
revize
c6b592e3ac

+ 161 - 0
app/Console/Commands/GenerateCCommand.php

@@ -0,0 +1,161 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+
+class GenerateCCommand extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'generatec
+                            {path: /path/to/controller.json}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        //
+
+        global $argv;
+        $controllers = json_decode(file_get_contents($argv[2]));
+
+//        Route::get('/my-payments-schedule', 'MyPaymentScheduleController@index');
+//        Route::get('/my-teams', 'MyTeamsController@index');
+//        Route::get('/my-clients', 'MyClientsController@index');
+//        Route::get('/notes', 'NotesController@index');
+//        Route::get('/erx', 'ERXController@index');
+//        Route::get('/action-items', 'ActionItemsController@index');
+//        Route::get('/care-months', 'CareMonthsController@index');
+//        Route::get('/bills', 'BillsController@index');
+//        Route::get('/transactions', 'TransactionsController@index');
+//        Route::get('/med-profile-lines', 'MedProfileLinesController@index');
+//        Route::get('/mcp-updates', 'McpUpdatesController@index');
+//        Route::get('/ally-updates', 'AllyUpdatesController@index');
+//        Route::get('/audit-log', 'AuditLogController@index');
+
+        $routesText = ["/* __SCAFFOLD_ROUTES__ */"];
+        $linksText = ["<!-- __SCAFFOLD_LINKS__ -->"];
+
+
+
+        foreach ($controllers as $name => $object) {
+
+            if(isset($object->todo)) continue;
+
+            // routes
+            $routesText[] = "/* SCAF */// $object->slug CRUD";
+            $routesText[] = "/* SCAF */Route::get ('/$object->slug', '{$name}Controller@index');";
+            $routesText[] = "/* SCAF */Route::get ('/$object->slug/:id', '{$name}Controller@show');";
+            $routesText[] = "/* SCAF */Route::get ('/$object->slug/create', '{$name}Controller@create');";
+            $routesText[] = "/* SCAF */Route::get ('/$object->slug/update/:id', '{$name}Controller@update');";
+
+            // left nav links
+            $linksText[] =
+'<!-- SCAF --><li class="nav-item"><a href="/' . $object->slug . '" class="nav-link"><i class="nav-icon ' . $object->icon . '"></i><p>' . $object->text . '</p></a></li>';
+
+            // controller
+            $template =
+'<?php
+
+namespace App\Http\Controllers;
+
+use Illuminate\Http\Request;
+use App\Models\_MODEL_;
+
+class _NAME_Controller extends Controller
+{
+
+  public function index(Request $request)
+  {
+    $rows = _MODEL_::all();
+    return view(\'_SLUG_.index\', compact(\'rows\'));
+  }
+
+  public function show(Request $request, $uid)
+  {
+    $row = _MODEL_::where(\'uid\', $uid)->first();
+    return view(\'_SLUG_.show\', compact(\'row\'));
+  }
+
+  public function create(Request $request)
+  {
+    return view(\'_SLUG_.create\');
+  }
+
+  public function update(Request $request, $uid)
+  {
+    $row = _MODEL_::where(\'uid\', $uid)->first();
+    return view(\'_SLUG_.update\', compact(\'row\'));
+  }
+
+}
+
+';
+
+            // write controller
+            $text = str_replace("_NAME_", $name, $template);
+            $text = str_replace("_MODEL_", $object->model, $text);
+            $text = str_replace("_SLUG_", $object->slug, $text);
+            if(!file_exists(app_path("Http/Controllers/{$name}Controller.php"))) {
+                file_put_contents(app_path("Http/Controllers/{$name}Controller.php"), $text);
+                echo "Generated " . app_path("Http/Controllers/{$name}Controller.php") . "\n";
+            }
+
+        }
+
+        // write routes
+        $text = [];
+        $file = fopen(base_path("routes/web.php"), "r");
+        while (!feof($file)) {
+            $line = rtrim(fgets($file));
+            if(strpos($line, "/* SCAF */") === FALSE) {
+                $text[] = $line;
+            }
+        }
+        $text = implode("\n", $text);
+        fclose($file);
+        $text = str_replace("/* __SCAFFOLD_ROUTES__ */", implode("\n", $routesText), $text);
+        file_put_contents(base_path("routes/web.php"), $text);
+        echo "Updated " . base_path("routes/web.php") . "\n";
+
+        // write left nav links
+        $text = [];
+        $file = fopen(resource_path("views/layouts/pro-logged-in.blade.php"), "r");
+        while (!feof($file)) {
+            $line = rtrim(fgets($file));
+            if(strpos($line, "<!-- SCAF -->") === FALSE) {
+                $text[] = $line;
+            }
+        }
+        $text = implode("\n", $text);
+        fclose($file);
+        $text = str_replace("<!-- __SCAFFOLD_LINKS__ -->", implode("\n", $linksText), $text);
+        file_put_contents(resource_path("views/layouts/pro-logged-in.blade.php"), $text);
+        echo "Updated " . resource_path("views/layouts/pro-logged-in.blade.php") . "\n";
+
+    }
+}

+ 1 - 0
app/Console/Kernel.php

@@ -14,6 +14,7 @@ class Kernel extends ConsoleKernel
      */
     protected $commands = [
         Commands\GenerateMCommand::class,
+        Commands\GenerateCCommand::class,
         Commands\GenerateCVCommand::class,
     ];
 

+ 32 - 0
generatecv/controllers.json

@@ -0,0 +1,32 @@
+{
+    "MyTeams": {
+        "text": "My Teams",
+        "slug": "my-teams",
+        "model": "Team",
+        "icon": "fa fa-user"
+    },
+    "MyClients": {
+        "text": "My Clients",
+        "slug": "my-clients",
+        "model": "Client",
+        "icon": "fa fa-user"
+    },
+    "Notes": {
+        "text": "Notes",
+        "slug": "notes",
+        "model": "Note",
+        "icon": "fa fa-user"
+    },
+    "ERX": { "todo": true },
+    "ActionItems": { "todo": true },
+    "CareMonths": { "todo": true },
+    "Bills": { "todo": true },
+    "Transactions": { "todo": true },
+    "MedProfileLines": { "todo": true },
+    "MedProfileLineUpdates": { "todo": true },
+    "ProAccess": { "todo": true },
+    "McpUpdates": { "todo": true },
+    "AllyUpdates": { "todo": true },
+    "AuditLog": { "todo": true },
+    "MyPaymentSchedule": { "todo": true }
+}

+ 3 - 0
resources/views/layouts/pro-logged-in.blade.php

@@ -93,6 +93,9 @@
                             </p>
                         </a>
                     </li>
+
+<!-- __SCAFFOLD_LINKS__ -->
+
                 </ul>
             </nav>
             <!-- /.sidebar-menu -->

+ 3 - 0
routes/web.php

@@ -39,4 +39,7 @@ Route::middleware('ensureValidSession')->group(function(){
     Route::get('/pro/meeting/{meetingID}', 'ProController@meeting');
 
     Route::get('/pro/logout', 'AppSessionController@processProLogOut')->name('pro-logout');
+
+/* __SCAFFOLD_ROUTES__ */
+
 });