src/Command/AddUserCommand.php
/**
     * This method is executed after initialize() and before execute(). Its purpose
     * is to check if some of the options/arguments are missing and interactively
     * ask the user for those values.
     *
     * This method is completely optional. If you are developing an internal console
     * command, you probably should not implement this method because it requires
     * quite a lot of work. However, if the command is meant to be used by external
     * users, this method is a nice way to fall back and prevent errors.
     */
This method is executed after initialize() and before execute(). Its purpose
is to check if some of the options/arguments are missing and interactively
ask the user for those values.

This method is completely optional. If you are developing an internal console
command, you probably should not implement this method because it requires
quite a lot of work. However, if the command is meant to be used by external
users, this method is a nice way to fall back and prevent errors.
This method is executed after initialize () and before execute () . Its purpose is to check if some of the options / arguments are missing and interactively ask the user for those values . This method is completely optional . If you are developing an internal console command you probably should not implement this method because it requires quite a lot of work . However if the command is meant to be used by external users this method is a nice way to fall back and prevent errors .
protected function interact(InputInterface $input, OutputInterface $output): void
    {
        if ($input->getArgument('username') !== null && $input->getArgument('password') !== null && $input->getArgument('email') !== null && $input->getArgument('display-name') !== null) {
            return;
        }
        $this->io->title('Add Bolt User Command');
        $this->io->text([
            'If you prefer to not use this interactive wizard, provide the',
            'arguments required by this command as follows:',
            '',
            ' $ php bin/console bolt:add-user username password email@example.com DisplayName',
            '',
            'Now we\'ll ask you for the value of all the missing command arguments.',
        ]);
        $username = $input->getArgument('username');
        if ($username !== null) {
            $this->io->text(' > <info>Username</info>: ' . $username);
        } else {
            $username = $this->io->ask('Username', null, function (?string $username) {
                $errors = $this->validator->validatePropertyValue(User::class, 'username', $username);
                if ($errors->count() > 0) {
                    throw new InvalidArgumentException($errors->get(0)->getMessage());
                }
                return $username;
            });
            $input->setArgument('username', $username);
        }
        $password = $input->getArgument('password');
        if ($password !== null) {
            $this->io->text(' > <info>Password</info>: ' . str_repeat('*', mb_strlen($password)));
        } else {
            $passwordQuestion = new Question('Password (input is hidden)', Str::generatePassword());
            $passwordQuestion->setHidden(true);
            $passwordQuestion->setValidator(function (?string $password) {
                $errors = $this->validator->validatePropertyValue(User::class, 'plainPassword', $password);
                if ($errors->count() > 0) {
                    throw new InvalidArgumentException($errors->get(0)->getMessage());
                }
                return $password;
            });
            $password = $this->io->askQuestion($passwordQuestion);
            $input->setArgument('password', $password);
        }
        $email = $input->getArgument('email');
        if ($email !== null) {
            $this->io->text(' > <info>Email</info>: ' . $email);
        } else {
            $email = $this->io->ask('Email', null, function (?string $email) {
                $errors = $this->validator->validatePropertyValue(User::class, 'email', $email);
                if ($errors->count() > 0) {
                    throw new InvalidArgumentException($errors->get(0)->getMessage());
                }
                return $email;
            });
            $input->setArgument('email', $email);
        }
        $displayName = $input->getArgument('display-name');
        if ($displayName !== null) {
            $this->io->text(' > <info>Display Name</info>: ' . $displayName);
        } else {
            $displayName = $this->io->ask('Display Name', null, function (?string $displayName) {
                $errors = $this->validator->validatePropertyValue(User::class, 'displayName', $displayName);
                if ($errors->count() > 0) {
                    throw new InvalidArgumentException($errors->get(0)->getMessage());
                }
                return $displayName;
            });
            $input->setArgument('display-name', $displayName);
        }
        $assignableRoles = $this->config->get('permissions/assignable_roles')->toArray();
        $isAdmin = $input->getOption('admin') ? ['ROLE_ADMIN'] : [];
        $isDeveloper = $input->getOption('developer') ? ['ROLE_DEVELOPER'] : [];
        $roles = $input->getOption('roles') ?? [];
        $roles = array_merge($roles, $isAdmin, $isDeveloper);
        $nonAssignableRoles = array_diff($roles, $assignableRoles);
        $roles = array_intersect($assignableRoles, $roles);
        if (! empty($nonAssignableRoles)) {
            $this->io->warning(sprintf('The role(s) [%s] are non-assignable and will be ignored. Please check your config/permissions.yaml file.', implode(',', $nonAssignableRoles)));
        }
        if (empty($roles)) {
            $rolesQuestion = new ChoiceQuestion('Role', $assignableRoles);
            $roles = [$this->io->askQuestion($rolesQuestion)];
        }
        $input->setOption('roles', $roles);
    }
protected function interact ( InputInterface $ input , OutputInterface $ output ) : void { if ( $ input -> getArgument ( 'username' ) !== null && $ input -> getArgument ( 'password' ) !== null && $ input -> getArgument ( 'email' ) !== null && $ input -> getArgument ( 'display-name' ) !== null ) { return ; } $ this -> io -> title ( 'Add Bolt User Command' ) ; $ this -> io -> text ( [ 'If you prefer to not use this interactive wizard, provide the' , 'arguments required by this command as follows:' , '' , ' $ php bin/console bolt:add-user username password email@example.com DisplayName' , '' , 'Now we\'ll ask you for the value of all the missing command arguments.' , ] ) ; $ username = $ input -> getArgument ( 'username' ) ; if ( $ username !== null ) { $ this -> io -> text ( ' > <info>Username</info>: ' . $ username ) ; } else { $ username = $ this -> io -> ask ( 'Username' , null , function ( ? string $ username ) { $ errors = $ this -> validator -> validatePropertyValue ( User :: class , 'username' , $ username ) ; if ( $ errors -> count ( ) > 0 ) { throw new InvalidArgumentException ( $ errors -> get ( 0 ) -> getMessage ( ) ) ; } return $ username ; } ) ; $ input -> setArgument ( 'username' , $ username ) ; } $ password = $ input -> getArgument ( 'password' ) ; if ( $ password !== null ) { $ this -> io -> text ( ' > <info>Password</info>: ' . str_repeat ( '*' , mb_strlen ( $ password ) ) ) ; } else { $ passwordQuestion = new Question ( 'Password (input is hidden)' , Str :: generatePassword ( ) ) ; $ passwordQuestion -> setHidden ( true ) ; $ passwordQuestion -> setValidator ( function ( ? string $ password ) { $ errors = $ this -> validator -> validatePropertyValue ( User :: class , 'plainPassword' , $ password ) ; if ( $ errors -> count ( ) > 0 ) { throw new InvalidArgumentException ( $ errors -> get ( 0 ) -> getMessage ( ) ) ; } return $ password ; } ) ; $ password = $ this -> io -> askQuestion ( $ passwordQuestion ) ; $ input -> setArgument ( 'password' , $ password ) ; } $ email = $ input -> getArgument ( 'email' ) ; if ( $ email !== null ) { $ this -> io -> text ( ' > <info>Email</info>: ' . $ email ) ; } else { $ email = $ this -> io -> ask ( 'Email' , null , function ( ? string $ email ) { $ errors = $ this -> validator -> validatePropertyValue ( User :: class , 'email' , $ email ) ; if ( $ errors -> count ( ) > 0 ) { throw new InvalidArgumentException ( $ errors -> get ( 0 ) -> getMessage ( ) ) ; } return $ email ; } ) ; $ input -> setArgument ( 'email' , $ email ) ; } $ displayName = $ input -> getArgument ( 'display-name' ) ; if ( $ displayName !== null ) { $ this -> io -> text ( ' > <info>Display Name</info>: ' . $ displayName ) ; } else { $ displayName = $ this -> io -> ask ( 'Display Name' , null , function ( ? string $ displayName ) { $ errors = $ this -> validator -> validatePropertyValue ( User :: class , 'displayName' , $ displayName ) ; if ( $ errors -> count ( ) > 0 ) { throw new InvalidArgumentException ( $ errors -> get ( 0 ) -> getMessage ( ) ) ; } return $ displayName ; } ) ; $ input -> setArgument ( 'display-name' , $ displayName ) ; } $ assignableRoles = $ this -> config -> get ( 'permissions/assignable_roles' ) -> toArray ( ) ; $ isAdmin = $ input -> getOption ( 'admin' ) ? [ 'ROLE_ADMIN' ] : [ ] ; $ isDeveloper = $ input -> getOption ( 'developer' ) ? [ 'ROLE_DEVELOPER' ] : [ ] ; $ roles = $ input -> getOption ( 'roles' ) ?? [ ] ; $ roles = array_merge ( $ roles , $ isAdmin , $ isDeveloper ) ; $ nonAssignableRoles = array_diff ( $ roles , $ assignableRoles ) ; $ roles = array_intersect ( $ assignableRoles , $ roles ) ; if ( ! empty ( $ nonAssignableRoles ) ) { $ this -> io -> warning ( sprintf ( 'The role(s) [%s] are non-assignable and will be ignored. Please check your config/permissions.yaml file.' , implode ( ',' , $ nonAssignableRoles ) ) ) ; } if ( empty ( $ roles ) ) { $ rolesQuestion = new ChoiceQuestion ( 'Role' , $ assignableRoles ) ; $ roles = [ $ this -> io -> askQuestion ( $ rolesQuestion ) ] ; } $ input -> setOption ( 'roles' , $ roles ) ; }
This method is executed after initialize() and before execute().
This method is executed after initialize () and before execute () .
// Ask for the username if it's not defined // Ask for the password if it's not defined // Ask for the email if it's not defined // Ask for the display name if it's not defined // Set the roles for the new user. // Only allow the assignable roles to be assigned. // Ask the roles question, if neither --roles nor --admin is set.