|
| 1 | +<?php namespace Mrabbani\ModuleManager\Console\Generators; |
| 2 | + |
| 3 | +class MakeSeeder extends AbstractGenerator |
| 4 | +{ |
| 5 | + /** |
| 6 | + * The name and signature of the console command. |
| 7 | + * |
| 8 | + * @var string |
| 9 | + */ |
| 10 | + protected $signature = 'module:make:seeder |
| 11 | + {alias : The alias of the module} |
| 12 | + {name : Seeder name}'; |
| 13 | + |
| 14 | + /** |
| 15 | + * The type of class being generated. |
| 16 | + * |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + protected $type = 'Seeder'; |
| 20 | + |
| 21 | + /** |
| 22 | + * Get the stub file for the generator. |
| 23 | + * |
| 24 | + * @return string |
| 25 | + */ |
| 26 | + protected function getStub() |
| 27 | + { |
| 28 | + |
| 29 | + return __DIR__ . '/../../../resources/stubs/seeds/seeder.stub'; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Execute the console command. |
| 34 | + * |
| 35 | + * @return bool|null |
| 36 | + */ |
| 37 | + public function handle() |
| 38 | + { |
| 39 | + $this->createModuleTableSeederIfNotExist(); |
| 40 | + $nameInput = $this->getNameInput(); |
| 41 | + |
| 42 | + if ($this->alreadyExists($nameInput)) { |
| 43 | + $this->error($this->type . ' already exists!'); |
| 44 | + |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + $this->createSeederClass($nameInput); |
| 49 | + |
| 50 | + $this->info($this->type . ' created successfully.'); |
| 51 | + } |
| 52 | + |
| 53 | + protected function createModuleTableSeederIfNotExist() |
| 54 | + { |
| 55 | + $moduleSeederClass = studly_case(preg_replace('/\-/', '_', $this->argument('alias'))) . 'TableSeeder'; |
| 56 | + |
| 57 | + if ($this->alreadyExists($moduleSeederClass)) { |
| 58 | + |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + $this->createSeederClass($moduleSeederClass); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + protected function createSeederClass($nameInput) |
| 67 | + { |
| 68 | + |
| 69 | + $name = $this->parseName($nameInput); |
| 70 | + |
| 71 | + $path = $this->getPath($name); |
| 72 | + |
| 73 | + $this->makeDirectory($path); |
| 74 | + |
| 75 | + $this->files->put($path, $this->buildClass($name)); |
| 76 | + |
| 77 | + } |
| 78 | + /** |
| 79 | + * Get the destination class path. |
| 80 | + * |
| 81 | + * @param string $name |
| 82 | + * @return string |
| 83 | + */ |
| 84 | + protected function getPath($name) |
| 85 | + { |
| 86 | + $path = $this->getModuleInfo('module-path') . 'database/seeds/' . str_replace('\\', '/', $name) . '.php'; |
| 87 | + |
| 88 | + return $path; |
| 89 | + } |
| 90 | + |
| 91 | + protected function replaceParameters(&$stub) |
| 92 | + { |
| 93 | + $stub = str_replace([ |
| 94 | + '{alias}', |
| 95 | + ], [ |
| 96 | + $this->argument('alias'), |
| 97 | + ], $stub); |
| 98 | + } |
| 99 | +} |
0 commit comments