GenerateCCommand.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. class GenerateCCommand extends Command
  5. {
  6. /**
  7. * The name and signature of the console command.
  8. *
  9. * @var string
  10. */
  11. protected $signature = 'generatec
  12. {path: /path/to/controller.json}';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Command description';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return mixed
  32. */
  33. public function handle()
  34. {
  35. //
  36. global $argv;
  37. $controllers = json_decode(file_get_contents($argv[2]));
  38. // Route::get('/my-payments-schedule', 'MyPaymentScheduleController@index');
  39. // Route::get('/my-teams', 'MyTeamsController@index');
  40. // Route::get('/my-clients', 'MyClientsController@index');
  41. // Route::get('/notes', 'NotesController@index');
  42. // Route::get('/erx', 'ERXController@index');
  43. // Route::get('/action-items', 'ActionItemsController@index');
  44. // Route::get('/care-months', 'CareMonthsController@index');
  45. // Route::get('/bills', 'BillsController@index');
  46. // Route::get('/transactions', 'TransactionsController@index');
  47. // Route::get('/med-profile-lines', 'MedProfileLinesController@index');
  48. // Route::get('/mcp-updates', 'McpUpdatesController@index');
  49. // Route::get('/ally-updates', 'AllyUpdatesController@index');
  50. // Route::get('/audit-log', 'AuditLogController@index');
  51. $routesText = ["/* __SCAFFOLD_ROUTES__ */"];
  52. $linksText = ["<!-- __SCAFFOLD_LINKS__ -->"];
  53. foreach ($controllers as $name => $object) {
  54. if(isset($object->todo)) continue;
  55. // routes
  56. $routesText[] = "/* SCAF */// $object->slug CRUD";
  57. $routesText[] = "/* SCAF */Route::get ('/$object->slug', '{$name}Controller@index');";
  58. $routesText[] = "/* SCAF */Route::get ('/$object->slug/:id', '{$name}Controller@show');";
  59. $routesText[] = "/* SCAF */Route::get ('/$object->slug/create', '{$name}Controller@create');";
  60. $routesText[] = "/* SCAF */Route::get ('/$object->slug/update/:id', '{$name}Controller@update');";
  61. // left nav links
  62. $linksText[] =
  63. '<!-- 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>';
  64. // controller
  65. $template =
  66. '<?php
  67. namespace App\Http\Controllers;
  68. use Illuminate\Http\Request;
  69. use App\Models\_MODEL_;
  70. class _NAME_Controller extends Controller
  71. {
  72. public function index(Request $request)
  73. {
  74. $rows = _MODEL_::all();
  75. return view(\'_SLUG_.index\', compact(\'rows\'));
  76. }
  77. public function show(Request $request, $uid)
  78. {
  79. $row = _MODEL_::where(\'uid\', $uid)->first();
  80. return view(\'_SLUG_.show\', compact(\'row\'));
  81. }
  82. public function create(Request $request)
  83. {
  84. return view(\'_SLUG_.create\');
  85. }
  86. public function update(Request $request, $uid)
  87. {
  88. $row = _MODEL_::where(\'uid\', $uid)->first();
  89. return view(\'_SLUG_.update\', compact(\'row\'));
  90. }
  91. }
  92. ';
  93. // write controller
  94. $text = str_replace("_NAME_", $name, $template);
  95. $text = str_replace("_MODEL_", $object->model, $text);
  96. $text = str_replace("_SLUG_", $object->slug, $text);
  97. if(!file_exists(app_path("Http/Controllers/{$name}Controller.php"))) {
  98. file_put_contents(app_path("Http/Controllers/{$name}Controller.php"), $text);
  99. echo "Generated " . app_path("Http/Controllers/{$name}Controller.php") . "\n";
  100. }
  101. }
  102. // write routes
  103. $text = [];
  104. $file = fopen(base_path("routes/web.php"), "r");
  105. while (!feof($file)) {
  106. $line = rtrim(fgets($file));
  107. if(strpos($line, "/* SCAF */") === FALSE) {
  108. $text[] = $line;
  109. }
  110. }
  111. $text = implode("\n", $text);
  112. fclose($file);
  113. $text = str_replace("/* __SCAFFOLD_ROUTES__ */", implode("\n", $routesText), $text);
  114. file_put_contents(base_path("routes/web.php"), $text);
  115. echo "Updated " . base_path("routes/web.php") . "\n";
  116. // write left nav links
  117. $text = [];
  118. $file = fopen(resource_path("views/layouts/pro-logged-in.blade.php"), "r");
  119. while (!feof($file)) {
  120. $line = rtrim(fgets($file));
  121. if(strpos($line, "<!-- SCAF -->") === FALSE) {
  122. $text[] = $line;
  123. }
  124. }
  125. $text = implode("\n", $text);
  126. fclose($file);
  127. $text = str_replace("<!-- __SCAFFOLD_LINKS__ -->", implode("\n", $linksText), $text);
  128. file_put_contents(resource_path("views/layouts/pro-logged-in.blade.php"), $text);
  129. echo "Updated " . resource_path("views/layouts/pro-logged-in.blade.php") . "\n";
  130. }
  131. }