feat: laravel prompts
This commit is contained in:
@@ -10,6 +10,13 @@ use Illuminate\Support\Facades\Validator;
|
|||||||
use Illuminate\Validation\Rule;
|
use Illuminate\Validation\Rule;
|
||||||
use Illuminate\Validation\Rules\Password;
|
use Illuminate\Validation\Rules\Password;
|
||||||
|
|
||||||
|
use function Laravel\Prompts\error;
|
||||||
|
use function Laravel\Prompts\intro;
|
||||||
|
use function Laravel\Prompts\outro;
|
||||||
|
use function Laravel\Prompts\password;
|
||||||
|
use function Laravel\Prompts\select;
|
||||||
|
use function Laravel\Prompts\text;
|
||||||
|
|
||||||
class CreateUser extends Command
|
class CreateUser extends Command
|
||||||
{
|
{
|
||||||
protected $signature = 'user:create
|
protected $signature = 'user:create
|
||||||
@@ -23,12 +30,26 @@ class CreateUser extends Command
|
|||||||
|
|
||||||
public function handle(): int
|
public function handle(): int
|
||||||
{
|
{
|
||||||
$name = trim((string) ($this->option('name') ?: $this->ask('Name')));
|
intro('Create a Bowli user');
|
||||||
$email = strtolower(trim((string) ($this->option('email') ?: $this->ask('Email address'))));
|
|
||||||
$role = (string) ($this->option('role') ?: $this->choice(
|
$name = trim((string) ($this->option('name') ?: text(
|
||||||
'Role',
|
label: 'User name',
|
||||||
array_column(UserRole::cases(), 'value'),
|
placeholder: 'e.g. jane.doe',
|
||||||
UserRole::USER->value,
|
required: true,
|
||||||
|
hint: '3 to 32 characters: letters, numbers, dots, hyphens, or underscores.',
|
||||||
|
)));
|
||||||
|
$email = strtolower(trim((string) ($this->option('email') ?: text(
|
||||||
|
label: 'Email address',
|
||||||
|
placeholder: 'e.g. jane@example.com',
|
||||||
|
required: true,
|
||||||
|
))));
|
||||||
|
$role = (string) ($this->option('role') ?: select(
|
||||||
|
label: 'Role',
|
||||||
|
options: collect(UserRole::cases())
|
||||||
|
->mapWithKeys(fn (UserRole $role): array => [$role->value => ucfirst($role->value)])
|
||||||
|
->all(),
|
||||||
|
default: UserRole::USER->value,
|
||||||
|
hint: 'Choose the permissions granted to this user.',
|
||||||
));
|
));
|
||||||
$locale = strtolower(trim((string) $this->option('locale')));
|
$locale = strtolower(trim((string) $this->option('locale')));
|
||||||
|
|
||||||
@@ -46,14 +67,21 @@ class CreateUser extends Command
|
|||||||
|
|
||||||
if ($attributesValidator->fails()) {
|
if ($attributesValidator->fails()) {
|
||||||
foreach ($attributesValidator->errors()->all() as $error) {
|
foreach ($attributesValidator->errors()->all() as $error) {
|
||||||
$this->components->error($error);
|
error($error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::FAILURE;
|
return self::FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
$password = (string) $this->secret('Password');
|
$password = password(
|
||||||
$passwordConfirmation = (string) $this->secret('Confirm password');
|
label: 'Password',
|
||||||
|
required: true,
|
||||||
|
hint: 'At least 8 characters with uppercase, lowercase, number, and symbol.',
|
||||||
|
);
|
||||||
|
$passwordConfirmation = password(
|
||||||
|
label: 'Confirm password',
|
||||||
|
required: true,
|
||||||
|
);
|
||||||
|
|
||||||
$passwordValidator = Validator::make([
|
$passwordValidator = Validator::make([
|
||||||
'password' => $password,
|
'password' => $password,
|
||||||
@@ -72,7 +100,7 @@ class CreateUser extends Command
|
|||||||
|
|
||||||
if ($passwordValidator->fails()) {
|
if ($passwordValidator->fails()) {
|
||||||
foreach ($passwordValidator->errors()->all() as $error) {
|
foreach ($passwordValidator->errors()->all() as $error) {
|
||||||
$this->components->error($error);
|
error($error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::FAILURE;
|
return self::FAILURE;
|
||||||
@@ -87,7 +115,7 @@ class CreateUser extends Command
|
|||||||
'role' => UserRole::from($role),
|
'role' => UserRole::from($role),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->components->info("User {$user->email} created with ID {$user->getKey()} and role {$user->role->value}.");
|
outro("User {$user->email} created with ID {$user->getKey()} and role {$user->role->value}.");
|
||||||
|
|
||||||
return self::SUCCESS;
|
return self::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user