Skip to content

Commit 515c00e

Browse files
committed
Merge branch 'pull/566'
2 parents 65d0665 + a2a6831 commit 515c00e

File tree

3 files changed

+37
-50
lines changed

3 files changed

+37
-50
lines changed

src/AppBundle/Command/AddUserCommand.php

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Console\Input\InputOption;
2020
use Symfony\Component\Console\Output\OutputInterface;
2121
use Symfony\Component\Console\Question\Question;
22+
use Symfony\Component\Console\Style\SymfonyStyle;
2223
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
2324

2425
/**
@@ -91,34 +92,28 @@ protected function interact(InputInterface $input, OutputInterface $output)
9192
return;
9293
}
9394

95+
// See: http://symfony.com/doc/current/console/style.html
96+
$io = new SymfonyStyle($input, $output);
97+
98+
// Use the title() method to display the title
99+
$io->title('Add User Command Interactive Wizard');
100+
94101
// multi-line messages can be displayed this way...
95-
$output->writeln('');
96-
$output->writeln('Add User Command Interactive Wizard');
97-
$output->writeln('-----------------------------------');
102+
$io->text('If you prefer to not use this interactive wizard, provide the');
103+
$io->text('arguments required by this command as follows:');
98104

99-
// ...but you can also pass an array of strings to the writeln() method
100-
$output->writeln([
101-
'',
102-
'If you prefer to not use this interactive wizard, provide the',
103-
'arguments required by this command as follows:',
105+
// ...but you can also pass an array of strings to the text() method
106+
$io->text([
104107
'',
105108
' $ php bin/console app:add-user username password [email protected]',
106109
'',
107-
]);
108-
109-
$output->writeln([
110-
'',
111110
'Now we\'ll ask you for the value of all the missing command arguments.',
112-
'',
113111
]);
114112

115-
// See https://symfony.com/doc/current/components/console/helpers/questionhelper.html
116-
$console = $this->getHelper('question');
117-
118113
// Ask for the username if it's not defined
119114
$username = $input->getArgument('username');
120115
if (null === $username) {
121-
$question = new Question(' > <info>Username</info>: ');
116+
$question = new Question('Username');
122117
$question->setValidator(function ($answer) {
123118
if (empty($answer)) {
124119
throw new \RuntimeException('The username cannot be empty');
@@ -128,50 +123,50 @@ protected function interact(InputInterface $input, OutputInterface $output)
128123
});
129124
$question->setMaxAttempts(self::MAX_ATTEMPTS);
130125

131-
$username = $console->ask($input, $output, $question);
126+
$username = $io->askQuestion($question);
132127
$input->setArgument('username', $username);
133128
} else {
134-
$output->writeln(' > <info>Username</info>: '.$username);
129+
$io->text(' > <info>Username</info>: '.$username);
135130
}
136131

137132
// Ask for the password if it's not defined
138133
$password = $input->getArgument('password');
139134
if (null === $password) {
140-
$question = new Question(' > <info>Password</info> (your type will be hidden): ');
135+
$question = new Question('Password (your type will be hidden)');
141136
$question->setValidator([$this, 'passwordValidator']);
142137
$question->setHidden(true);
143138
$question->setMaxAttempts(self::MAX_ATTEMPTS);
144139

145-
$password = $console->ask($input, $output, $question);
140+
$password = $io->askQuestion($question);
146141
$input->setArgument('password', $password);
147142
} else {
148-
$output->writeln(' > <info>Password</info>: '.str_repeat('*', mb_strlen($password)));
143+
$io->text(' > <info>Password</info>: '.str_repeat('*', mb_strlen($password)));
149144
}
150145

151146
// Ask for the email if it's not defined
152147
$email = $input->getArgument('email');
153148
if (null === $email) {
154-
$question = new Question(' > <info>Email</info>: ');
149+
$question = new Question('Email');
155150
$question->setValidator([$this, 'emailValidator']);
156151
$question->setMaxAttempts(self::MAX_ATTEMPTS);
157152

158-
$email = $console->ask($input, $output, $question);
153+
$email = $io->askQuestion($question);
159154
$input->setArgument('email', $email);
160155
} else {
161-
$output->writeln(' > <info>Email</info>: '.$email);
156+
$io->text(' > <info>Email</info>: '.$email);
162157
}
163158

164159
// Ask for the full name if it's not defined
165160
$fullName = $input->getArgument('full-name');
166161
if (null === $fullName) {
167-
$question = new Question(' > <info>Full Name</info>: ');
162+
$question = new Question('Full Name');
168163
$question->setValidator([$this, 'fullNameValidator']);
169164
$question->setMaxAttempts(self::MAX_ATTEMPTS);
170165

171-
$fullName = $console->ask($input, $output, $question);
166+
$fullName = $io->askQuestion($question);
172167
$input->setArgument('full-name', $fullName);
173168
} else {
174-
$output->writeln(' > <info>Full Name</info>: '.$fullName);
169+
$io->text(' > <info>Full Name</info>: '.$fullName);
175170
}
176171
}
177172

@@ -182,6 +177,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
182177
protected function execute(InputInterface $input, OutputInterface $output)
183178
{
184179
$startTime = microtime(true);
180+
$io = new SymfonyStyle($input, $output);
185181

186182
$username = $input->getArgument('username');
187183
$plainPassword = $input->getArgument('password');
@@ -206,14 +202,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
206202
$this->entityManager->persist($user);
207203
$this->entityManager->flush();
208204

209-
$output->writeln('');
210-
$output->writeln(sprintf('[OK] %s was successfully created: %s (%s)', $isAdmin ? 'Administrator user' : 'User', $user->getUsername(), $user->getEmail()));
205+
$io->success(sprintf('%s was successfully created: %s (%s)', $isAdmin ? 'Administrator user' : 'User', $user->getUsername(), $user->getEmail()));
211206

212207
if ($output->isVerbose()) {
213208
$finishTime = microtime(true);
214209
$elapsedTime = $finishTime - $startTime;
215210

216-
$output->writeln(sprintf('[INFO] New user database id: %d / Elapsed time: %.2f ms', $user->getId(), $elapsedTime * 1000));
211+
$io->note(sprintf('New user database id: %d / Elapsed time: %.2f ms', $user->getId(), $elapsedTime * 1000));
217212
}
218213
}
219214

src/AppBundle/Command/DeleteUserCommand.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Component\Console\Input\InputArgument;
1818
use Symfony\Component\Console\Input\InputInterface;
1919
use Symfony\Component\Console\Output\OutputInterface;
20-
use Symfony\Component\Console\Question\Question;
20+
use Symfony\Component\Console\Style\SymfonyStyle;
2121

2222
/**
2323
* A command console that deletes users from the database.
@@ -77,32 +77,23 @@ protected function interact(InputInterface $input, OutputInterface $output)
7777
return;
7878
}
7979

80-
$output->writeln('');
81-
$output->writeln('Delete User Command Interactive Wizard');
82-
$output->writeln('-----------------------------------');
80+
// See: http://symfony.com/doc/current/console/style.html
81+
$io = new SymfonyStyle($input, $output);
8382

84-
$output->writeln([
85-
'',
83+
$io->title('Delete User Command Interactive Wizard');
84+
85+
$io->text([
8686
'If you prefer to not use this interactive wizard, provide the',
8787
'arguments required by this command as follows:',
8888
'',
8989
' $ php bin/console app:delete-user username',
9090
'',
91-
]);
92-
93-
$output->writeln([
94-
'',
9591
'Now we\'ll ask you for the value of all the missing command arguments.',
9692
'',
9793
]);
9894

99-
$helper = $this->getHelper('question');
100-
101-
$question = new Question(' > <info>Username</info>: ');
102-
$question->setValidator([$this, 'usernameValidator']);
103-
$question->setMaxAttempts(self::MAX_ATTEMPTS);
95+
$username = $io->ask('Username', null, [$this, 'usernameValidator']);
10496

105-
$username = $helper->ask($input, $output, $question);
10697
$input->setArgument('username', $username);
10798
}
10899

@@ -127,8 +118,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
127118
$this->entityManager->remove($user);
128119
$this->entityManager->flush();
129120

130-
$output->writeln('');
131-
$output->writeln(sprintf('[OK] User "%s" (ID: %d, email: %s) was successfully deleted.', $user->getUsername(), $userId, $user->getEmail()));
121+
(new SymfonyStyle($input, $output))
122+
->success(sprintf('User "%s" (ID: %d, email: %s) was successfully deleted.', $user->getUsername(), $userId, $user->getEmail()));
132123
}
133124

134125
/**

src/AppBundle/Command/ListUsersCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
110110
$table
111111
->setHeaders(['ID', 'Full Name', 'Username', 'Email', 'Roles'])
112112
->setRows($usersAsPlainArrays)
113-
;
113+
->setStyle(clone Table::getStyleDefinition('symfony-style-guide'))
114+
;
114115
$table->render();
115116

116117
// instead of displaying the table of users, store it in a variable

0 commit comments

Comments
 (0)