Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Illuminate/Console/Concerns/FindsAvailableModels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Illuminate\Console\Concerns;

use Illuminate\Support\Collection;
use Symfony\Component\Finder\Finder;

trait FindsAvailableModels
{
/**
* Get a list of possible model names.
*
* @return array<int, string>
*/
protected function findAvailableModels()
{
$modelPath = is_dir(app_path('Models')) ? app_path('Models') : app_path();

return (new Collection(Finder::create()->files()->depth(0)->in($modelPath)))
->map(fn ($file) => $file->getBasename('.php'))
->sort()
->values()
->all();
}
}
19 changes: 3 additions & 16 deletions src/Illuminate/Console/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Console;

use Illuminate\Console\Concerns\CreatesMatchingTest;
use Illuminate\Console\Concerns\FindsAvailableModels;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
Expand All @@ -12,6 +13,8 @@

abstract class GeneratorCommand extends Command implements PromptsForMissingInput
{
use FindsAvailableModels;

/**
* The filesystem instance.
*
Expand Down Expand Up @@ -239,22 +242,6 @@ protected function qualifyModel(string $model)
: $rootNamespace.$model;
}

/**
* Get a list of possible model names.
*
* @return array<int, string>
*/
protected function possibleModels()
{
$modelPath = is_dir(app_path('Models')) ? app_path('Models') : app_path();

return (new Collection(Finder::create()->files()->depth(0)->in($modelPath)))
->map(fn ($file) => $file->getBasename('.php'))
->sort()
->values()
->all();
}

/**
* Get a list of possible event names.
*
Expand Down
20 changes: 19 additions & 1 deletion src/Illuminate/Database/Console/ShowModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

namespace Illuminate\Database\Console;

use Illuminate\Console\Concerns\FindsAvailableModels;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\ModelInspector;
use Illuminate\Support\Collection;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Output\OutputInterface;

use function Laravel\Prompts\suggest;

#[AsCommand(name: 'model:show')]
class ShowModelCommand extends DatabaseInspectionCommand
class ShowModelCommand extends DatabaseInspectionCommand implements PromptsForMissingInput
{
use FindsAvailableModels;

/**
* The console command name.
*
Expand Down Expand Up @@ -211,4 +217,16 @@ protected function displayCli($class, $database, $table, $policy, $attributes, $

$this->newLine();
}

/**
* Prompt for missing input arguments using the returned questions.
*
* @return array<string, \Closure(): string>
*/
protected function promptForMissingArgumentsUsing(): array
{
return [
'model' => fn (): string => suggest('Which model would you like to show?', $this->findAvailableModels()),
];
}
}
4 changes: 2 additions & 2 deletions src/Illuminate/Foundation/Console/ObserverMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp
}

$model = suggest(
'What model should this observer apply to? (Optional)',
$this->possibleModels(),
'What model should be observed? (Optional)',
$this->findAvailableModels(),
);

if ($model) {
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Console/PolicyMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp

$model = suggest(
'What model should this policy apply to? (Optional)',
$this->possibleModels(),
$this->findAvailableModels(),
);

if ($model) {
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Routing/Console/ControllerMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp

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

if ($model) {
Expand Down
Loading