Skip to content

Commit

Permalink
Merge pull request #1931 from alissn/RefactorModelShow
Browse files Browse the repository at this point in the history
Refactor `module:model-show` Command for Improved Model Detection
  • Loading branch information
dcblogdev authored Sep 10, 2024
2 parents 2890081 + df0b623 commit 427315e
Showing 1 changed file with 62 additions and 10 deletions.
72 changes: 62 additions & 10 deletions src/Commands/Actions/ModelShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +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 @@ -34,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 @@ -43,17 +51,61 @@ protected function qualifyModel(string $model): string
return $model;
}

$rootNamespace = config('modules.namespace');
$modelPaths = $this->findModels($model);

$modelPath = glob($rootNamespace.DIRECTORY_SEPARATOR.
'*'.DIRECTORY_SEPARATOR.
config('modules.paths.generator.model.path').DIRECTORY_SEPARATOR.
"$model.php");

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();
}

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 427315e

Please sign in to comment.