1<?php 2 3/* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Symfony\Bundle\WebServerBundle\Command; 13 14use Symfony\Bundle\WebServerBundle\WebServer; 15use Symfony\Bundle\WebServerBundle\WebServerConfig; 16use Symfony\Component\Console\Input\InputArgument; 17use Symfony\Component\Console\Input\InputInterface; 18use Symfony\Component\Console\Input\InputOption; 19use Symfony\Component\Console\Output\ConsoleOutputInterface; 20use Symfony\Component\Console\Output\OutputInterface; 21use Symfony\Component\Console\Style\SymfonyStyle; 22use Symfony\Component\EventDispatcher\EventDispatcher; 23 24/** 25 * Runs a local web server in a background process. 26 * 27 * @author Christian Flothmann <christian.flothmann@xabbuh.de> 28 */ 29class ServerStartCommand extends ServerCommand 30{ 31 private $documentRoot; 32 private $environment; 33 34 protected static $defaultName = 'server:start'; 35 36 public function __construct($documentRoot = null, $environment = null) 37 { 38 $this->documentRoot = $documentRoot; 39 $this->environment = $environment; 40 41 parent::__construct(); 42 } 43 44 /** 45 * {@inheritdoc} 46 */ 47 protected function configure() 48 { 49 $this 50 ->setDefinition([ 51 new InputArgument('addressport', InputArgument::OPTIONAL, 'The address to listen to (can be address:port, address, or port)'), 52 new InputOption('docroot', 'd', InputOption::VALUE_REQUIRED, 'Document root'), 53 new InputOption('router', 'r', InputOption::VALUE_REQUIRED, 'Path to custom router script'), 54 new InputOption('pidfile', null, InputOption::VALUE_REQUIRED, 'PID file'), 55 ]) 56 ->setDescription('Starts a local web server in the background') 57 ->setHelp(<<<'EOF' 58<info>%command.name%</info> runs a local web server: By default, the server 59listens on <comment>127.0.0.1</> address and the port number is automatically selected 60as the first free port starting from <comment>8000</>: 61 62 <info>php %command.full_name%</info> 63 64The server is run in the background and you can keep executing other commands. 65Execute <comment>server:stop</> to stop it. 66 67Change the default address and port by passing them as an argument: 68 69 <info>php %command.full_name% 127.0.0.1:8080</info> 70 71Use the <info>--docroot</info> option to change the default docroot directory: 72 73 <info>php %command.full_name% --docroot=htdocs/</info> 74 75Specify your own router script via the <info>--router</info> option: 76 77 <info>php %command.full_name% --router=app/config/router.php</info> 78 79See also: https://php.net/features.commandline.webserver 80EOF 81 ) 82 ; 83 } 84 85 /** 86 * {@inheritdoc} 87 */ 88 protected function execute(InputInterface $input, OutputInterface $output) 89 { 90 $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output); 91 92 if (!\extension_loaded('pcntl')) { 93 $io->error([ 94 'This command needs the pcntl extension to run.', 95 'You can either install it or use the "server:run" command instead.', 96 ]); 97 98 if ($io->confirm('Do you want to execute <info>server:run</info> immediately?', false)) { 99 return $this->getApplication()->find('server:run')->run($input, $output); 100 } 101 102 return 1; 103 } 104 105 // deprecated, logic to be removed in 4.0 106 // this allows the commands to work out of the box with web/ and public/ 107 if ($this->documentRoot && !is_dir($this->documentRoot) && is_dir(\dirname($this->documentRoot).'/web')) { 108 $this->documentRoot = \dirname($this->documentRoot).'/web'; 109 } 110 111 if (null === $documentRoot = $input->getOption('docroot')) { 112 if (!$this->documentRoot) { 113 $io->error('The document root directory must be either passed as first argument of the constructor or through the "docroot" input option.'); 114 115 return 1; 116 } 117 $documentRoot = $this->documentRoot; 118 } 119 120 if (!$env = $this->environment) { 121 if ($input->hasOption('env') && !$env = $input->getOption('env')) { 122 $io->error('The environment must be either passed as second argument of the constructor or through the "--env" input option.'); 123 124 return 1; 125 } else { 126 $io->error('The environment must be passed as second argument of the constructor.'); 127 128 return 1; 129 } 130 } 131 132 if ('prod' === $env) { 133 $io->error('Running this server in production environment is NOT recommended!'); 134 } 135 136 // replace event dispatcher with an empty one to prevent console.terminate from firing 137 // as container could have changed between start and stop 138 $this->getApplication()->setDispatcher(new EventDispatcher()); 139 140 try { 141 $server = new WebServer(); 142 if ($server->isRunning($input->getOption('pidfile'))) { 143 $io->error(sprintf('The web server is already running (listening on http://%s).', $server->getAddress($input->getOption('pidfile')))); 144 145 return 1; 146 } 147 148 $config = new WebServerConfig($documentRoot, $env, $input->getArgument('addressport'), $input->getOption('router')); 149 150 if (WebServer::STARTED === $server->start($config, $input->getOption('pidfile'))) { 151 $io->success(sprintf('Server listening on http://%s', $config->getAddress())); 152 } 153 } catch (\Exception $e) { 154 $io->error($e->getMessage()); 155 156 return 1; 157 } 158 159 return null; 160 } 161} 162