|
3 | 3 | namespace Leaf\Queue\Commands; |
4 | 4 |
|
5 | 5 | use Aloe\Command; |
| 6 | +use Illuminate\Support\Str; |
6 | 7 |
|
7 | 8 | class GenerateJobCommand extends Command |
8 | 9 | { |
9 | 10 | protected static $defaultName = 'g:job'; |
10 | | - public $description = 'Generate a job class'; |
11 | | - public $help = 'Generate a job class'; |
| 11 | + public $description = 'Create a job class'; |
| 12 | + public $help = 'Generate a new job class'; |
12 | 13 |
|
13 | 14 | protected function config() |
14 | 15 | { |
15 | | - $this->setArgument('job', 'required'); |
| 16 | + $this->setArgument('job', 'required', 'job name'); |
16 | 17 | } |
17 | 18 |
|
18 | 19 | protected function handle() |
19 | 20 | { |
20 | | - $job = $this->argument('config'); |
| 21 | + $job = Str::studly(Str::singular($this->argument('job'))); |
21 | 22 |
|
22 | | - $this->writeln("Generating job $job"); |
| 23 | + if (!strpos($job, 'Job')) { |
| 24 | + $job .= 'Job'; |
| 25 | + } |
| 26 | + |
| 27 | + $file = \Aloe\Command\Config::rootpath(AppPaths('jobs') . "/$job.php"); |
| 28 | + |
| 29 | + if (file_exists($file)) { |
| 30 | + $this->error("$job already exists"); |
| 31 | + return 1; |
| 32 | + } |
| 33 | + |
| 34 | + touch($file); |
| 35 | + |
| 36 | + $fileContent = \file_get_contents(__DIR__ . '/stubs/job.stub'); |
| 37 | + $fileContent = str_replace('ClassName', $job, $fileContent); |
| 38 | + |
| 39 | + file_put_contents($file, $fileContent); |
| 40 | + |
| 41 | + $this->comment("$job generated successfully"); |
| 42 | + return 0; |
23 | 43 | } |
24 | 44 | } |
0 commit comments