This commit is contained in:
TiclemFR
2024-01-20 23:14:52 +01:00
parent a068f54957
commit 031f7071e6
881 changed files with 241469 additions and 247870 deletions

View File

@@ -2,8 +2,6 @@
namespace Laravel\Prompts;
use Closure;
class ConfirmPrompt extends Prompt
{
/**
@@ -20,8 +18,8 @@ class ConfirmPrompt extends Prompt
public string $yes = 'Yes',
public string $no = 'No',
public bool|string $required = false,
public ?Closure $validate = null,
public string $hint = ''
public mixed $validate = null,
public string $hint = '',
) {
$this->confirmed = $default;

View File

@@ -35,7 +35,7 @@ class MultiSearchPrompt extends Prompt
public string $placeholder = '',
public int $scroll = 5,
public bool|string $required = false,
public ?Closure $validate = null,
public mixed $validate = null,
public string $hint = '',
) {
$this->trackTypedValue(submit: false, ignore: fn ($key) => Key::oneOf([Key::SPACE, Key::HOME, Key::END, Key::CTRL_A, Key::CTRL_E], $key) && $this->highlighted !== null);

View File

@@ -2,7 +2,6 @@
namespace Laravel\Prompts;
use Closure;
use Illuminate\Support\Collection;
class MultiSelectPrompt extends Prompt
@@ -42,8 +41,8 @@ class MultiSelectPrompt extends Prompt
array|Collection $default = [],
public int $scroll = 5,
public bool|string $required = false,
public ?Closure $validate = null,
public string $hint = ''
public mixed $validate = null,
public string $hint = '',
) {
$this->options = $options instanceof Collection ? $options->all() : $options;
$this->default = $default instanceof Collection ? $default->all() : $default;

View File

@@ -2,8 +2,6 @@
namespace Laravel\Prompts;
use Closure;
class PasswordPrompt extends Prompt
{
use Concerns\TypedValue;
@@ -15,8 +13,8 @@ class PasswordPrompt extends Prompt
public string $label,
public string $placeholder = '',
public bool|string $required = false,
public ?Closure $validate = null,
public string $hint = ''
public mixed $validate = null,
public string $hint = '',
) {
$this->trackTypedValue();
}

View File

@@ -45,15 +45,25 @@ abstract class Prompt
public bool|string $required;
/**
* The validator callback.
* The validator callback or rules.
*/
protected ?Closure $validate;
public mixed $validate;
/**
* The cancellation callback.
*/
protected static Closure $cancelUsing;
/**
* Indicates if the prompt has been validated.
*/
protected bool $validated = false;
/**
* The custom validation callback.
*/
protected static ?Closure $validateUsing;
/**
* The output instance.
*/
@@ -108,7 +118,11 @@ abstract class Prompt
if ($continue === false || $key === Key::CTRL_C) {
if ($key === Key::CTRL_C) {
static::terminal()->exit();
if (isset(static::$cancelUsing)) {
return (static::$cancelUsing)();
} else {
static::terminal()->exit();
}
}
return $this->value();
@@ -119,6 +133,14 @@ abstract class Prompt
}
}
/**
* Register a callback to be invoked when a user cancels a prompt.
*/
public static function cancelUsing(Closure $callback): void
{
static::$cancelUsing = $callback;
}
/**
* How many new lines were written by the last output.
*/
@@ -173,6 +195,14 @@ abstract class Prompt
return static::$terminal ??= new Terminal();
}
/**
* Set the custom validation callback.
*/
public static function validateUsing(Closure $callback): void
{
static::$validateUsing = $callback;
}
/**
* Render the prompt.
*/
@@ -314,14 +344,18 @@ abstract class Prompt
return;
}
if (! isset($this->validate)) {
if (! isset($this->validate) && ! isset(static::$validateUsing)) {
return;
}
$error = ($this->validate)($value);
$error = match (true) {
is_callable($this->validate) => ($this->validate)($value),
isset(static::$validateUsing) => (static::$validateUsing)($this),
default => throw new RuntimeException('The validation logic is missing.'),
};
if (! is_string($error) && ! is_null($error)) {
throw new \RuntimeException('The validator must return a string or null.');
throw new RuntimeException('The validator must return a string or null.');
}
if (is_string($error) && strlen($error) > 0) {

View File

@@ -28,7 +28,7 @@ class SearchPrompt extends Prompt
public Closure $options,
public string $placeholder = '',
public int $scroll = 5,
public ?Closure $validate = null,
public mixed $validate = null,
public string $hint = '',
public bool|string $required = true,
) {

View File

@@ -2,7 +2,6 @@
namespace Laravel\Prompts;
use Closure;
use Illuminate\Support\Collection;
use InvalidArgumentException;
@@ -27,7 +26,7 @@ class SelectPrompt extends Prompt
array|Collection $options,
public int|string|null $default = null,
public int $scroll = 5,
public ?Closure $validate = null,
public mixed $validate = null,
public string $hint = '',
public bool|string $required = true,
) {

View File

@@ -37,8 +37,8 @@ class SuggestPrompt extends Prompt
public string $default = '',
public int $scroll = 5,
public bool|string $required = false,
public ?Closure $validate = null,
public string $hint = ''
public mixed $validate = null,
public string $hint = '',
) {
$this->options = $options instanceof Collection ? $options->all() : $options;

View File

@@ -2,8 +2,6 @@
namespace Laravel\Prompts;
use Closure;
class TextPrompt extends Prompt
{
use Concerns\TypedValue;
@@ -16,8 +14,8 @@ class TextPrompt extends Prompt
public string $placeholder = '',
public string $default = '',
public bool|string $required = false,
public ?Closure $validate = null,
public string $hint = ''
public mixed $validate = null,
public string $hint = '',
) {
$this->trackTypedValue($default);
}

View File

@@ -8,17 +8,17 @@ use Illuminate\Support\Collection;
/**
* Prompt the user for text input.
*/
function text(string $label, string $placeholder = '', string $default = '', bool|string $required = false, ?Closure $validate = null, string $hint = ''): string
function text(string $label, string $placeholder = '', string $default = '', bool|string $required = false, mixed $validate = null, string $hint = ''): string
{
return (new TextPrompt($label, $placeholder, $default, $required, $validate, $hint))->prompt();
return (new TextPrompt(...func_get_args()))->prompt();
}
/**
* Prompt the user for input, hiding the value.
*/
function password(string $label, string $placeholder = '', bool|string $required = false, ?Closure $validate = null, string $hint = ''): string
function password(string $label, string $placeholder = '', bool|string $required = false, mixed $validate = null, string $hint = ''): string
{
return (new PasswordPrompt($label, $placeholder, $required, $validate, $hint))->prompt();
return (new PasswordPrompt(...func_get_args()))->prompt();
}
/**
@@ -27,9 +27,9 @@ function password(string $label, string $placeholder = '', bool|string $required
* @param array<int|string, string>|Collection<int|string, string> $options
* @param true|string $required
*/
function select(string $label, array|Collection $options, int|string|null $default = null, int $scroll = 5, ?Closure $validate = null, string $hint = '', bool|string $required = true): int|string
function select(string $label, array|Collection $options, int|string|null $default = null, int $scroll = 5, mixed $validate = null, string $hint = '', bool|string $required = true): int|string
{
return (new SelectPrompt($label, $options, $default, $scroll, $validate, $hint, $required))->prompt();
return (new SelectPrompt(...func_get_args()))->prompt();
}
/**
@@ -39,17 +39,17 @@ function select(string $label, array|Collection $options, int|string|null $defau
* @param array<int|string>|Collection<int, int|string> $default
* @return array<int|string>
*/
function multiselect(string $label, array|Collection $options, array|Collection $default = [], int $scroll = 5, bool|string $required = false, ?Closure $validate = null, string $hint = 'Use the space bar to select options.'): array
function multiselect(string $label, array|Collection $options, array|Collection $default = [], int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = 'Use the space bar to select options.'): array
{
return (new MultiSelectPrompt($label, $options, $default, $scroll, $required, $validate, $hint))->prompt();
return (new MultiSelectPrompt(...func_get_args()))->prompt();
}
/**
* Prompt the user to confirm an action.
*/
function confirm(string $label, bool $default = true, string $yes = 'Yes', string $no = 'No', bool|string $required = false, ?Closure $validate = null, string $hint = ''): bool
function confirm(string $label, bool $default = true, string $yes = 'Yes', string $no = 'No', bool|string $required = false, mixed $validate = null, string $hint = ''): bool
{
return (new ConfirmPrompt($label, $default, $yes, $no, $required, $validate, $hint))->prompt();
return (new ConfirmPrompt(...func_get_args()))->prompt();
}
/**
@@ -57,9 +57,9 @@ function confirm(string $label, bool $default = true, string $yes = 'Yes', strin
*
* @param array<string>|Collection<int, string>|Closure(string): array<string> $options
*/
function suggest(string $label, array|Collection|Closure $options, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, ?Closure $validate = null, string $hint = ''): string
function suggest(string $label, array|Collection|Closure $options, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = ''): string
{
return (new SuggestPrompt($label, $options, $placeholder, $default, $scroll, $required, $validate, $hint))->prompt();
return (new SuggestPrompt(...func_get_args()))->prompt();
}
/**
@@ -68,9 +68,9 @@ function suggest(string $label, array|Collection|Closure $options, string $place
* @param Closure(string): array<int|string, string> $options
* @param true|string $required
*/
function search(string $label, Closure $options, string $placeholder = '', int $scroll = 5, ?Closure $validate = null, string $hint = '', bool|string $required = true): int|string
function search(string $label, Closure $options, string $placeholder = '', int $scroll = 5, mixed $validate = null, string $hint = '', bool|string $required = true): int|string
{
return (new SearchPrompt($label, $options, $placeholder, $scroll, $validate, $hint, $required))->prompt();
return (new SearchPrompt(...func_get_args()))->prompt();
}
/**
@@ -79,9 +79,9 @@ function search(string $label, Closure $options, string $placeholder = '', int $
* @param Closure(string): array<int|string, string> $options
* @return array<int|string>
*/
function multisearch(string $label, Closure $options, string $placeholder = '', int $scroll = 5, bool|string $required = false, ?Closure $validate = null, string $hint = 'Use the space bar to select options.'): array
function multisearch(string $label, Closure $options, string $placeholder = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = 'Use the space bar to select options.'): array
{
return (new MultiSearchPrompt($label, $options, $placeholder, $scroll, $required, $validate, $hint))->prompt();
return (new MultiSearchPrompt(...func_get_args()))->prompt();
}
/**