diff --git a/src/AppBundle/Command/AddUserCommand.php b/src/AppBundle/Command/AddUserCommand.php index 970d1b1d7..d263434fe 100644 --- a/src/AppBundle/Command/AddUserCommand.php +++ b/src/AppBundle/Command/AddUserCommand.php @@ -19,6 +19,7 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\Question; +use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; /** @@ -91,34 +92,28 @@ protected function interact(InputInterface $input, OutputInterface $output) return; } + // See: http://symfony.com/doc/current/console/style.html + $io = new SymfonyStyle($input, $output); + + // Use the title() method to display the title + $io->title('Add User Command Interactive Wizard'); + // multi-line messages can be displayed this way... - $output->writeln(''); - $output->writeln('Add User Command Interactive Wizard'); - $output->writeln('-----------------------------------'); + $io->text('If you prefer to not use this interactive wizard, provide the'); + $io->text('arguments required by this command as follows:'); - // ...but you can also pass an array of strings to the writeln() method - $output->writeln([ - '', - 'If you prefer to not use this interactive wizard, provide the', - 'arguments required by this command as follows:', + // ...but you can also pass an array of strings to the text() method + $io->text([ '', ' $ php bin/console app:add-user username password email@example.com', '', - ]); - - $output->writeln([ - '', 'Now we\'ll ask you for the value of all the missing command arguments.', - '', ]); - // See https://symfony.com/doc/current/components/console/helpers/questionhelper.html - $console = $this->getHelper('question'); - // Ask for the username if it's not defined $username = $input->getArgument('username'); if (null === $username) { - $question = new Question(' > Username: '); + $question = new Question('Username'); $question->setValidator(function ($answer) { if (empty($answer)) { throw new \RuntimeException('The username cannot be empty'); @@ -128,50 +123,50 @@ protected function interact(InputInterface $input, OutputInterface $output) }); $question->setMaxAttempts(self::MAX_ATTEMPTS); - $username = $console->ask($input, $output, $question); + $username = $io->askQuestion($question); $input->setArgument('username', $username); } else { - $output->writeln(' > Username: '.$username); + $io->text(' > Username: '.$username); } // Ask for the password if it's not defined $password = $input->getArgument('password'); if (null === $password) { - $question = new Question(' > Password (your type will be hidden): '); + $question = new Question('Password (your type will be hidden)'); $question->setValidator([$this, 'passwordValidator']); $question->setHidden(true); $question->setMaxAttempts(self::MAX_ATTEMPTS); - $password = $console->ask($input, $output, $question); + $password = $io->askQuestion($question); $input->setArgument('password', $password); } else { - $output->writeln(' > Password: '.str_repeat('*', mb_strlen($password))); + $io->text(' > Password: '.str_repeat('*', mb_strlen($password))); } // Ask for the email if it's not defined $email = $input->getArgument('email'); if (null === $email) { - $question = new Question(' > Email: '); + $question = new Question('Email'); $question->setValidator([$this, 'emailValidator']); $question->setMaxAttempts(self::MAX_ATTEMPTS); - $email = $console->ask($input, $output, $question); + $email = $io->askQuestion($question); $input->setArgument('email', $email); } else { - $output->writeln(' > Email: '.$email); + $io->text(' > Email: '.$email); } // Ask for the full name if it's not defined $fullName = $input->getArgument('full-name'); if (null === $fullName) { - $question = new Question(' > Full Name: '); + $question = new Question('Full Name'); $question->setValidator([$this, 'fullNameValidator']); $question->setMaxAttempts(self::MAX_ATTEMPTS); - $fullName = $console->ask($input, $output, $question); + $fullName = $io->askQuestion($question); $input->setArgument('full-name', $fullName); } else { - $output->writeln(' > Full Name: '.$fullName); + $io->text(' > Full Name: '.$fullName); } } @@ -182,6 +177,7 @@ protected function interact(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output) { $startTime = microtime(true); + $io = new SymfonyStyle($input, $output); $username = $input->getArgument('username'); $plainPassword = $input->getArgument('password'); @@ -206,14 +202,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->entityManager->persist($user); $this->entityManager->flush(); - $output->writeln(''); - $output->writeln(sprintf('[OK] %s was successfully created: %s (%s)', $isAdmin ? 'Administrator user' : 'User', $user->getUsername(), $user->getEmail())); + $io->success(sprintf('%s was successfully created: %s (%s)', $isAdmin ? 'Administrator user' : 'User', $user->getUsername(), $user->getEmail())); if ($output->isVerbose()) { $finishTime = microtime(true); $elapsedTime = $finishTime - $startTime; - $output->writeln(sprintf('[INFO] New user database id: %d / Elapsed time: %.2f ms', $user->getId(), $elapsedTime * 1000)); + $io->note(sprintf('New user database id: %d / Elapsed time: %.2f ms', $user->getId(), $elapsedTime * 1000)); } } diff --git a/src/AppBundle/Command/DeleteUserCommand.php b/src/AppBundle/Command/DeleteUserCommand.php index 9e3c8eaf7..060357908 100644 --- a/src/AppBundle/Command/DeleteUserCommand.php +++ b/src/AppBundle/Command/DeleteUserCommand.php @@ -17,7 +17,7 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Question\Question; +use Symfony\Component\Console\Style\SymfonyStyle; /** * A command console that deletes users from the database. @@ -77,32 +77,23 @@ protected function interact(InputInterface $input, OutputInterface $output) return; } - $output->writeln(''); - $output->writeln('Delete User Command Interactive Wizard'); - $output->writeln('-----------------------------------'); + // See: http://symfony.com/doc/current/console/style.html + $io = new SymfonyStyle($input, $output); - $output->writeln([ - '', + $io->title('Delete User Command Interactive Wizard'); + + $io->text([ 'If you prefer to not use this interactive wizard, provide the', 'arguments required by this command as follows:', '', ' $ php bin/console app:delete-user username', '', - ]); - - $output->writeln([ - '', 'Now we\'ll ask you for the value of all the missing command arguments.', '', ]); - $helper = $this->getHelper('question'); - - $question = new Question(' > Username: '); - $question->setValidator([$this, 'usernameValidator']); - $question->setMaxAttempts(self::MAX_ATTEMPTS); + $username = $io->ask('Username', null, [$this, 'usernameValidator']); - $username = $helper->ask($input, $output, $question); $input->setArgument('username', $username); } @@ -127,8 +118,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->entityManager->remove($user); $this->entityManager->flush(); - $output->writeln(''); - $output->writeln(sprintf('[OK] User "%s" (ID: %d, email: %s) was successfully deleted.', $user->getUsername(), $userId, $user->getEmail())); + (new SymfonyStyle($input, $output)) + ->success(sprintf('User "%s" (ID: %d, email: %s) was successfully deleted.', $user->getUsername(), $userId, $user->getEmail())); } /** diff --git a/src/AppBundle/Command/ListUsersCommand.php b/src/AppBundle/Command/ListUsersCommand.php index f19791762..11e699088 100644 --- a/src/AppBundle/Command/ListUsersCommand.php +++ b/src/AppBundle/Command/ListUsersCommand.php @@ -110,7 +110,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $table ->setHeaders(['ID', 'Full Name', 'Username', 'Email', 'Roles']) ->setRows($usersAsPlainArrays) - ; + ->setStyle(clone Table::getStyleDefinition('symfony-style-guide')) + ; $table->render(); // instead of displaying the table of users, store it in a variable