Skip to content

Commit

Permalink
[feat] add missing args model to command 'module:model-show'
Browse files Browse the repository at this point in the history
  • Loading branch information
alissn committed Sep 4, 2024
1 parent a9a5828 commit df0b623
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions src/Commands/Actions/ModelShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

namespace Nwidart\Modules\Commands\Actions;

use Illuminate\Contracts\Console\PromptsForMissingInput;
use Illuminate\Database\Console\ShowModelCommand;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use function Laravel\Prompts\search;
use function Laravel\Prompts\select;

#[AsCommand('module:model-show', 'Show information about an Eloquent model in modules')]
class ModelShowCommand extends ShowModelCommand
class ModelShowCommand extends ShowModelCommand implements PromptsForMissingInput
{
/**
* The console command name.
Expand Down Expand Up @@ -38,7 +43,6 @@ class ModelShowCommand extends ShowModelCommand
/**
* Qualify the given model class base name.
*
*
* @see \Illuminate\Console\GeneratorCommand
*/
protected function qualifyModel(string $model): string
Expand All @@ -47,15 +51,7 @@ protected function qualifyModel(string $model): string
return $model;
}

$pattern = sprintf(
'%s/*/%s/%s.php',
config('modules.paths.modules'),
config('modules.paths.generator.model.path'),
$model
);

$modelPaths = collect(File::glob($pattern))
->map($this->formatModuleNamespace(...));
$modelPaths = $this->findModels($model);

if ($modelPaths->count() == 0) {
return $model;
Expand All @@ -80,4 +76,36 @@ private function formatModuleNamespace(string $path): string
['', '\\', ''],
)->toString();
}

public function findModels(string $model): Collection
{
$pattern = sprintf(
'%s/*/%s/%s.php',
config('modules.paths.modules'),
config('modules.paths.generator.model.path'),
$model
);

return collect(File::glob($pattern))
->map($this->formatModuleNamespace(...));
}

protected function promptForMissingArguments(InputInterface $input, OutputInterface $output): void
{
$selected_item = search(
label: 'Select Model',
options: function (string $search_value) {
return $this->findModels(
Str::of($search_value)->wrap('', '*')
)->toArray();
},
placeholder: 'type some thing',
required: 'You must select one Model',
);

$input->setArgument(
name: 'model',
value: $selected_item
);
}
}

0 comments on commit df0b623

Please sign in to comment.