GenerateMCommand.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. class GenerateMCommand extends Command
  5. {
  6. /**
  7. * The name and signature of the console command.
  8. *
  9. * @var string
  10. */
  11. protected $signature = 'generatem
  12. {path: /path/to/models.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. $models = json_decode(file_get_contents($argv[2]));
  38. print_r($models);
  39. $template =
  40. '<?php
  41. namespace App\Models;
  42. use Illuminate\Database\Eloquent\Model;
  43. class _NAME_ extends Model
  44. {
  45. protected $table = "_TABLE_";
  46. //
  47. }
  48. ';
  49. foreach ($models as $model) {
  50. $name = str_replace(" ", "", ucwords(str_replace("_", " ", $model)));
  51. $text = str_replace("_NAME_", $name, $template);
  52. $text = str_replace("_TABLE_", $model, $text);
  53. if(!file_exists(app_path("Models/$name.php"))) {
  54. file_put_contents(app_path("Models/$name.php"), $text);
  55. echo "Generated " . app_path("Models/$name.php") . "\n";
  56. }
  57. }
  58. }
  59. }