|
3 | 3 | namespace Rappasoft\LaravelLivewireTables\Commands; |
4 | 4 |
|
5 | 5 | use Illuminate\Console\Command; |
| 6 | +use Illuminate\Contracts\Console\PromptsForMissingInput; |
6 | 7 | use Illuminate\Database\Eloquent\Model; |
7 | 8 | use Illuminate\Support\Facades\File; |
8 | 9 | use Illuminate\Support\Str; |
9 | 10 | use Livewire\Features\SupportConsoleCommands\Commands\ComponentParser; |
10 | 11 | use Livewire\Features\SupportConsoleCommands\Commands\MakeCommand as LivewireMakeCommand; |
| 12 | +use Symfony\Component\Console\Input\InputInterface; |
| 13 | +use Symfony\Component\Console\Output\OutputInterface; |
| 14 | +use Symfony\Component\Finder\Finder; |
| 15 | + |
| 16 | +use function Laravel\Prompts\suggest; |
| 17 | +use function Laravel\Prompts\text; |
11 | 18 |
|
12 | 19 | /** |
13 | 20 | * Class MakeCommand |
14 | 21 | */ |
15 | | -class MakeCommand extends Command |
| 22 | +class MakeCommand extends Command implements PromptsForMissingInput |
16 | 23 | { |
17 | 24 | protected ComponentParser $parser; |
18 | 25 |
|
@@ -164,4 +171,52 @@ private function generateColumns(string $modelName): string |
164 | 171 |
|
165 | 172 | return $columns; |
166 | 173 | } |
| 174 | + |
| 175 | + protected function possibleModels() |
| 176 | + { |
| 177 | + $modelPath = is_dir(app_path('Models')) ? app_path('Models') : app_path(); |
| 178 | + |
| 179 | + return collect(Finder::create()->files()->depth(0)->in($modelPath)) |
| 180 | + ->map(fn ($file) => $file->getBasename('.php')) |
| 181 | + ->sort() |
| 182 | + ->values() |
| 183 | + ->all(); |
| 184 | + } |
| 185 | + |
| 186 | + protected function promptForMissingArguments(InputInterface $input, OutputInterface $output) |
| 187 | + { |
| 188 | + |
| 189 | + if ($this->didReceiveOptions($input)) { |
| 190 | + return; |
| 191 | + } |
| 192 | + |
| 193 | + if (trim($this->argument('name')) === '') { |
| 194 | + $name = text('What is the name of your Livewire class?', 'TestTable'); |
| 195 | + |
| 196 | + if ($name) { |
| 197 | + $input->setArgument('name', $name); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + if (trim($this->argument('model')) === '') { |
| 202 | + $model = suggest( |
| 203 | + 'What is the name of the model you want to use in this table?', |
| 204 | + $this->possibleModels(), |
| 205 | + 'Test' |
| 206 | + ); |
| 207 | + |
| 208 | + if ($model) { |
| 209 | + $input->setArgument('model', $model); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + if (trim($this->argument('modelpath')) === '' && ! in_array($this->argument('model'), $this->possibleModels())) { |
| 214 | + |
| 215 | + $modelPath = text('What is the path to the model you want to use in this table?', 'app/TestModels/'); |
| 216 | + |
| 217 | + if ($modelPath) { |
| 218 | + $input->setArgument('modelpath', $modelPath); |
| 219 | + } |
| 220 | + } |
| 221 | + } |
167 | 222 | } |
0 commit comments