|
8 | 8 |
|
9 | 9 | class AddCustomRouteContent extends Command |
10 | 10 | { |
| 11 | + use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput; |
| 12 | + |
11 | 13 | /** |
12 | 14 | * The name and signature of the console command. |
13 | 15 | * |
@@ -45,32 +47,40 @@ public function handle() |
45 | 47 | $disk = Storage::disk($disk_name); |
46 | 48 | $code = $this->argument('code'); |
47 | 49 |
|
48 | | - if ($disk->exists($path)) { |
49 | | - $old_file_path = $disk->path($path); |
| 50 | + $this->progressBlock("Adding route to $path"); |
50 | 51 |
|
51 | | - // insert the given code before the file's last line |
52 | | - $file_lines = file($old_file_path, FILE_IGNORE_NEW_LINES); |
| 52 | + // Validate file exists |
| 53 | + if (! $disk->exists($path)) { |
| 54 | + Artisan::call('vendor:publish', ['--provider' => \Backpack\CRUD\BackpackServiceProvider::class, '--tag' => 'custom_routes']); |
| 55 | + $this->handle(); |
53 | 56 |
|
54 | | - // if the code already exists in the file, abort |
55 | | - if ($this->getLastLineNumberThatContains($code, $file_lines)) { |
56 | | - return $this->comment('Route already existed.'); |
57 | | - } |
| 57 | + return; |
| 58 | + } |
58 | 59 |
|
59 | | - $end_line_number = $this->customRoutesFileEndLine($file_lines); |
60 | | - $file_lines[$end_line_number + 1] = $file_lines[$end_line_number]; |
61 | | - $file_lines[$end_line_number] = ' '.$code; |
62 | | - $new_file_content = implode(PHP_EOL, $file_lines); |
| 60 | + // insert the given code before the file's last line |
| 61 | + $old_file_path = $disk->path($path); |
| 62 | + $file_lines = file($old_file_path, FILE_IGNORE_NEW_LINES); |
63 | 63 |
|
64 | | - if ($disk->put($path, $new_file_content)) { |
65 | | - $this->info('Successfully added code to '.$path); |
66 | | - } else { |
67 | | - $this->error('Could not write to file: '.$path); |
68 | | - } |
69 | | - } else { |
70 | | - Artisan::call('vendor:publish', ['--provider' => 'Backpack\CRUD\BackpackServiceProvider', '--tag' => 'custom_routes']); |
| 64 | + // if the code already exists in the file, abort |
| 65 | + if ($this->getLastLineNumberThatContains($code, $file_lines)) { |
| 66 | + $this->closeProgressBlock('Already existed', 'yellow'); |
71 | 67 |
|
72 | | - $this->handle(); |
| 68 | + return; |
73 | 69 | } |
| 70 | + |
| 71 | + $end_line_number = $this->customRoutesFileEndLine($file_lines); |
| 72 | + $file_lines[$end_line_number + 1] = $file_lines[$end_line_number]; |
| 73 | + $file_lines[$end_line_number] = ' '.$code; |
| 74 | + $new_file_content = implode(PHP_EOL, $file_lines); |
| 75 | + |
| 76 | + if (! $disk->put($path, $new_file_content)) { |
| 77 | + $this->errorProgressBlock(); |
| 78 | + $this->note('Could not write to file.', 'red'); |
| 79 | + |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + $this->closeProgressBlock(); |
74 | 84 | } |
75 | 85 |
|
76 | 86 | private function customRoutesFileEndLine($file_lines) |
|
0 commit comments