diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php index 44ebdeb36e..767a935286 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php @@ -15,6 +15,7 @@ use Symfony\Component\Console\Input\InputArgument; 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; @@ -84,6 +85,8 @@ EOF */ protected function execute(InputInterface $input, OutputInterface $output) { + $stdout = $output; + $output = new SymfonyStyle($input, $output); $documentRoot = $input->getOption('docroot'); if (null === $documentRoot) { @@ -91,7 +94,7 @@ EOF } if (!is_dir($documentRoot)) { - $output->writeln(sprintf('The given document root directory "%s" does not exist', $documentRoot)); + $output->error(sprintf('The given document root directory "%s" does not exist', $documentRoot)); return 1; } @@ -104,17 +107,17 @@ EOF } if ($this->isOtherServerProcessRunning($address)) { - $output->writeln(sprintf('A process is already listening on http://%s.', $address)); + $output->error(sprintf('A process is already listening on http://%s.', $address)); return 1; } if ('prod' === $env) { - $output->writeln('Running PHP built-in server in production environment is NOT recommended!'); + $output->error('Running PHP built-in server in production environment is NOT recommended!'); } - $output->writeln(sprintf("Server running on http://%s\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; @@ -124,26 +127,28 @@ EOF $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('Built-in server terminated unexpectedly'); + $errorMessages = array('Built-in server terminated unexpectedly.'); if ($process->isOutputDisabled()) { - $output->writeln('Run the command again with -v option for more details'); + $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() @@ -152,7 +157,7 @@ EOF ; if (!file_exists($router)) { - $output->writeln(sprintf('The given router script "%s" does not exist', $router)); + $output->error(sprintf('The given router script "%s" does not exist.', $router)); return; } @@ -161,7 +166,7 @@ EOF $finder = new PhpExecutableFinder(); if (false === $binary = $finder->find()) { - $output->writeln('Unable to find PHP binary to run server'); + $output->error('Unable to find PHP binary to run server.'); return; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ServerStartCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ServerStartCommand.php index 6300e9774a..309ede6d06 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ServerStartCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ServerStartCommand.php @@ -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; @@ -74,11 +74,15 @@ EOF */ protected function execute(InputInterface $input, OutputInterface $output) { - if (!extension_loaded('pcntl')) { - $output->writeln('This command needs the pcntl extension to run.'); - $output->writeln('You can either install it or use the server:run command instead to run the built-in web server.'); + $output = new SymfonyStyle($input, $output); - if ($this->getHelper('question')->ask($input, $output, new ConfirmationQuestion('Do you want to start server:run immediately? [Yn] ', true))) { + if (!extension_loaded('pcntl')) { + $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 ($output->ask('Do you want to execute server:run immediately? [Yn] ', true)) { $command = $this->getApplication()->find('server:run'); return $command->run($input, $output); @@ -94,7 +98,7 @@ EOF } if (!is_dir($documentRoot)) { - $output->writeln(sprintf('The given document root directory "%s" does not exist', $documentRoot)); + $output->error(sprintf('The given document root directory "%s" does not exist.', $documentRoot)); return 1; } @@ -112,32 +116,34 @@ EOF } if (!$input->getOption('force') && $this->isOtherServerProcessRunning($address)) { - $output->writeln(sprintf('A process is already listening on http://%s.', $address)); - $output->writeln(sprintf('Use the --force option if the server process terminated unexpectedly to start a new web server process.')); + $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('Running PHP built-in server in production environment is NOT recommended!'); + $output->error('Running PHP built-in server in production environment is NOT recommended!'); } $pid = pcntl_fork(); if ($pid < 0) { - $output->writeln('Unable to start the server process'); + $output->error('Unable to start the server process.'); return 1; } if ($pid > 0) { - $output->writeln(sprintf('Web server listening on http://%s', $address)); + $output->success(sprintf('Web server listening on http://%s', $address)); return; } if (posix_setsid() < 0) { - $output->writeln('Unable to set the child process as session leader'); + $output->error('Unable to set the child process as session leader'); return 1; } @@ -152,7 +158,7 @@ EOF touch($lockFile); if (!$process->isRunning()) { - $output->writeln('Unable to start the server process'); + $output->error('Unable to start the server process'); unlink($lockFile); return 1; @@ -172,13 +178,13 @@ EOF * 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 @@ -189,7 +195,7 @@ EOF } if (false === $path = realpath($router)) { - $output->writeln(sprintf('The given router script "%s" does not exist', $router)); + $output->error(sprintf('The given router script "%s" does not exist.', $router)); return false; } @@ -200,18 +206,18 @@ EOF /** * 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('Unable to find PHP binary to start server'); + $output->error('Unable to find PHP binary to start server.'); return; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ServerStatusCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ServerStatusCommand.php index 2c6d2c49ff..7b73fc9384 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ServerStatusCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ServerStatusCommand.php @@ -14,6 +14,7 @@ namespace Symfony\Bundle\FrameworkBundle\Command; 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 @@ -42,6 +43,7 @@ class ServerStatusCommand extends ServerCommand */ protected function execute(InputInterface $input, OutputInterface $output) { + $output = new SymfonyStyle($input, $output); $address = $input->getArgument('address'); // remove an orphaned lock file @@ -50,9 +52,9 @@ class ServerStatusCommand extends ServerCommand } if (file_exists($this->getLockFile($address))) { - $output->writeln(sprintf('Web server still listening on http://%s', $address)); + $output->success(sprintf('Web server still listening on http://%s', $address)); } else { - $output->writeln(sprintf('No web server is listening on http://%s', $address)); + $output->warning(sprintf('No web server is listening on http://%s', $address)); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ServerStopCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ServerStopCommand.php index c40952059a..d2bdaa167d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ServerStopCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ServerStopCommand.php @@ -15,6 +15,7 @@ use Symfony\Component\Console\Input\InputArgument; 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. @@ -54,6 +55,8 @@ EOF */ 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'); @@ -62,12 +65,12 @@ EOF $lockFile = $this->getLockFile($address); if (!file_exists($lockFile)) { - $output->writeln(sprintf('No web server is listening on http://%s', $address)); + $output->error(sprintf('No web server is listening on http://%s', $address)); return 1; } unlink($lockFile); - $output->writeln(sprintf('Stopped the web server listening on http://%s', $address)); + $output->success(sprintf('Stopped the web server listening on http://%s', $address)); } }