GenerateCCommand.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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')->name('{$object->slug}-index');";
  58. $routesText[] = "/* SCAF */Route::get ('/$object->slug/{id}', '{$name}Controller@show')->name('{$object->slug}-view');";
  59. $routesText[] = "/* SCAF */Route::get ('/$object->slug/create', '{$name}Controller@create')->name('{$object->slug}-create');";
  60. $routesText[] = "/* SCAF */Route::get ('/$object->slug/update/{id}', '{$name}Controller@update')->name('{$object->slug}-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 = file_get_contents(base_path('generatecv/controller.template.php'));
  66. $text = str_replace("_NAME_", $name, $template);
  67. $text = str_replace("_MODEL_", $object->model, $text);
  68. $text = str_replace("_SLUG_", $object->slug, $text);
  69. // if(!file_exists(app_path("Http/Controllers/{$name}Controller.php"))) {
  70. file_put_contents(app_path("Http/Controllers/{$name}Controller.php"), $text);
  71. echo "Generated " . app_path("Http/Controllers/{$name}Controller.php") . "\n";
  72. // }
  73. // views - index
  74. if(isset($object->list)) {
  75. $template = file_get_contents(base_path('generatecv/listing.template.blade.php'));
  76. $text = str_replace("_NAME_", $object->text, $template);
  77. $text = str_replace("_SLUG_", $object->slug, $text);
  78. $ths = [];
  79. foreach ($object->list as $field => $display) {
  80. $ths[] = "<th>{$display}</th>";
  81. }
  82. $ths = implode("\n", $ths);
  83. $text = str_replace("<!-- __SCAFFOLD_THS__ -->", $ths, $text);
  84. $tds = [];
  85. foreach ($object->list as $field => $display) {
  86. if($field === 'id') {
  87. $tds[] = '<td><a href="/' . $object->slug . '/<?= $row->uid ?>"><?= $row->' . $field . ' ?></a></td>';
  88. }
  89. else {
  90. $tds[] = '<td><?= $row->' . $field . ' ?></td>';
  91. }
  92. }
  93. $tds = implode("\n", $tds);
  94. $text = str_replace("<!-- __SCAFFOLD_TDS__ -->", $tds, $text);
  95. // if(!file_exists(resource_path("views/{$object->slug}/index.blade.php"))) {
  96. $this->file_force_contents(resource_path("views/{$object->slug}/index.blade.php"), $text);
  97. echo "Generated " . resource_path("views/{$object->slug}/index.blade.php") . "\n";
  98. // }
  99. }
  100. if(isset($object->show)) {
  101. $template = file_get_contents(base_path('generatecv/show.template.blade.php'));
  102. $dts = [];
  103. foreach ($object->show as $field => $display) {
  104. $dts[] = "<dt class='col-md-2'>{$display}</dt>" .
  105. '<dd class="col-md-10"><?= $row->' . $field . ' ?></dd>';
  106. }
  107. $dts = implode("\n", $dts);
  108. $text = str_replace("<!-- __SCAFFOLD_DTS__ -->", $dts, $template);
  109. // if(!file_exists(resource_path("views/{$object->slug}/show.blade.php"))) {
  110. $this->file_force_contents(resource_path("views/{$object->slug}/show.blade.php"), $text);
  111. echo "Generated " . resource_path("views/{$object->slug}/show.blade.php") . "\n";
  112. // }
  113. }
  114. }
  115. // write routes
  116. $text = [];
  117. $file = fopen(base_path("routes/web.php"), "r");
  118. while (!feof($file)) {
  119. $line = rtrim(fgets($file));
  120. if(strpos($line, "/* SCAF */") === FALSE) {
  121. $text[] = $line;
  122. }
  123. }
  124. $text = implode("\n", $text);
  125. fclose($file);
  126. $text = str_replace("/* __SCAFFOLD_ROUTES__ */", implode("\n", $routesText), $text);
  127. file_put_contents(base_path("routes/web.php"), $text);
  128. echo "Updated " . base_path("routes/web.php") . "\n";
  129. // write left nav links
  130. $text = [];
  131. $file = fopen(resource_path("views/layouts/pro-logged-in.blade.php"), "r");
  132. while (!feof($file)) {
  133. $line = rtrim(fgets($file));
  134. if(strpos($line, "<!-- SCAF -->") === FALSE) {
  135. $text[] = $line;
  136. }
  137. }
  138. $text = implode("\n", $text);
  139. fclose($file);
  140. $text = str_replace("<!-- __SCAFFOLD_LINKS__ -->", implode("\n", $linksText), $text);
  141. file_put_contents(resource_path("views/layouts/pro-logged-in.blade.php"), $text);
  142. echo "Updated " . resource_path("views/layouts/pro-logged-in.blade.php") . "\n";
  143. }
  144. private function file_force_contents($dir, $contents){
  145. $parts = explode('/', $dir);
  146. $file = array_pop($parts);
  147. $dir = '';
  148. foreach($parts as $part)
  149. if(!is_dir($dir .= "/$part")) mkdir($dir);
  150. file_put_contents("$dir/$file", $contents);
  151. }
  152. }