LaraStructure.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Filesystem\Filesystem;
  5. class LaraStructure extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'make:structure {model} {--m|migration}';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'This command generate model and a base repository out of the box.';
  19. /**
  20. * Filesystem instance
  21. *
  22. * @var string
  23. */
  24. protected $filesystem;
  25. /**
  26. * Default laracom folder
  27. *
  28. * @var string
  29. */
  30. protected $folder;
  31. /**
  32. * Create a new command instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct(Filesystem $filesystem)
  37. {
  38. parent::__construct();
  39. $this->filesystem = $filesystem;
  40. }
  41. /**
  42. * Execute the console command.
  43. *
  44. * @return mixed
  45. */
  46. public function handle()
  47. {
  48. // Get model name from developer
  49. $this->model = ucfirst($this->argument('model'));
  50. // Get plural name for the given model
  51. $pluralModel = str_plural($this->model);
  52. // Check if the model already created
  53. if ( $this->filesystem->exists(app_path("Shop/{$pluralModel}/{$this->model}.php")) ){
  54. return $this->error("The given model already exists!");
  55. }
  56. // create all structured folders
  57. $this->createFolders('Shop');
  58. $this->createFile(
  59. app_path('Console/Stubs/DummyRepository.stub'),
  60. app_path("Shop/{$pluralModel}/Repositories/{$this->model}Repository.php")
  61. );
  62. $this->createFile(
  63. app_path('Console/Stubs/DummyRepositoryInterface.stub'),
  64. app_path("Shop/{$pluralModel}/Repositories/Interfaces/{$this->model}RepositoryInterface.php")
  65. );
  66. $this->info('File structure for ' . $this->model . ' created.');
  67. // Create Model under default instalation folder
  68. $this->call('make:model', [
  69. 'name' => 'Shop/' . $pluralModel . '/' .$this->model,
  70. '--migration' => $this->option('migration'),
  71. ]);
  72. }
  73. /**
  74. * Create source from dummy model name
  75. *
  76. * @param string $dummy
  77. * @param string $destinationPath
  78. * @return void
  79. */
  80. protected function createFile($dummySource, $destinationPath)
  81. {
  82. $pluralModel = str_plural($this->model);
  83. $dummyRepository = $this->filesystem->get($dummySource);
  84. $repositoryContent = str_replace(['Dummy', 'Dummies'], [$this->model, $pluralModel], $dummyRepository);
  85. $this->filesystem->put($dummySource, $repositoryContent);
  86. $this->filesystem->copy($dummySource, $destinationPath);
  87. $this->filesystem->put($dummySource, $dummyRepository);
  88. }
  89. /**
  90. * Create all required folders
  91. *
  92. * @return void
  93. */
  94. protected function createFolders($baseFolder)
  95. {
  96. // get plural from model name
  97. $pluralModel = str_plural($this->model);
  98. // create container folder
  99. $this->filesystem->makeDirectory(app_path($baseFolder."/{$pluralModel}"));
  100. // add requests folder
  101. $this->filesystem->makeDirectory(app_path($baseFolder."/{$pluralModel}/Requests"));
  102. // add repositories folder
  103. $this->filesystem->makeDirectory(app_path($baseFolder."/{$pluralModel}/Repositories/"));
  104. // add Interfaces folder
  105. $this->filesystem->makeDirectory(app_path($baseFolder."/{$pluralModel}/Repositories/Interfaces"));
  106. }
  107. }