Skip to content

Commit 738dabf

Browse files
committed
refactor: use PromptParamsHelper for robust parameter access
Replace direct array index access ($params[0], $params[1]) with named parameter access via PromptParamsHelper. This centralizes parameter mapping and makes the code more maintainable and robust against Laravel Prompts updates.
1 parent e053d31 commit 738dabf

File tree

2 files changed

+217
-60
lines changed

2 files changed

+217
-60
lines changed

packages/prompts/src/Filament/Components/RunCommandComponent.php

Lines changed: 119 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Livewire\Component;
1515
use Moox\Prompts\Support\PromptFlowRunner;
1616
use Moox\Prompts\Support\PromptFlowStateStore;
17+
use Moox\Prompts\Support\PromptParamsHelper;
1718
use Moox\Prompts\Support\PromptResponseStore;
1819
use Moox\Prompts\Support\PromptRuntime;
1920
use Moox\Prompts\Support\WebCommandRunner;
@@ -177,24 +178,64 @@ protected function appendOutput(?string $newOutput): void
177178
protected function prefillPromptForm(array $prompt): void
178179
{
179180
$promptId = $prompt['id'] ?? null;
180-
if (! $promptId || ! isset($this->answers[$promptId])) {
181+
if (! $promptId) {
181182
return;
182183
}
183184

184-
$value = $this->answers[$promptId];
185-
if (($prompt['method'] ?? '') === 'multiselect') {
186-
if (! is_array($value)) {
187-
if ($value === true) {
188-
$params = $prompt['params'] ?? [];
189-
$options = $params[1] ?? [];
190-
$value = array_keys($options);
191-
} else {
192-
$value = [];
185+
$method = $prompt['method'] ?? '';
186+
$params = $prompt['params'] ?? [];
187+
$p = PromptParamsHelper::extract($method, $params);
188+
189+
// Wenn bereits eine Antwort vorhanden ist, diese verwenden
190+
if (isset($this->answers[$promptId])) {
191+
$value = $this->answers[$promptId];
192+
if ($method === 'multiselect') {
193+
if (! is_array($value)) {
194+
if ($value === true) {
195+
$options = $p['options'] ?? [];
196+
$value = array_keys($options);
197+
} else {
198+
$value = [];
199+
}
193200
}
194201
}
202+
$this->form->fill([$promptId => $value]);
203+
return;
204+
}
205+
206+
// Ansonsten Default-Wert aus den Prompt-Params verwenden
207+
if ($method === 'confirm') {
208+
$default = $p['default'] ?? false; // default Parameter (bool)
209+
$value = $default ? 'yes' : 'no';
210+
$this->form->fill([$promptId => $value]);
211+
return;
212+
}
213+
214+
if ($method === 'multiselect') {
215+
$defaultValue = $p['default'] ?? []; // default Parameter (array)
216+
// Für multiselect müssen wir die einzelnen Checkboxen füllen
217+
$options = $p['options'] ?? [];
218+
$fillData = [];
219+
foreach (array_keys($options) as $key) {
220+
$checkboxId = $promptId.'_'.$key;
221+
$fillData[$checkboxId] = is_array($defaultValue) && in_array($key, $defaultValue);
222+
}
223+
if (! empty($fillData)) {
224+
$this->form->fill($fillData);
225+
}
226+
return;
195227
}
196228

197-
$this->form->fill([$promptId => $value]);
229+
$defaultValue = null;
230+
if ($method === 'select') {
231+
$defaultValue = $p['default'] ?? null; // default Parameter
232+
} elseif (in_array($method, ['text', 'textarea', 'password'])) {
233+
$defaultValue = $p['default'] ?? ''; // default Parameter
234+
}
235+
236+
if ($defaultValue !== null) {
237+
$this->form->fill([$promptId => $defaultValue]);
238+
}
198239
}
199240

200241
protected function formatThrowableMessage(Throwable $e): string
@@ -276,7 +317,7 @@ public function submitPrompt(): void
276317
$answer = false;
277318
}
278319

279-
if (($answer === null || $answer === '' || ($this->currentPrompt['method'] === 'multiselect' && ! is_array($answer))) && $this->currentPrompt['method'] !== 'confirm') {
320+
if (($answer === null || $answer === '' || ($this->currentPrompt['method'] === 'multiselect' && ! is_array($answer))) && $this->currentPrompt['method'] !== 'confirm') {
280321
try {
281322
$data = $this->form->getState();
282323
$answer = $data[$promptId] ?? null;
@@ -353,7 +394,11 @@ public function submitPrompt(): void
353394
}
354395

355396
if ($this->currentPrompt['method'] === 'confirm') {
356-
if (! is_bool($answer)) {
397+
if ($answer === 'yes') {
398+
$answer = true;
399+
} elseif ($answer === 'no') {
400+
$answer = false;
401+
} elseif (! is_bool($answer)) {
357402
$answer = (bool) $answer;
358403
}
359404
}
@@ -377,24 +422,26 @@ protected function validatePromptAnswer(string $promptId, mixed $answer, array $
377422
$method = $prompt['method'] ?? '';
378423
$params = $prompt['params'] ?? [];
379424

425+
$p = PromptParamsHelper::extract($method, $params);
426+
380427
$rules = [];
381428
$messages = [];
382429

383430
$requiredFlag = match ($method) {
384-
'text', 'textarea', 'password' => $params[3] ?? false,
385-
'multiselect' => $params[3] ?? false,
386-
'confirm' => $params[2] ?? false,
431+
'text', 'textarea', 'password' => $p['required'] ?? false,
432+
'multiselect' => $p['required'] ?? false,
433+
'confirm' => $p['required'] ?? false,
387434
default => false,
388435
};
389436

390-
if ($method === 'multiselect' && ! empty($params[1] ?? [])) {
437+
if ($method === 'multiselect' && ! empty($p['options'] ?? [])) {
391438
$requiredFlag = true;
392439
}
393440

394441
$validate = match ($method) {
395-
'text', 'textarea', 'password' => $params[4] ?? null,
396-
'select' => $params[5] ?? null,
397-
'multiselect' => $params[6] ?? null,
442+
'text', 'textarea', 'password' => $p['validate'] ?? null,
443+
'select' => $p['validate'] ?? null,
444+
'multiselect' => $p['validate'] ?? null,
398445
default => null,
399446
};
400447

@@ -420,12 +467,12 @@ protected function validatePromptAnswer(string $promptId, mixed $answer, array $
420467
}
421468
}
422469

423-
if ($method === 'confirm') {
424-
$rules[] = 'boolean';
470+
if ($method === 'confirm' && $requiredFlag !== false) {
471+
$rules[] = 'required';
425472
}
426473

427474
if ($method === 'select' && $requiredFlag !== false) {
428-
$rules[] = 'in:'.implode(',', array_keys($params[1] ?? []));
475+
$rules[] = 'in:'.implode(',', array_keys($p['options'] ?? []));
429476
}
430477

431478
$pushRules($rules, $validate);
@@ -449,19 +496,25 @@ protected function validatePromptAnswer(string $promptId, mixed $answer, array $
449496
$callableErrors[] = $result;
450497
}
451498
if ($result === false) {
452-
$callableErrors[] = 'Ungültiger Wert.';
499+
$callableErrors[] = __('moox-prompts::prompts.validation.callable_invalid');
453500
}
454501
}
455502

456503
if (! empty($rules)) {
504+
$label = $p['label'] ?? $promptId;
505+
506+
if (in_array($method, ['text', 'textarea', 'password'])) {
507+
$messages["{$promptId}.required"] = __('moox-prompts::prompts.validation.text_required', ['label' => $label]);
508+
}
509+
457510
if ($method === 'multiselect') {
458-
$messages["{$promptId}.required"] = 'Bitte mindestens eine Option wählen.';
459-
$messages["{$promptId}.min"] = 'Bitte mindestens eine Option wählen.';
511+
$messages["{$promptId}.required"] = __('moox-prompts::prompts.validation.multiselect_required');
512+
$messages["{$promptId}.min"] = __('moox-prompts::prompts.validation.multiselect_min');
460513
}
461514

462515
if ($method === 'select') {
463-
$messages["{$promptId}.required"] = 'Bitte wählen Sie eine Option aus.';
464-
$messages["{$promptId}.in"] = 'Bitte wählen Sie eine gültige Option aus.';
516+
$messages["{$promptId}.required"] = __('moox-prompts::prompts.validation.select_required');
517+
$messages["{$promptId}.in"] = __('moox-prompts::prompts.validation.select_in');
465518
}
466519

467520
$validator = Validator::make(
@@ -517,10 +570,13 @@ protected function getFormSchema(): array
517570

518571
protected function createMultiselectFields(string $promptId, array $params): array
519572
{
520-
$label = $params[0] ?? '';
521-
$required = ($params[3] ?? false) !== false;
522-
$defaultValue = $this->answers[$promptId] ?? null;
523-
$options = $params[1] ?? [];
573+
$p = PromptParamsHelper::extract('multiselect', $params);
574+
575+
$label = $p['label'] ?? '';
576+
$required = ($p['required'] ?? false) !== false;
577+
// Default-Wert: erst aus answers, dann aus default-Parameter
578+
$defaultValue = $this->answers[$promptId] ?? ($p['default'] ?? []);
579+
$options = $p['options'] ?? [];
524580

525581
$fields = [];
526582

@@ -543,23 +599,30 @@ protected function createMultiselectFields(string $promptId, array $params): arr
543599

544600
protected function createFieldFromPrompt(string $promptId, string $method, array $params): ?\Filament\Forms\Components\Field
545601
{
546-
$label = $params[0] ?? '';
602+
$p = PromptParamsHelper::extract($method, $params);
603+
604+
$label = $p['label'] ?? '';
547605
$required = match ($method) {
548-
'text', 'textarea', 'password' => ($params[3] ?? false) !== false,
549-
'multiselect' => ($params[3] ?? false) !== false,
550-
'confirm' => ($params[2] ?? false) !== false,
551-
'select' => ($params[2] ?? null) === null,
606+
'text', 'textarea', 'password' => ($p['required'] ?? false) !== false,
607+
'multiselect' => ($p['required'] ?? false) !== false,
608+
'confirm' => ($p['required'] ?? false) !== false,
609+
'select' => ($p['default'] ?? null) === null,
552610
default => false,
553611
};
554612
$defaultValue = $this->answers[$promptId] ?? null;
555-
$options = $params[1] ?? [];
556-
$defaultSelect = $defaultValue ?? ($params[2] ?? null);
557-
$confirmDefault = $defaultValue ?? ($params[1] ?? false);
613+
$options = $p['options'] ?? [];
614+
$defaultSelect = $defaultValue ?? ($p['default'] ?? null);
615+
616+
// Für confirm: Default aus params[1] (default Parameter), falls noch keine Antwort vorhanden
617+
$confirmDefault = null;
618+
if ($method === 'confirm') {
619+
$confirmDefault = $defaultValue !== null ? $defaultValue : ($p['default'] ?? false);
620+
}
558621

559622
$validate = match ($method) {
560-
'text', 'textarea', 'password' => $params[4] ?? null,
561-
'select' => $params[5] ?? null,
562-
'multiselect' => $params[6] ?? null,
623+
'text', 'textarea', 'password' => $p['validate'] ?? null,
624+
'select' => $p['validate'] ?? null,
625+
'multiselect' => $p['validate'] ?? null,
563626
default => null,
564627
};
565628

@@ -585,36 +648,32 @@ protected function createFieldFromPrompt(string $promptId, string $method, array
585648
$rules[] = 'in:'.implode(',', array_keys($options));
586649
}
587650

588-
if ($method === 'confirm') {
589-
$rules[] = 'boolean';
590-
}
591-
592651
$pushRules($rules, $validate);
593652

594653
return match ($method) {
595654
'text' => TextInput::make($promptId)
596655
->label($label)
597-
->placeholder($params[1] ?? '')
598-
->default($defaultValue ?? $params[2] ?? '')
656+
->placeholder($p['placeholder'] ?? '')
657+
->default($defaultValue ?? $p['default'] ?? '')
599658
->rules($rules)
600-
->hint($params[6] ?? null)
659+
->hint($p['hint'] ?? null)
601660
->live(onBlur: false),
602661

603662
'textarea' => Textarea::make($promptId)
604663
->label($label)
605-
->placeholder($params[1] ?? '')
606-
->default($defaultValue ?? $params[2] ?? '')
664+
->placeholder($p['placeholder'] ?? '')
665+
->default($defaultValue ?? $p['default'] ?? '')
607666
->rules($rules)
608667
->rows(5)
609-
->hint($params[6] ?? null),
668+
->hint($p['hint'] ?? null),
610669

611670
'password' => TextInput::make($promptId)
612671
->label($label)
613672
->password()
614-
->placeholder($params[1] ?? '')
673+
->placeholder($p['placeholder'] ?? '')
615674
->default($defaultValue ?? '')
616675
->rules($rules)
617-
->hint($params[6] ?? null)
676+
->hint($p['hint'] ?? null)
618677
->live(onBlur: false),
619678

620679
'select' => Select::make($promptId)
@@ -623,20 +682,20 @@ protected function createFieldFromPrompt(string $promptId, string $method, array
623682
->default($defaultSelect !== null ? $defaultSelect : null)
624683
->rules($rules)
625684
->placeholder('Bitte wählen...')
626-
->hint($params[4] ?? null)
685+
->hint($p['hint'] ?? null)
627686
->live(onBlur: false),
628687

629688
'multiselect' => null,
630689

631690
'confirm' => Radio::make($promptId)
632691
->label($label)
633692
->options([
634-
true => 'Ja',
635-
false => 'Nein',
693+
'yes' => 'Ja',
694+
'no' => 'Nein',
636695
])
637-
->default($confirmDefault)
696+
->default($confirmDefault !== null ? ($confirmDefault ? 'yes' : 'no') : null)
638697
->rules($rules)
639-
->hint($params[6] ?? null)
698+
->hint($p['hint'] ?? null)
640699
->live(onBlur: false),
641700

642701
default => null,

0 commit comments

Comments
 (0)