1919use Symfony \Component \Console \Input \InputOption ;
2020use Symfony \Component \Console \Output \OutputInterface ;
2121use Symfony \Component \Console \Question \Question ;
22+ use Symfony \Component \Console \Style \SymfonyStyle ;
2223use 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
0 commit comments