[Console] Updated the styles of the server commands
This commit is contained in:
parent
c2cfbcfa60
commit
4e2cc0fc9a
@ -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('<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;
|
||||
}
|
||||
@ -104,17 +107,17 @@ EOF
|
||||
}
|
||||
|
||||
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;
|
||||
@ -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('<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()
|
||||
@ -152,7 +157,7 @@ EOF
|
||||
;
|
||||
|
||||
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;
|
||||
}
|
||||
@ -161,7 +166,7 @@ EOF
|
||||
$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;
|
||||
}
|
||||
|
@ -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('<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 = new SymfonyStyle($input, $output);
|
||||
|
||||
if ($this->getHelper('question')->ask($input, $output, new ConfirmationQuestion('Do you want to start <info>server:run</info> 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 <info>server:run</info> 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('<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;
|
||||
}
|
||||
@ -112,32 +116,34 @@ EOF
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@ -152,7 +158,7 @@ EOF
|
||||
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;
|
||||
@ -174,11 +180,11 @@ EOF
|
||||
*
|
||||
* @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 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('<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;
|
||||
}
|
||||
@ -200,18 +206,18 @@ EOF
|
||||
/**
|
||||
* Creates a process to start PHP's built-in web server.
|
||||
*
|
||||
* @param OutputInterface $output A OutputInterface instance
|
||||
* @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;
|
||||
}
|
||||
|
@ -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('<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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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('<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));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user