123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Filesystem\Filesystem;
- class LaraStructure extends Command
- {
-
- protected $signature = 'make:structure {model} {--m|migration}';
-
- protected $description = 'This command generate model and a base repository out of the box.';
-
- protected $filesystem;
-
- protected $folder;
-
- public function __construct(Filesystem $filesystem)
- {
- parent::__construct();
- $this->filesystem = $filesystem;
- }
-
- public function handle()
- {
-
- $this->model = ucfirst($this->argument('model'));
-
- $pluralModel = str_plural($this->model);
-
- if ( $this->filesystem->exists(app_path("Shop/{$pluralModel}/{$this->model}.php")) ){
- return $this->error("The given model already exists!");
- }
-
- $this->createFolders('Shop');
- $this->createFile(
- app_path('Console/Stubs/DummyRepository.stub'),
- app_path("Shop/{$pluralModel}/Repositories/{$this->model}Repository.php")
- );
- $this->createFile(
- app_path('Console/Stubs/DummyRepositoryInterface.stub'),
- app_path("Shop/{$pluralModel}/Repositories/Interfaces/{$this->model}RepositoryInterface.php")
- );
- $this->info('File structure for ' . $this->model . ' created.');
-
-
- $this->call('make:model', [
- 'name' => 'Shop/' . $pluralModel . '/' .$this->model,
- '--migration' => $this->option('migration'),
- ]);
- }
-
- protected function createFile($dummySource, $destinationPath)
- {
- $pluralModel = str_plural($this->model);
- $dummyRepository = $this->filesystem->get($dummySource);
- $repositoryContent = str_replace(['Dummy', 'Dummies'], [$this->model, $pluralModel], $dummyRepository);
- $this->filesystem->put($dummySource, $repositoryContent);
- $this->filesystem->copy($dummySource, $destinationPath);
- $this->filesystem->put($dummySource, $dummyRepository);
- }
-
- protected function createFolders($baseFolder)
- {
-
- $pluralModel = str_plural($this->model);
-
- $this->filesystem->makeDirectory(app_path($baseFolder."/{$pluralModel}"));
-
- $this->filesystem->makeDirectory(app_path($baseFolder."/{$pluralModel}/Requests"));
-
- $this->filesystem->makeDirectory(app_path($baseFolder."/{$pluralModel}/Repositories/"));
-
- $this->filesystem->makeDirectory(app_path($baseFolder."/{$pluralModel}/Repositories/Interfaces"));
- }
- }
|