11
11
12
12
namespace Symfony \Bundle \FrameworkBundle \Command ;
13
13
14
- use Symfony \Component \Console \Question \ConfirmationQuestion ;
15
14
use Symfony \Component \Console \Input \InputArgument ;
16
15
use Symfony \Component \Console \Input \InputInterface ;
17
16
use Symfony \Component \Console \Input \InputOption ;
18
17
use Symfony \Component \Console \Output \OutputInterface ;
18
+ use Symfony \Component \Console \Style \SymfonyStyle ;
19
19
use Symfony \Component \Process \PhpExecutableFinder ;
20
20
use Symfony \Component \Process \Process ;
21
21
@@ -74,11 +74,15 @@ protected function configure()
74
74
*/
75
75
protected function execute (InputInterface $ input , OutputInterface $ output )
76
76
{
77
+ $ output = new SymfonyStyle ($ input , $ output );
78
+
77
79
if (!extension_loaded ('pcntl ' )) {
78
- $ output ->writeln ('<error>This command needs the pcntl extension to run.</error> ' );
79
- $ output ->writeln ('You can either install it or use the <info>server:run</info> command instead to run the built-in web server. ' );
80
+ $ output ->error (array (
81
+ 'This command needs the pcntl extension to run. ' ,
82
+ 'You can either install it or use the "server:run" command instead to run the built-in web server. ' ,
83
+ ));
80
84
81
- if ($ this -> getHelper ( ' question ' )-> ask ($ input , $ output , new ConfirmationQuestion ( 'Do you want to start <info>server:run</info> immediately? [Yn] ' , true ) )) {
85
+ if ($ output -> ask ('Do you want to execute <info>server:run</info> immediately? [Yn] ' , true )) {
82
86
$ command = $ this ->getApplication ()->find ('server:run ' );
83
87
84
88
return $ command ->run ($ input , $ output );
@@ -94,7 +98,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
94
98
}
95
99
96
100
if (!is_dir ($ documentRoot )) {
97
- $ output ->writeln (sprintf ('<error> The given document root directory "%s" does not exist</error> ' , $ documentRoot ));
101
+ $ output ->error (sprintf ('The given document root directory "%s" does not exist. ' , $ documentRoot ));
98
102
99
103
return 1 ;
100
104
}
@@ -112,32 +116,34 @@ protected function execute(InputInterface $input, OutputInterface $output)
112
116
}
113
117
114
118
if (!$ input ->getOption ('force ' ) && $ this ->isOtherServerProcessRunning ($ address )) {
115
- $ output ->writeln (sprintf ('<error>A process is already listening on http://%s.</error> ' , $ address ));
116
- $ output ->writeln (sprintf ('<error>Use the --force option if the server process terminated unexpectedly to start a new web server process.</error> ' ));
119
+ $ output ->error (array (
120
+ sprintf ('A process is already listening on http://%s. ' , $ address ),
121
+ 'Use the --force option if the server process terminated unexpectedly to start a new web server process. ' ,
122
+ ));
117
123
118
124
return 1 ;
119
125
}
120
126
121
127
if ('prod ' === $ env ) {
122
- $ output ->writeln ( ' <error> Running PHP built-in server in production environment is NOT recommended!</error> ' );
128
+ $ output ->error ( ' Running PHP built-in server in production environment is NOT recommended! ' );
123
129
}
124
130
125
131
$ pid = pcntl_fork ();
126
132
127
133
if ($ pid < 0 ) {
128
- $ output ->writeln ( ' <error> Unable to start the server process</error> ' );
134
+ $ output ->error ( ' Unable to start the server process. ' );
129
135
130
136
return 1 ;
131
137
}
132
138
133
139
if ($ pid > 0 ) {
134
- $ output ->writeln (sprintf ('<info> Web server listening on http://%s</info> ' , $ address ));
140
+ $ output ->success (sprintf ('Web server listening on http://%s ' , $ address ));
135
141
136
142
return ;
137
143
}
138
144
139
145
if (posix_setsid () < 0 ) {
140
- $ output ->writeln ( ' <error> Unable to set the child process as session leader</error> ' );
146
+ $ output ->error ( ' Unable to set the child process as session leader ' );
141
147
142
148
return 1 ;
143
149
}
@@ -152,7 +158,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
152
158
touch ($ lockFile );
153
159
154
160
if (!$ process ->isRunning ()) {
155
- $ output ->writeln ( ' <error> Unable to start the server process</error> ' );
161
+ $ output ->error ( ' Unable to start the server process ' );
156
162
unlink ($ lockFile );
157
163
158
164
return 1 ;
@@ -172,13 +178,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
172
178
* Determine the absolute file path for the router script, using the environment to choose a standard script
173
179
* if no custom router script is specified.
174
180
*
175
- * @param string|null $router File path of the custom router script, if set by the user; otherwise null
176
- * @param string $env The application environment
177
- * @param OutputInterface $output An OutputInterface instance
181
+ * @param string|null $router File path of the custom router script, if set by the user; otherwise null
182
+ * @param string $env The application environment
183
+ * @param SymfonyStyle $output An SymfonyStyle instance
178
184
*
179
185
* @return string|bool The absolute file path of the router script, or false on failure
180
186
*/
181
- private function determineRouterScript ($ router , $ env , OutputInterface $ output )
187
+ private function determineRouterScript ($ router , $ env , SymfonyStyle $ output )
182
188
{
183
189
if (null === $ router ) {
184
190
$ router = $ this
@@ -189,7 +195,7 @@ private function determineRouterScript($router, $env, OutputInterface $output)
189
195
}
190
196
191
197
if (false === $ path = realpath ($ router )) {
192
- $ output ->writeln (sprintf ('<error> The given router script "%s" does not exist</error> ' , $ router ));
198
+ $ output ->error (sprintf ('The given router script "%s" does not exist. ' , $ router ));
193
199
194
200
return false ;
195
201
}
@@ -200,18 +206,18 @@ private function determineRouterScript($router, $env, OutputInterface $output)
200
206
/**
201
207
* Creates a process to start PHP's built-in web server.
202
208
*
203
- * @param OutputInterface $output A OutputInterface instance
204
- * @param string $address IP address and port to listen to
205
- * @param string $documentRoot The application's document root
206
- * @param string $router The router filename
209
+ * @param SymfonyStyle $output A SymfonyStyle instance
210
+ * @param string $address IP address and port to listen to
211
+ * @param string $documentRoot The application's document root
212
+ * @param string $router The router filename
207
213
*
208
214
* @return Process The process
209
215
*/
210
- private function createServerProcess (OutputInterface $ output , $ address , $ documentRoot , $ router )
216
+ private function createServerProcess (SymfonyStyle $ output , $ address , $ documentRoot , $ router )
211
217
{
212
218
$ finder = new PhpExecutableFinder ();
213
219
if (false === $ binary = $ finder ->find ()) {
214
- $ output ->writeln ( ' <error> Unable to find PHP binary to start server</error> ' );
220
+ $ output ->error ( ' Unable to find PHP binary to start server. ' );
215
221
216
222
return ;
217
223
}
0 commit comments