GenerateVCommand.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. class GenerateVCommand extends Command
  5. {
  6. /**
  7. * The name and signature of the console command.
  8. *
  9. * @var string
  10. */
  11. protected $signature = 'generatev
  12. {path: /path/to/views.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. $views = json_decode(file_get_contents($argv[2]));
  38. $routes = ['/* __SCAFFOLD_VIEW_ROUTES__ */'];
  39. foreach ($views as $view) {
  40. $template = file_get_contents(base_path('generatecv/form.template.blade.php'));
  41. $text = str_replace("_TITLE_", $view->title, $template);
  42. $text = str_replace("_ACTION_", $view->action, $text);
  43. // fields
  44. /*
  45. <div class="form-group">
  46. <label class="control-label">cellNumber</label>
  47. <input type=cellNumber"text" class="form-control">
  48. </div>
  49. */
  50. $fields = [];
  51. foreach ($view->fields as $name => $field) {
  52. $html = "<div class='form-group'>\n" .
  53. "<label class='control-label'>{$field->name}</label>\n";
  54. if(!isset($field->type)) {
  55. $field->type = "text";
  56. }
  57. switch($field->type) {
  58. case "switch":
  59. // todo
  60. break;
  61. default:
  62. $html .= "<input type='{$field->type}' name='$name' class='form-control'>\n";
  63. break;
  64. }
  65. $html .= "</div>\n";
  66. $fields[] = $html;
  67. }
  68. $text = str_replace("_FIELDS_", implode("\n", $fields), $text);
  69. $this->file_force_contents(resource_path("views/{$view->output}.blade.php"), $text);
  70. echo "Generated " . resource_path("views/{$view->output}.blade.php") . "\n";
  71. $routes[] = "/* SCAFVR */Route::get('{$view->output}', function () { return view('{$view->output}'); });";
  72. }
  73. // write routes
  74. $text = [];
  75. $file = fopen(base_path("routes/web.php"), "r");
  76. while (!feof($file)) {
  77. $line = rtrim(fgets($file));
  78. if(strpos($line, "/* SCAFVR */") === FALSE) {
  79. $text[] = $line;
  80. }
  81. }
  82. $text = implode("\n", $text);
  83. fclose($file);
  84. $text = str_replace("/* __SCAFFOLD_VIEW_ROUTES__ */", implode("\n", $routes), $text);
  85. file_put_contents(base_path("routes/web.php"), $text);
  86. echo "Updated " . base_path("routes/web.php") . "\n";
  87. }
  88. private function file_force_contents($dir, $contents){
  89. $parts = explode('/', $dir);
  90. $file = array_pop($parts);
  91. $dir = '';
  92. foreach($parts as $part)
  93. if(!is_dir($dir .= "/$part")) mkdir($dir);
  94. file_put_contents("$dir/$file", $contents);
  95. }
  96. }