vendor/symfony/web-server-bundle/Command/ServerRunCommand.php line 40

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\WebServerBundle\Command;
  11. use Symfony\Bundle\WebServerBundle\WebServer;
  12. use Symfony\Bundle\WebServerBundle\WebServerConfig;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. use Symfony\Component\Console\Style\SymfonyStyle;
  20. use Symfony\Component\Process\Process;
  21. /**
  22.  * Runs Symfony application using a local web server.
  23.  *
  24.  * @author MichaƂ Pipa <michal.pipa.xsolve@gmail.com>
  25.  *
  26.  * @deprecated since Symfony 4.4, to be removed in 5.0; the new Symfony local server has more features, you can use it instead.
  27.  */
  28. class ServerRunCommand extends Command
  29. {
  30.     private $documentRoot;
  31.     private $environment;
  32.     private $pidFileDirectory;
  33.     protected static $defaultName = 'server:run';
  34.     public function __construct(string $documentRoot = null, string $environment = null, string $pidFileDirectory = null)
  35.     {
  36.         $this->documentRoot = $documentRoot;
  37.         $this->environment = $environment;
  38.         $this->pidFileDirectory = $pidFileDirectory;
  39.         parent::__construct();
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     protected function configure()
  45.     {
  46.         $this
  47.             ->setDefinition([
  48.                 new InputArgument('addressport', InputArgument::OPTIONAL, 'The address to listen to (can be address:port, address, or port)'),
  49.                 new InputOption('docroot', 'd', InputOption::VALUE_REQUIRED, 'Document root, usually where your front controllers are stored'),
  50.                 new InputOption('router', 'r', InputOption::VALUE_REQUIRED, 'Path to custom router script'),
  51.             ])
  52.             ->setDescription('Run a local web server')
  53.             ->setHelp(<<<'EOF'
  54. <info>%command.name%</info> runs a local web server: By default, the server
  55. listens on <comment>127.0.0.1</> address and the port number is automatically selected
  56. as the first free port starting from <comment>8000</>:
  57.   <info>%command.full_name%</info>
  58. This command blocks the console. If you want to run other commands, stop it by
  59. pressing <comment>Control+C</> or use the non-blocking <comment>server:start</>
  60. command instead.
  61. Change the default address and port by passing them as an argument:
  62.   <info>%command.full_name% 127.0.0.1:8080</info>
  63. Use the <info>--docroot</info> option to change the default docroot directory:
  64.   <info>%command.full_name% --docroot=htdocs/</info>
  65. Specify your own router script via the <info>--router</info> option:
  66.   <info>%command.full_name% --router=app/config/router.php</info>
  67. See also: https://php.net/features.commandline.webserver
  68. EOF
  69.             )
  70.         ;
  71.     }
  72.     /**
  73.      * {@inheritdoc}
  74.      */
  75.     protected function execute(InputInterface $input, OutputInterface $output)
  76.     {
  77.         @trigger_error('Using the WebserverBundle is deprecated since Symfony 4.4. The new Symfony local server has more features, you can use it instead.', \E_USER_DEPRECATED);
  78.         $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
  79.         if (null === $documentRoot = $input->getOption('docroot')) {
  80.             if (!$this->documentRoot) {
  81.                 $io->error('The document root directory must be either passed as first argument of the constructor or through the "--docroot" input option.');
  82.                 return 1;
  83.             }
  84.             $documentRoot = $this->documentRoot;
  85.         }
  86.         if (!$env = $this->environment) {
  87.             if ($input->hasOption('env') && !$env = $input->getOption('env')) {
  88.                 $io->error('The environment must be either passed as second argument of the constructor or through the "--env" input option.');
  89.                 return 1;
  90.             } else {
  91.                 $io->error('The environment must be passed as second argument of the constructor.');
  92.                 return 1;
  93.             }
  94.         }
  95.         if ('prod' === $env) {
  96.             $io->error('Running this server in production environment is NOT recommended!');
  97.         }
  98.         $callback = null;
  99.         $disableOutput = false;
  100.         if ($output->isQuiet()) {
  101.             $disableOutput = true;
  102.         } else {
  103.             $callback = function ($type, $buffer) use ($output) {
  104.                 if (Process::ERR === $type && $output instanceof ConsoleOutputInterface) {
  105.                     $output = $output->getErrorOutput();
  106.                 }
  107.                 $output->write($buffer, false, OutputInterface::OUTPUT_RAW);
  108.             };
  109.         }
  110.         try {
  111.             $server = new WebServer($this->pidFileDirectory);
  112.             $config = new WebServerConfig($documentRoot, $env, $input->getArgument('addressport'), $input->getOption('router'));
  113.             $message = sprintf('Server listening on http://%s', $config->getAddress());
  114.             if ('' !== $displayAddress = $config->getDisplayAddress()) {
  115.                 $message = sprintf('Server listening on all interfaces, port %s -- see http://%s', $config->getPort(), $displayAddress);
  116.             }
  117.             $io->success($message);
  118.             if (\ini_get('xdebug.profiler_enable_trigger')) {
  119.                 $io->comment('Xdebug profiler trigger enabled.');
  120.             }
  121.             $io->comment('Quit the server with CONTROL-C.');
  122.             $exitCode = $server->run($config, $disableOutput, $callback);
  123.         } catch (\Exception $e) {
  124.             $io->error($e->getMessage());
  125.             return 1;
  126.         }
  127.         return $exitCode;
  128.     }
  129. }