Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 26 additions & 31 deletions src/AppBundle/Command/AddUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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(' > <info>Username</info>: ');
$question = new Question('Username');
$question->setValidator(function ($answer) {
if (empty($answer)) {
throw new \RuntimeException('The username cannot be empty');
Expand All @@ -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(' > <info>Username</info>: '.$username);
$io->text(' > <info>Username</info>: '.$username);
}

// Ask for the password if it's not defined
$password = $input->getArgument('password');
if (null === $password) {
$question = new Question(' > <info>Password</info> (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(' > <info>Password</info>: '.str_repeat('*', mb_strlen($password)));
$io->text(' > <info>Password</info>: '.str_repeat('*', mb_strlen($password)));
}

// Ask for the email if it's not defined
$email = $input->getArgument('email');
if (null === $email) {
$question = new Question(' > <info>Email</info>: ');
$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(' > <info>Email</info>: '.$email);
$io->text(' > <info>Email</info>: '.$email);
}

// Ask for the full name if it's not defined
$fullName = $input->getArgument('full-name');
if (null === $fullName) {
$question = new Question(' > <info>Full Name</info>: ');
$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(' > <info>Full Name</info>: '.$fullName);
$io->text(' > <info>Full Name</info>: '.$fullName);
}
}

Expand All @@ -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');
Expand All @@ -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));
}
}

Expand Down
27 changes: 9 additions & 18 deletions src/AppBundle/Command/DeleteUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(' > <info>Username</info>: ');
$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);
}

Expand All @@ -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()));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/AppBundle/Command/ListUsersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down