Skip to content

Commit

Permalink
refactor: Resource generation now based from selected model
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-code-labx committed Jun 7, 2024
1 parent 82f268d commit f521265
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 42 deletions.
63 changes: 22 additions & 41 deletions src/Commands/Generator/MakeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ final class MakeResource extends GeneratorCommand
*/
public function handle(): ?bool
{

$this->actions ??= collect();
$this->filters ??= collect();
$this->presenters ??= collect();
Expand Down Expand Up @@ -110,6 +111,19 @@ public function handle(): ?bool
return null;
}

protected function interact(InputInterface $input, OutputInterface $output): void
{
parent::interact($input, $output);

if ($this->didReceiveOptions($input) || $this->argument('kit_namespace')) {
return;
}

$this->promptForModel($input);
$this->promptForFilters($input);
$this->promptForPresenters($input);
}

protected function getStub(): string
{
$stub = $this->argument('name') === 'Auth'
Expand Down Expand Up @@ -156,40 +170,15 @@ protected function buildClass($name): string
protected function getArguments(): array
{
return [
['name', InputArgument::REQUIRED, 'The name of the '.strtolower($this->type)],
['type', InputArgument::REQUIRED, 'The type of resource to create'],
['name', InputArgument::OPTIONAL, 'The name of the '.strtolower($this->type)],
['type', InputArgument::OPTIONAL, 'The type of resource to create'],
['model', InputArgument::OPTIONAL, 'The model that the resource references'],
['filters', InputArgument::OPTIONAL, 'The filters to include in the resource'],
['presenters', InputArgument::OPTIONAL, 'The presenters to include in the resource'],
['kit_namespace', InputArgument::OPTIONAL, 'The namespace of the '.strtolower($this->type)],
];
}

/**
* @return array<string, array<int, string>>
*/
protected function promptForMissingArgumentsUsing(): array
{
return [
'name' => [
'Whats the name of your resource? (singular)',
'e.g. Post',
],
];
}

protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output): void
{
if ($this->didReceiveOptions($input)) {
return;
}

$this->promptForResourceType($input);
$this->promptForModel($input);
$this->promptForFilters($input);
$this->promptForPresenters($input);
}

private function guessRelationSearchKey(string $filter): ?string
{
if (! $relationTable = $this->findTableByName(table: $filter, exactMatch: false)) {
Expand Down Expand Up @@ -293,20 +282,10 @@ private function getPresenters(): Collection
return $this->presentersArgument;
}

private function promptForResourceType(InputInterface $input): void
{
$type = select('Which type of resource would you like to create?', [
'new' => 'New resource',
'extend' => 'Extend existing resource',
]);

$input->setArgument('type', $type);
}

private function promptForModel(InputInterface $input): void
{
$model = search(
label: 'Which model should the resource use?',
label: 'Which model should the resource be generated for?',
options: fn (): array => $this->scanModels(), // @phpstan-ignore-line
placeholder: 'Search for a model...',
hint: 'Press <comment>Enter</> to select the model.'
Expand All @@ -316,6 +295,8 @@ private function promptForModel(InputInterface $input): void
$this->setModel($model);

$input->setArgument('model', $model);
$input->setArgument('name', class_basename($model));
$input->setArgument('type', 'new');
}

private function promptForFilters(InputInterface $input): void
Expand All @@ -325,10 +306,10 @@ private function promptForFilters(InputInterface $input): void
if ($suggestFilters->isNotEmpty()) {
/** @var array<string> $selectedFilters */
$selectedFilters = multiselect(
label: 'Here are some suggested filters to add to your resource:',
label: __('Here are some suggested filters we found in your :model model to add to your generated resource:', ['model' => class_basename($this->model)]),
options: $suggestFilters, // @phpstan-ignore-line
default: $suggestFilters->keys(), // @phpstan-ignore-line
hint: 'Press <comment>Enter</> to select the filters.'
hint: 'Press <comment>Space</> to select the filters then <comment>Enter</> to confirm.'
);

$this->filters = $suggestFilters->only($selectedFilters);
Expand Down Expand Up @@ -392,7 +373,7 @@ private function promptForPresenters(InputInterface $input, bool $init = true):
label: 'Select the fields you would like to include in the presenter:',
options: $fields->keys()->toArray(), // @phpstan-ignore-line
default: $fields->keys(),
hint: 'Press <comment>Enter</> to select the fields.'
hint: 'Press <comment>Space</> to select the fields then <comment>Enter</> to confirm.'
);
$presenterFields = $fields->only($selectedFields)->toArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace {{ namespace }};

use {{ resourceNamespace }} as {{ aliasResource }};
use XtendPackages\RESTPresenter\StarterKits\Auth\Sanctum\Actions;
use XtendPackages\RESTPresenter\StarterKits\Sanctum\Actions;

class {{ class }} extends {{ aliasResource }}
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use {{ resourceNamespace }} as {{ aliasResource }};

class {{ class }} extends {{ aliasResource }}
{
public static bool $isAuthenticated = false;

public function routeActions(): array
{
return array_merge(
Expand Down

0 comments on commit f521265

Please sign in to comment.