From a9a58287768042a43941b9ceb0e159e4047fcf99 Mon Sep 17 00:00:00 2001 From: Ali Sasani Date: Wed, 4 Sep 2024 10:59:41 +0330 Subject: [PATCH 1/2] [refactor] refactor command 'module:model-show' --- src/Commands/Actions/ModelShowCommand.php | 38 ++++++++++++++++++----- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/Commands/Actions/ModelShowCommand.php b/src/Commands/Actions/ModelShowCommand.php index 1fe385a75..2498e53a8 100644 --- a/src/Commands/Actions/ModelShowCommand.php +++ b/src/Commands/Actions/ModelShowCommand.php @@ -3,8 +3,12 @@ namespace Nwidart\Modules\Commands\Actions; use Illuminate\Database\Console\ShowModelCommand; +use Illuminate\Support\Facades\File; +use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; +use function Laravel\Prompts\select; + #[AsCommand('module:model-show', 'Show information about an Eloquent model in modules')] class ModelShowCommand extends ShowModelCommand { @@ -43,17 +47,37 @@ protected function qualifyModel(string $model): string return $model; } - $rootNamespace = config('modules.namespace'); + $pattern = sprintf( + '%s/*/%s/%s.php', + config('modules.paths.modules'), + config('modules.paths.generator.model.path'), + $model + ); - $modelPath = glob($rootNamespace.DIRECTORY_SEPARATOR. - '*'.DIRECTORY_SEPARATOR. - config('modules.paths.generator.model.path').DIRECTORY_SEPARATOR. - "$model.php"); + $modelPaths = collect(File::glob($pattern)) + ->map($this->formatModuleNamespace(...)); - if (! count($modelPath)) { + if ($modelPaths->count() == 0) { return $model; + } elseif ($modelPaths->count() == 1) { + return $this->formatModuleNamespace($modelPaths->first()); } - return str_replace(['/', '.php'], ['\\', ''], $modelPath[0]); + return select( + label: 'Select Model', + options: $modelPaths, + required: 'You must select at least one model', + ); + } + + private function formatModuleNamespace(string $path): string + { + return + Str::of($path) + ->after(base_path().DIRECTORY_SEPARATOR) + ->replace( + [config('modules.paths.app_folder'), '/', '.php'], + ['', '\\', ''], + )->toString(); } } From df0b623f8023d46e2c4eda129220013174fec263 Mon Sep 17 00:00:00 2001 From: Ali Sasani Date: Wed, 4 Sep 2024 11:22:36 +0330 Subject: [PATCH 2/2] [feat] add missing args model to command 'module:model-show' --- src/Commands/Actions/ModelShowCommand.php | 50 ++++++++++++++++++----- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/src/Commands/Actions/ModelShowCommand.php b/src/Commands/Actions/ModelShowCommand.php index 2498e53a8..f50df1e25 100644 --- a/src/Commands/Actions/ModelShowCommand.php +++ b/src/Commands/Actions/ModelShowCommand.php @@ -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. @@ -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 @@ -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; @@ -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 + ); + } }