18
18
use Symfony \Component \Console \Input \InputInterface ;
19
19
use Symfony \Component \Console \Input \InputOption ;
20
20
use Symfony \Component \Console \Output \OutputInterface ;
21
- use Symfony \Component \Console \Question \Question ;
22
21
use Symfony \Component \Console \Style \SymfonyStyle ;
23
22
use Symfony \Component \Security \Core \Encoder \UserPasswordEncoderInterface ;
23
+ use Symfony \Component \Stopwatch \Stopwatch ;
24
24
25
25
/**
26
26
* A command console that creates users and stores them in the database.
@@ -45,6 +45,7 @@ class AddUserCommand extends Command
45
45
{
46
46
const MAX_ATTEMPTS = 5 ;
47
47
48
+ private $ io ;
48
49
private $ entityManager ;
49
50
private $ passwordEncoder ;
50
51
@@ -76,6 +77,18 @@ protected function configure()
76
77
;
77
78
}
78
79
80
+ /**
81
+ * This optional method is the first one executed for a command after configure()
82
+ * and is useful to initialize properties based on the input arguments and options.
83
+ */
84
+ protected function initialize (InputInterface $ input , OutputInterface $ output )
85
+ {
86
+ // SymfonyStyle is an optional feature that Symfony provides so you can
87
+ // apply a consistent look to the commands of your application.
88
+ // See https://symfony.com/doc/current/console/style.html
89
+ $ this ->io = new SymfonyStyle ($ input , $ output );
90
+ }
91
+
79
92
/**
80
93
* This method is executed after initialize() and before execute(). Its purpose
81
94
* is to check if some of the options/arguments are missing and interactively
@@ -92,18 +105,10 @@ protected function interact(InputInterface $input, OutputInterface $output)
92
105
return ;
93
106
}
94
107
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
-
101
- // multi-line messages can be displayed this way...
102
- $ io ->text ('If you prefer to not use this interactive wizard, provide the ' );
103
- $ io ->text ('arguments required by this command as follows: ' );
104
-
105
- // ...but you can also pass an array of strings to the text() method
106
- $ io ->text ([
108
+ $ this ->io ->title ('Add User Command Interactive Wizard ' );
109
+ $ this ->io ->text ([
110
+ 'If you prefer to not use this interactive wizard, provide the ' ,
111
+ 'arguments required by this command as follows: ' ,
107
112
'' ,
108
113
' $ php bin/console app:add-user username password [email protected] ' ,
109
114
'' ,
@@ -112,61 +117,45 @@ protected function interact(InputInterface $input, OutputInterface $output)
112
117
113
118
// Ask for the username if it's not defined
114
119
$ username = $ input ->getArgument ('username ' );
115
- if (null === $ username ) {
116
- $ question = new Question ('Username ' );
117
- $ question ->setValidator (function ($ answer ) {
120
+ if (null !== $ username ) {
121
+ $ this ->io ->text (' > <info>Username</info>: ' .$ username );
122
+ } else {
123
+ $ username = $ this ->io ->ask ('Username ' , null , function ($ answer ) {
118
124
if (empty ($ answer )) {
119
125
throw new \RuntimeException ('The username cannot be empty ' );
120
126
}
121
127
122
128
return $ answer ;
123
129
});
124
- $ question ->setMaxAttempts (self ::MAX_ATTEMPTS );
125
130
126
- $ username = $ io ->askQuestion ($ question );
127
131
$ input ->setArgument ('username ' , $ username );
128
- } else {
129
- $ io ->text (' > <info>Username</info>: ' .$ username );
130
132
}
131
133
132
134
// Ask for the password if it's not defined
133
135
$ password = $ input ->getArgument ('password ' );
134
- if (null === $ password ) {
135
- $ question = new Question ('Password (your type will be hidden) ' );
136
- $ question ->setValidator ([$ this , 'passwordValidator ' ]);
137
- $ question ->setHidden (true );
138
- $ question ->setMaxAttempts (self ::MAX_ATTEMPTS );
139
-
140
- $ password = $ io ->askQuestion ($ question );
141
- $ input ->setArgument ('password ' , $ password );
136
+ if (null !== $ password ) {
137
+ $ this ->io ->text (' > <info>Password</info>: ' .str_repeat ('* ' , mb_strlen ($ password )));
142
138
} else {
143
- $ io ->text (' > <info>Password</info>: ' .str_repeat ('* ' , mb_strlen ($ password )));
139
+ $ password = $ this ->io ->askHidden ('Password (your type will be hidden) ' , null , [$ this , 'passwordValidator ' ]);
140
+ $ input ->setArgument ('password ' , $ password );
144
141
}
145
142
146
143
// Ask for the email if it's not defined
147
144
$ email = $ input ->getArgument ('email ' );
148
- if (null === $ email ) {
149
- $ question = new Question ('Email ' );
150
- $ question ->setValidator ([$ this , 'emailValidator ' ]);
151
- $ question ->setMaxAttempts (self ::MAX_ATTEMPTS );
152
-
153
- $ email = $ io ->askQuestion ($ question );
154
- $ input ->setArgument ('email ' , $ email );
145
+ if (null !== $ email ) {
146
+ $ this ->io ->text (' > <info>Email</info>: ' .$ email );
155
147
} else {
156
- $ io ->text (' > <info>Email</info>: ' .$ email );
148
+ $ email = $ this ->io ->ask ('Email ' , null , [$ this , 'emailValidator ' ]);
149
+ $ input ->setArgument ('email ' , $ email );
157
150
}
158
151
159
152
// Ask for the full name if it's not defined
160
153
$ fullName = $ input ->getArgument ('full-name ' );
161
- if (null === $ fullName ) {
162
- $ question = new Question ('Full Name ' );
163
- $ question ->setValidator ([$ this , 'fullNameValidator ' ]);
164
- $ question ->setMaxAttempts (self ::MAX_ATTEMPTS );
165
-
166
- $ fullName = $ io ->askQuestion ($ question );
167
- $ input ->setArgument ('full-name ' , $ fullName );
154
+ if (null !== $ fullName ) {
155
+ $ this ->io ->text (' > <info>Full Name</info>: ' .$ fullName );
168
156
} else {
169
- $ io ->text (' > <info>Full Name</info>: ' .$ fullName );
157
+ $ fullName = $ this ->io ->ask ('Full Name ' , null , [$ this , 'fullNameValidator ' ]);
158
+ $ input ->setArgument ('full-name ' , $ fullName );
170
159
}
171
160
}
172
161
@@ -176,8 +165,8 @@ protected function interact(InputInterface $input, OutputInterface $output)
176
165
*/
177
166
protected function execute (InputInterface $ input , OutputInterface $ output )
178
167
{
179
- $ startTime = microtime ( true );
180
- $ io = new SymfonyStyle ( $ input , $ output );
168
+ $ stopwatch = new Stopwatch ( );
169
+ $ stopwatch -> start ( ' add-user-command ' );
181
170
182
171
$ username = $ input ->getArgument ('username ' );
183
172
$ plainPassword = $ input ->getArgument ('password ' );
@@ -202,13 +191,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
202
191
$ this ->entityManager ->persist ($ user );
203
192
$ this ->entityManager ->flush ();
204
193
205
- $ io ->success (sprintf ('%s was successfully created: %s (%s) ' , $ isAdmin ? 'Administrator user ' : 'User ' , $ user ->getUsername (), $ user ->getEmail ()));
194
+ $ this -> io ->success (sprintf ('%s was successfully created: %s (%s) ' , $ isAdmin ? 'Administrator user ' : 'User ' , $ user ->getUsername (), $ user ->getEmail ()));
206
195
196
+ $ event = $ stopwatch ->stop ('add-user-command ' );
207
197
if ($ output ->isVerbose ()) {
208
- $ finishTime = microtime (true );
209
- $ elapsedTime = $ finishTime - $ startTime ;
210
-
211
- $ io ->note (sprintf ('New user database id: %d / Elapsed time: %.2f ms ' , $ user ->getId (), $ elapsedTime * 1000 ));
198
+ $ this ->io ->comment (sprintf ('New user database id: %d / Elapsed time: %.2f ms / Consumed memory: %.2f MB ' , $ user ->getId (), $ event ->getDuration (), $ event ->getMemory () / pow (1024 , 2 )));
212
199
}
213
200
}
214
201
0 commit comments