Skip to content

Commit d65f864

Browse files
committed
feature #15972 [Console] Updated the styles of the server commands (javiereguiluz)
This PR was squashed before being merged into the 2.8 branch (closes #15972). Discussion ---------- [Console] Updated the styles of the server commands | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR uses `comment()` which hasn't been merged yet. WIP PR at #15964 ![server_comparison_1](https://cloud.githubusercontent.com/assets/73419/10139550/a5dc0d70-6603-11e5-8b4c-30cae7f52232.png) ![server_comparison_2](https://cloud.githubusercontent.com/assets/73419/10139552/a82932f6-6603-11e5-9bf5-7d0944a98327.png) Commits ------- 4e2cc0f [Console] Updated the styles of the server commands
2 parents 5322cbb + 4e2cc0f commit d65f864

File tree

4 files changed

+55
-39
lines changed

4 files changed

+55
-39
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Console\Input\InputOption;
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Output\OutputInterface;
18+
use Symfony\Component\Console\Style\SymfonyStyle;
1819
use Symfony\Component\Process\PhpExecutableFinder;
1920
use Symfony\Component\Process\ProcessBuilder;
2021

@@ -84,14 +85,16 @@ protected function configure()
8485
*/
8586
protected function execute(InputInterface $input, OutputInterface $output)
8687
{
88+
$stdout = $output;
89+
$output = new SymfonyStyle($input, $output);
8790
$documentRoot = $input->getOption('docroot');
8891

8992
if (null === $documentRoot) {
9093
$documentRoot = $this->getContainer()->getParameter('kernel.root_dir').'/../web';
9194
}
9295

9396
if (!is_dir($documentRoot)) {
94-
$output->writeln(sprintf('<error>The given document root directory "%s" does not exist</error>', $documentRoot));
97+
$output->error(sprintf('The given document root directory "%s" does not exist', $documentRoot));
9598

9699
return 1;
97100
}
@@ -104,17 +107,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
104107
}
105108

106109
if ($this->isOtherServerProcessRunning($address)) {
107-
$output->writeln(sprintf('<error>A process is already listening on http://%s.</error>', $address));
110+
$output->error(sprintf('A process is already listening on http://%s.', $address));
108111

109112
return 1;
110113
}
111114

112115
if ('prod' === $env) {
113-
$output->writeln('<error>Running PHP built-in server in production environment is NOT recommended!</error>');
116+
$output->error('Running PHP built-in server in production environment is NOT recommended!');
114117
}
115118

116-
$output->writeln(sprintf("Server running on <info>http://%s</info>\n", $address));
117-
$output->writeln('Quit the server with CONTROL-C.');
119+
$output->success(sprintf('Server running on http://%s', $address));
120+
$output->comment('Quit the server with CONTROL-C.');
118121

119122
if (null === $builder = $this->createPhpProcessBuilder($output, $address, $input->getOption('router'), $env)) {
120123
return 1;
@@ -124,26 +127,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
124127
$builder->setTimeout(null);
125128
$process = $builder->getProcess();
126129

127-
if (OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) {
130+
if (OutputInterface::VERBOSITY_VERBOSE > $stdout->getVerbosity()) {
128131
$process->disableOutput();
129132
}
130133

131134
$this
132135
->getHelper('process')
133-
->run($output, $process, null, null, OutputInterface::VERBOSITY_VERBOSE);
136+
->run($stdout, $process, null, null, OutputInterface::VERBOSITY_VERBOSE);
134137

135138
if (!$process->isSuccessful()) {
136-
$output->writeln('<error>Built-in server terminated unexpectedly</error>');
139+
$errorMessages = array('Built-in server terminated unexpectedly.');
137140

138141
if ($process->isOutputDisabled()) {
139-
$output->writeln('<error>Run the command again with -v option for more details</error>');
142+
$errorMessages[] = 'Run the command again with -v option for more details.';
140143
}
144+
145+
$output->error($errorMessages);
141146
}
142147

143148
return $process->getExitCode();
144149
}
145150

146-
private function createPhpProcessBuilder(OutputInterface $output, $address, $router, $env)
151+
private function createPhpProcessBuilder(SymfonyStyle $output, $address, $router, $env)
147152
{
148153
$router = $router ?: $this
149154
->getContainer()
@@ -152,7 +157,7 @@ private function createPhpProcessBuilder(OutputInterface $output, $address, $rou
152157
;
153158

154159
if (!file_exists($router)) {
155-
$output->writeln(sprintf('<error>The given router script "%s" does not exist</error>', $router));
160+
$output->error(sprintf('The given router script "%s" does not exist.', $router));
156161

157162
return;
158163
}
@@ -161,7 +166,7 @@ private function createPhpProcessBuilder(OutputInterface $output, $address, $rou
161166
$finder = new PhpExecutableFinder();
162167

163168
if (false === $binary = $finder->find()) {
164-
$output->writeln('<error>Unable to find PHP binary to run server</error>');
169+
$output->error('Unable to find PHP binary to run server.');
165170

166171
return;
167172
}

src/Symfony/Bundle/FrameworkBundle/Command/ServerStartCommand.php

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

14-
use Symfony\Component\Console\Question\ConfirmationQuestion;
1514
use Symfony\Component\Console\Input\InputArgument;
1615
use Symfony\Component\Console\Input\InputInterface;
1716
use Symfony\Component\Console\Input\InputOption;
1817
use Symfony\Component\Console\Output\OutputInterface;
18+
use Symfony\Component\Console\Style\SymfonyStyle;
1919
use Symfony\Component\Process\PhpExecutableFinder;
2020
use Symfony\Component\Process\Process;
2121

@@ -74,11 +74,15 @@ protected function configure()
7474
*/
7575
protected function execute(InputInterface $input, OutputInterface $output)
7676
{
77+
$output = new SymfonyStyle($input, $output);
78+
7779
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+
));
8084

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)) {
8286
$command = $this->getApplication()->find('server:run');
8387

8488
return $command->run($input, $output);
@@ -94,7 +98,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
9498
}
9599

96100
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));
98102

99103
return 1;
100104
}
@@ -112,32 +116,34 @@ protected function execute(InputInterface $input, OutputInterface $output)
112116
}
113117

114118
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+
));
117123

118124
return 1;
119125
}
120126

121127
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!');
123129
}
124130

125131
$pid = pcntl_fork();
126132

127133
if ($pid < 0) {
128-
$output->writeln('<error>Unable to start the server process</error>');
134+
$output->error('Unable to start the server process.');
129135

130136
return 1;
131137
}
132138

133139
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));
135141

136142
return;
137143
}
138144

139145
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');
141147

142148
return 1;
143149
}
@@ -152,7 +158,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
152158
touch($lockFile);
153159

154160
if (!$process->isRunning()) {
155-
$output->writeln('<error>Unable to start the server process</error>');
161+
$output->error('Unable to start the server process');
156162
unlink($lockFile);
157163

158164
return 1;
@@ -172,13 +178,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
172178
* Determine the absolute file path for the router script, using the environment to choose a standard script
173179
* if no custom router script is specified.
174180
*
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
178184
*
179185
* @return string|bool The absolute file path of the router script, or false on failure
180186
*/
181-
private function determineRouterScript($router, $env, OutputInterface $output)
187+
private function determineRouterScript($router, $env, SymfonyStyle $output)
182188
{
183189
if (null === $router) {
184190
$router = $this
@@ -189,7 +195,7 @@ private function determineRouterScript($router, $env, OutputInterface $output)
189195
}
190196

191197
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));
193199

194200
return false;
195201
}
@@ -200,18 +206,18 @@ private function determineRouterScript($router, $env, OutputInterface $output)
200206
/**
201207
* Creates a process to start PHP's built-in web server.
202208
*
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
207213
*
208214
* @return Process The process
209215
*/
210-
private function createServerProcess(OutputInterface $output, $address, $documentRoot, $router)
216+
private function createServerProcess(SymfonyStyle $output, $address, $documentRoot, $router)
211217
{
212218
$finder = new PhpExecutableFinder();
213219
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.');
215221

216222
return;
217223
}

src/Symfony/Bundle/FrameworkBundle/Command/ServerStatusCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Console\Input\InputArgument;
1515
use Symfony\Component\Console\Input\InputInterface;
1616
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\Console\Style\SymfonyStyle;
1718

1819
/**
1920
* Shows the status of a process that is running PHP's built-in web server in
@@ -42,6 +43,7 @@ protected function configure()
4243
*/
4344
protected function execute(InputInterface $input, OutputInterface $output)
4445
{
46+
$output = new SymfonyStyle($input, $output);
4547
$address = $input->getArgument('address');
4648

4749
// remove an orphaned lock file
@@ -50,9 +52,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
5052
}
5153

5254
if (file_exists($this->getLockFile($address))) {
53-
$output->writeln(sprintf('<info>Web server still listening on http://%s</info>', $address));
55+
$output->success(sprintf('Web server still listening on http://%s', $address));
5456
} else {
55-
$output->writeln(sprintf('<error>No web server is listening on http://%s</error>', $address));
57+
$output->warning(sprintf('No web server is listening on http://%s', $address));
5658
}
5759
}
5860

src/Symfony/Bundle/FrameworkBundle/Command/ServerStopCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Console\Input\InputInterface;
1616
use Symfony\Component\Console\Output\OutputInterface;
1717
use Symfony\Component\Console\Input\InputOption;
18+
use Symfony\Component\Console\Style\SymfonyStyle;
1819

1920
/**
2021
* Stops a background process running PHP's built-in web server.
@@ -54,6 +55,8 @@ protected function configure()
5455
*/
5556
protected function execute(InputInterface $input, OutputInterface $output)
5657
{
58+
$output = new SymfonyStyle($input, $output);
59+
5760
$address = $input->getArgument('address');
5861
if (false === strpos($address, ':')) {
5962
$address = $address.':'.$input->getOption('port');
@@ -62,12 +65,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
6265
$lockFile = $this->getLockFile($address);
6366

6467
if (!file_exists($lockFile)) {
65-
$output->writeln(sprintf('<error>No web server is listening on http://%s</error>', $address));
68+
$output->error(sprintf('No web server is listening on http://%s', $address));
6669

6770
return 1;
6871
}
6972

7073
unlink($lockFile);
71-
$output->writeln(sprintf('<info>Stopped the web server listening on http://%s</info>', $address));
74+
$output->success(sprintf('Stopped the web server listening on http://%s', $address));
7275
}
7376
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy