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\Process\Process; 23 24/** 25 * Runs Symfony application using a local web server. 26 * 27 * @author Michał Pipa <michal.pipa.xsolve@gmail.com> 28 */ 29class ServerRunCommand extends ServerCommand 30{ 31 private $documentRoot; 32 private $environment; 33 34 protected static $defaultName = 'server:run'; 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, usually where your front controllers are stored'), 53 new InputOption('router', 'r', InputOption::VALUE_REQUIRED, 'Path to custom router script'), 54 ]) 55 ->setDescription('Runs a local web server') 56 ->setHelp(<<<'EOF' 57<info>%command.name%</info> runs a local web server: By default, the server 58listens on <comment>127.0.0.1</> address and the port number is automatically selected 59as the first free port starting from <comment>8000</>: 60 61 <info>%command.full_name%</info> 62 63This command blocks the console. If you want to run other commands, stop it by 64pressing <comment>Control+C</> or use the non-blocking <comment>server:start</> 65command instead. 66 67Change the default address and port by passing them as an argument: 68 69 <info>%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>%command.full_name% --docroot=htdocs/</info> 74 75Specify your own router script via the <info>--router</info> option: 76 77 <info>%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 // deprecated, logic to be removed in 4.0 93 // this allows the commands to work out of the box with web/ and public/ 94 if ($this->documentRoot && !is_dir($this->documentRoot) && is_dir(\dirname($this->documentRoot).'/web')) { 95 $this->documentRoot = \dirname($this->documentRoot).'/web'; 96 } 97 98 if (null === $documentRoot = $input->getOption('docroot')) { 99 if (!$this->documentRoot) { 100 $io->error('The document root directory must be either passed as first argument of the constructor or through the "--docroot" input option.'); 101 102 return 1; 103 } 104 $documentRoot = $this->documentRoot; 105 } 106 107 if (!$env = $this->environment) { 108 if ($input->hasOption('env') && !$env = $input->getOption('env')) { 109 $io->error('The environment must be either passed as second argument of the constructor or through the "--env" input option.'); 110 111 return 1; 112 } else { 113 $io->error('The environment must be passed as second argument of the constructor.'); 114 115 return 1; 116 } 117 } 118 119 if ('prod' === $env) { 120 $io->error('Running this server in production environment is NOT recommended!'); 121 } 122 123 $callback = null; 124 $disableOutput = false; 125 if ($output->isQuiet()) { 126 $disableOutput = true; 127 } else { 128 $callback = function ($type, $buffer) use ($output) { 129 if (Process::ERR === $type && $output instanceof ConsoleOutputInterface) { 130 $output = $output->getErrorOutput(); 131 } 132 $output->write($buffer, false, OutputInterface::OUTPUT_RAW); 133 }; 134 } 135 136 try { 137 $server = new WebServer(); 138 $config = new WebServerConfig($documentRoot, $env, $input->getArgument('addressport'), $input->getOption('router')); 139 140 $io->success(sprintf('Server listening on http://%s', $config->getAddress())); 141 $io->comment('Quit the server with CONTROL-C.'); 142 143 $exitCode = $server->run($config, $disableOutput, $callback); 144 } catch (\Exception $e) { 145 $io->error($e->getMessage()); 146 147 return 1; 148 } 149 150 return $exitCode; 151 } 152} 153