Skip to content

Commit

Permalink
[Console] Updated the styles of the server commands
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz authored and fabpot committed Sep 28, 2015
1 parent c2cfbcf commit 4e2cc0f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 39 deletions.
29 changes: 17 additions & 12 deletions src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\ProcessBuilder;

Expand Down Expand Up @@ -84,14 +85,16 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$stdout = $output;
$output = new SymfonyStyle($input, $output);
$documentRoot = $input->getOption('docroot');

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

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

return 1;
}
Expand All @@ -104,17 +107,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

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

return 1;
}

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

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

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

if (OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) {
if (OutputInterface::VERBOSITY_VERBOSE > $stdout->getVerbosity()) {
$process->disableOutput();
}

$this
->getHelper('process')
->run($output, $process, null, null, OutputInterface::VERBOSITY_VERBOSE);
->run($stdout, $process, null, null, OutputInterface::VERBOSITY_VERBOSE);

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

if ($process->isOutputDisabled()) {
$output->writeln('<error>Run the command again with -v option for more details</error>');
$errorMessages[] = 'Run the command again with -v option for more details.';
}

$output->error($errorMessages);
}

return $process->getExitCode();
}

private function createPhpProcessBuilder(OutputInterface $output, $address, $router, $env)
private function createPhpProcessBuilder(SymfonyStyle $output, $address, $router, $env)
{
$router = $router ?: $this
->getContainer()
Expand All @@ -152,7 +157,7 @@ private function createPhpProcessBuilder(OutputInterface $output, $address, $rou
;

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

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

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

return;
}
Expand Down
52 changes: 29 additions & 23 deletions src/Symfony/Bundle/FrameworkBundle/Command/ServerStartCommand.php
Expand Up @@ -11,11 +11,11 @@

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -74,11 +74,15 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output = new SymfonyStyle($input, $output);

if (!extension_loaded('pcntl')) {
$output->writeln('<error>This command needs the pcntl extension to run.</error>');
$output->writeln('You can either install it or use the <info>server:run</info> command instead to run the built-in web server.');
$output->error(array(
'This command needs the pcntl extension to run.',
'You can either install it or use the "server:run" command instead to run the built-in web server.',
));

if ($this->getHelper('question')->ask($input, $output, new ConfirmationQuestion('Do you want to start <info>server:run</info> immediately? [Yn] ', true))) {
if ($output->ask('Do you want to execute <info>server:run</info> immediately? [Yn] ', true)) {
$command = $this->getApplication()->find('server:run');

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

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

return 1;
}
Expand All @@ -112,32 +116,34 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

if (!$input->getOption('force') && $this->isOtherServerProcessRunning($address)) {
$output->writeln(sprintf('<error>A process is already listening on http://%s.</error>', $address));
$output->writeln(sprintf('<error>Use the --force option if the server process terminated unexpectedly to start a new web server process.</error>'));
$output->error(array(
sprintf('A process is already listening on http://%s.', $address),
'Use the --force option if the server process terminated unexpectedly to start a new web server process.',
));

return 1;
}

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

$pid = pcntl_fork();

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

return 1;
}

if ($pid > 0) {
$output->writeln(sprintf('<info>Web server listening on http://%s</info>', $address));
$output->success(sprintf('Web server listening on http://%s', $address));

return;
}

if (posix_setsid() < 0) {
$output->writeln('<error>Unable to set the child process as session leader</error>');
$output->error('Unable to set the child process as session leader');

return 1;
}
Expand All @@ -152,7 +158,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
touch($lockFile);

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

return 1;
Expand All @@ -172,13 +178,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
* Determine the absolute file path for the router script, using the environment to choose a standard script
* if no custom router script is specified.
*
* @param string|null $router File path of the custom router script, if set by the user; otherwise null
* @param string $env The application environment
* @param OutputInterface $output An OutputInterface instance
* @param string|null $router File path of the custom router script, if set by the user; otherwise null
* @param string $env The application environment
* @param SymfonyStyle $output An SymfonyStyle instance
*
* @return string|bool The absolute file path of the router script, or false on failure
*/
private function determineRouterScript($router, $env, OutputInterface $output)
private function determineRouterScript($router, $env, SymfonyStyle $output)
{
if (null === $router) {
$router = $this
Expand All @@ -189,7 +195,7 @@ private function determineRouterScript($router, $env, OutputInterface $output)
}

if (false === $path = realpath($router)) {
$output->writeln(sprintf('<error>The given router script "%s" does not exist</error>', $router));
$output->error(sprintf('The given router script "%s" does not exist.', $router));

return false;
}
Expand All @@ -200,18 +206,18 @@ private function determineRouterScript($router, $env, OutputInterface $output)
/**
* Creates a process to start PHP's built-in web server.
*
* @param OutputInterface $output A OutputInterface instance
* @param string $address IP address and port to listen to
* @param string $documentRoot The application's document root
* @param string $router The router filename
* @param SymfonyStyle $output A SymfonyStyle instance
* @param string $address IP address and port to listen to
* @param string $documentRoot The application's document root
* @param string $router The router filename
*
* @return Process The process
*/
private function createServerProcess(OutputInterface $output, $address, $documentRoot, $router)
private function createServerProcess(SymfonyStyle $output, $address, $documentRoot, $router)
{
$finder = new PhpExecutableFinder();
if (false === $binary = $finder->find()) {
$output->writeln('<error>Unable to find PHP binary to start server</error>');
$output->error('Unable to find PHP binary to start server.');

return;
}
Expand Down
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

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

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

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

Expand Down
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* Stops a background process running PHP's built-in web server.
Expand Down Expand Up @@ -54,6 +55,8 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output = new SymfonyStyle($input, $output);

$address = $input->getArgument('address');
if (false === strpos($address, ':')) {
$address = $address.':'.$input->getOption('port');
Expand All @@ -62,12 +65,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
$lockFile = $this->getLockFile($address);

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

return 1;
}

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

0 comments on commit 4e2cc0f

Please sign in to comment.