Skip to content

Commit 7f98d12

Browse files
[12.x] model:show command prompt for missing input with model suggestion (#57671)
* Refactor GeneratorCommand and ShowModelCommand to use InteractsWithModels trait for model interaction. Moved possibleModels method to the new trait for better code organization. * formatting and type improvements * formatting * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 810d6f4 commit 7f98d12

File tree

6 files changed

+52
-22
lines changed

6 files changed

+52
-22
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Illuminate\Console\Concerns;
4+
5+
use Illuminate\Support\Collection;
6+
use Symfony\Component\Finder\Finder;
7+
8+
trait FindsAvailableModels
9+
{
10+
/**
11+
* Get a list of possible model names.
12+
*
13+
* @return array<int, string>
14+
*/
15+
protected function findAvailableModels()
16+
{
17+
$modelPath = is_dir(app_path('Models')) ? app_path('Models') : app_path();
18+
19+
return (new Collection(Finder::create()->files()->depth(0)->in($modelPath)))
20+
->map(fn ($file) => $file->getBasename('.php'))
21+
->sort()
22+
->values()
23+
->all();
24+
}
25+
}

src/Illuminate/Console/GeneratorCommand.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Console;
44

55
use Illuminate\Console\Concerns\CreatesMatchingTest;
6+
use Illuminate\Console\Concerns\FindsAvailableModels;
67
use Illuminate\Contracts\Console\PromptsForMissingInput;
78
use Illuminate\Filesystem\Filesystem;
89
use Illuminate\Support\Collection;
@@ -12,6 +13,8 @@
1213

1314
abstract class GeneratorCommand extends Command implements PromptsForMissingInput
1415
{
16+
use FindsAvailableModels;
17+
1518
/**
1619
* The filesystem instance.
1720
*
@@ -239,22 +242,6 @@ protected function qualifyModel(string $model)
239242
: $rootNamespace.$model;
240243
}
241244

242-
/**
243-
* Get a list of possible model names.
244-
*
245-
* @return array<int, string>
246-
*/
247-
protected function possibleModels()
248-
{
249-
$modelPath = is_dir(app_path('Models')) ? app_path('Models') : app_path();
250-
251-
return (new Collection(Finder::create()->files()->depth(0)->in($modelPath)))
252-
->map(fn ($file) => $file->getBasename('.php'))
253-
->sort()
254-
->values()
255-
->all();
256-
}
257-
258245
/**
259246
* Get a list of possible event names.
260247
*

src/Illuminate/Database/Console/ShowModelCommand.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
namespace Illuminate\Database\Console;
44

5+
use Illuminate\Console\Concerns\FindsAvailableModels;
6+
use Illuminate\Contracts\Console\PromptsForMissingInput;
57
use Illuminate\Contracts\Container\BindingResolutionException;
68
use Illuminate\Database\Eloquent\ModelInspector;
79
use Illuminate\Support\Collection;
810
use Symfony\Component\Console\Attribute\AsCommand;
911
use Symfony\Component\Console\Output\OutputInterface;
1012

13+
use function Laravel\Prompts\suggest;
14+
1115
#[AsCommand(name: 'model:show')]
12-
class ShowModelCommand extends DatabaseInspectionCommand
16+
class ShowModelCommand extends DatabaseInspectionCommand implements PromptsForMissingInput
1317
{
18+
use FindsAvailableModels;
19+
1420
/**
1521
* The console command name.
1622
*
@@ -211,4 +217,16 @@ protected function displayCli($class, $database, $table, $policy, $attributes, $
211217

212218
$this->newLine();
213219
}
220+
221+
/**
222+
* Prompt for missing input arguments using the returned questions.
223+
*
224+
* @return array<string, \Closure(): string>
225+
*/
226+
protected function promptForMissingArgumentsUsing(): array
227+
{
228+
return [
229+
'model' => fn (): string => suggest('Which model would you like to show?', $this->findAvailableModels()),
230+
];
231+
}
214232
}

src/Illuminate/Foundation/Console/ObserverMakeCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp
158158
}
159159

160160
$model = suggest(
161-
'What model should this observer apply to? (Optional)',
162-
$this->possibleModels(),
161+
'What model should be observed? (Optional)',
162+
$this->findAvailableModels(),
163163
);
164164

165165
if ($model) {

src/Illuminate/Foundation/Console/PolicyMakeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp
218218

219219
$model = suggest(
220220
'What model should this policy apply to? (Optional)',
221-
$this->possibleModels(),
221+
$this->findAvailableModels(),
222222
);
223223

224224
if ($model) {

src/Illuminate/Routing/Console/ControllerMakeCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp
328328

329329
if (in_array($type, ['api', 'resource', 'singleton'])) {
330330
$model = suggest(
331-
"What model should this $type controller be for? (Optional)",
332-
$this->possibleModels()
331+
"What model is this $type controller for? (Optional)",
332+
$this->findAvailableModels()
333333
);
334334

335335
if ($model) {

0 commit comments

Comments
 (0)