19
19
use Symfony \Component \Console \Input \InputOption ;
20
20
use Symfony \Component \Console \Output \OutputInterface ;
21
21
use Symfony \Component \Console \Question \Question ;
22
+ use Symfony \Component \Console \Style \SymfonyStyle ;
22
23
use Symfony \Component \Security \Core \Encoder \UserPasswordEncoderInterface ;
23
24
24
25
/**
@@ -91,34 +92,28 @@ protected function interact(InputInterface $input, OutputInterface $output)
91
92
return ;
92
93
}
93
94
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
+
94
101
// 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: ' );
98
104
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 ([
104
107
'' ,
105
108
' $ php bin/console app:add-user username password [email protected] ' ,
106
109
'' ,
107
- ]);
108
-
109
- $ output ->writeln ([
110
- '' ,
111
110
'Now we \'ll ask you for the value of all the missing command arguments. ' ,
112
- '' ,
113
111
]);
114
112
115
- // See https://symfony.com/doc/current/components/console/helpers/questionhelper.html
116
- $ console = $ this ->getHelper ('question ' );
117
-
118
113
// Ask for the username if it's not defined
119
114
$ username = $ input ->getArgument ('username ' );
120
115
if (null === $ username ) {
121
- $ question = new Question (' > <info> Username</info>: ' );
116
+ $ question = new Question ('Username ' );
122
117
$ question ->setValidator (function ($ answer ) {
123
118
if (empty ($ answer )) {
124
119
throw new \RuntimeException ('The username cannot be empty ' );
@@ -128,50 +123,50 @@ protected function interact(InputInterface $input, OutputInterface $output)
128
123
});
129
124
$ question ->setMaxAttempts (self ::MAX_ATTEMPTS );
130
125
131
- $ username = $ console -> ask ( $ input , $ output , $ question );
126
+ $ username = $ io -> askQuestion ( $ question );
132
127
$ input ->setArgument ('username ' , $ username );
133
128
} else {
134
- $ output -> writeln (' > <info>Username</info>: ' .$ username );
129
+ $ io -> text (' > <info>Username</info>: ' .$ username );
135
130
}
136
131
137
132
// Ask for the password if it's not defined
138
133
$ password = $ input ->getArgument ('password ' );
139
134
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) ' );
141
136
$ question ->setValidator ([$ this , 'passwordValidator ' ]);
142
137
$ question ->setHidden (true );
143
138
$ question ->setMaxAttempts (self ::MAX_ATTEMPTS );
144
139
145
- $ password = $ console -> ask ( $ input , $ output , $ question );
140
+ $ password = $ io -> askQuestion ( $ question );
146
141
$ input ->setArgument ('password ' , $ password );
147
142
} else {
148
- $ output -> writeln (' > <info>Password</info>: ' .str_repeat ('* ' , mb_strlen ($ password )));
143
+ $ io -> text (' > <info>Password</info>: ' .str_repeat ('* ' , mb_strlen ($ password )));
149
144
}
150
145
151
146
// Ask for the email if it's not defined
152
147
$ email = $ input ->getArgument ('email ' );
153
148
if (null === $ email ) {
154
- $ question = new Question (' > <info> Email</info>: ' );
149
+ $ question = new Question ('Email ' );
155
150
$ question ->setValidator ([$ this , 'emailValidator ' ]);
156
151
$ question ->setMaxAttempts (self ::MAX_ATTEMPTS );
157
152
158
- $ email = $ console -> ask ( $ input , $ output , $ question );
153
+ $ email = $ io -> askQuestion ( $ question );
159
154
$ input ->setArgument ('email ' , $ email );
160
155
} else {
161
- $ output -> writeln (' > <info>Email</info>: ' .$ email );
156
+ $ io -> text (' > <info>Email</info>: ' .$ email );
162
157
}
163
158
164
159
// Ask for the full name if it's not defined
165
160
$ fullName = $ input ->getArgument ('full-name ' );
166
161
if (null === $ fullName ) {
167
- $ question = new Question (' > <info> Full Name</info>: ' );
162
+ $ question = new Question ('Full Name ' );
168
163
$ question ->setValidator ([$ this , 'fullNameValidator ' ]);
169
164
$ question ->setMaxAttempts (self ::MAX_ATTEMPTS );
170
165
171
- $ fullName = $ console -> ask ( $ input , $ output , $ question );
166
+ $ fullName = $ io -> askQuestion ( $ question );
172
167
$ input ->setArgument ('full-name ' , $ fullName );
173
168
} else {
174
- $ output -> writeln (' > <info>Full Name</info>: ' .$ fullName );
169
+ $ io -> text (' > <info>Full Name</info>: ' .$ fullName );
175
170
}
176
171
}
177
172
@@ -182,6 +177,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
182
177
protected function execute (InputInterface $ input , OutputInterface $ output )
183
178
{
184
179
$ startTime = microtime (true );
180
+ $ io = new SymfonyStyle ($ input , $ output );
185
181
186
182
$ username = $ input ->getArgument ('username ' );
187
183
$ plainPassword = $ input ->getArgument ('password ' );
@@ -206,14 +202,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
206
202
$ this ->entityManager ->persist ($ user );
207
203
$ this ->entityManager ->flush ();
208
204
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 ()));
211
206
212
207
if ($ output ->isVerbose ()) {
213
208
$ finishTime = microtime (true );
214
209
$ elapsedTime = $ finishTime - $ startTime ;
215
210
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 ));
217
212
}
218
213
}
219
214
0 commit comments