vendor/symfony/web-server-bundle/Command/ServerStartCommand.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\EventDispatcher\EventDispatcher;
  21. /**
  22.  * Runs a local web server in a background process.
  23.  *
  24.  * @author Christian Flothmann <christian.flothmann@xabbuh.de>
  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 ServerStartCommand extends Command
  29. {
  30.     private $documentRoot;
  31.     private $environment;
  32.     private $pidFileDirectory;
  33.     protected static $defaultName = 'server:start';
  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'),
  50.                 new InputOption('router', 'r', InputOption::VALUE_REQUIRED, 'Path to custom router script'),
  51.                 new InputOption('pidfile', null, InputOption::VALUE_REQUIRED, 'PID file'),
  52.             ])
  53.             ->setDescription('Start a local web server in the background')
  54.             ->setHelp(<<<'EOF'
  55. <info>%command.name%</info> runs a local web server: By default, the server
  56. listens on <comment>127.0.0.1</> address and the port number is automatically selected
  57. as the first free port starting from <comment>8000</>:
  58.   <info>php %command.full_name%</info>
  59. The server is run in the background and you can keep executing other commands.
  60. Execute <comment>server:stop</> to stop it.
  61. Change the default address and port by passing them as an argument:
  62.   <info>php %command.full_name% 127.0.0.1:8080</info>
  63. Use the <info>--docroot</info> option to change the default docroot directory:
  64.   <info>php %command.full_name% --docroot=htdocs/</info>
  65. Specify your own router script via the <info>--router</info> option:
  66.   <info>php %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 (!\extension_loaded('pcntl')) {
  80.             $io->error([
  81.                 'This command needs the pcntl extension to run.',
  82.                 'You can either install it or use the "server:run" command instead.',
  83.             ]);
  84.             if ($io->confirm('Do you want to execute <info>server:run</info> immediately?', false)) {
  85.                 return $this->getApplication()->find('server:run')->run($input, $output);
  86.             }
  87.             return 1;
  88.         }
  89.         if (null === $documentRoot = $input->getOption('docroot')) {
  90.             if (!$this->documentRoot) {
  91.                 $io->error('The document root directory must be either passed as first argument of the constructor or through the "docroot" input option.');
  92.                 return 1;
  93.             }
  94.             $documentRoot = $this->documentRoot;
  95.         }
  96.         if (!$env = $this->environment) {
  97.             if ($input->hasOption('env') && !$env = $input->getOption('env')) {
  98.                 $io->error('The environment must be either passed as second argument of the constructor or through the "--env" input option.');
  99.                 return 1;
  100.             } else {
  101.                 $io->error('The environment must be passed as second argument of the constructor.');
  102.                 return 1;
  103.             }
  104.         }
  105.         if ('prod' === $env) {
  106.             $io->error('Running this server in production environment is NOT recommended!');
  107.         }
  108.         // replace event dispatcher with an empty one to prevent console.terminate from firing
  109.         // as container could have changed between start and stop
  110.         $this->getApplication()->setDispatcher(new EventDispatcher());
  111.         try {
  112.             $server = new WebServer($this->pidFileDirectory);
  113.             if ($server->isRunning($input->getOption('pidfile'))) {
  114.                 $io->error(sprintf('The web server has already been started. It is currently listening on http://%s. Please stop the web server before you try to start it again.', $server->getAddress($input->getOption('pidfile'))));
  115.                 return 1;
  116.             }
  117.             $config = new WebServerConfig($documentRoot, $env, $input->getArgument('addressport'), $input->getOption('router'));
  118.             if (WebServer::STARTED === $server->start($config, $input->getOption('pidfile'))) {
  119.                 $message = sprintf('Server listening on http://%s', $config->getAddress());
  120.                 if ('' !== $displayAddress = $config->getDisplayAddress()) {
  121.                     $message = sprintf('Server listening on all interfaces, port %s -- see http://%s', $config->getPort(), $displayAddress);
  122.                 }
  123.                 $io->success($message);
  124.                 if (\ini_get('xdebug.profiler_enable_trigger')) {
  125.                     $io->comment('Xdebug profiler trigger enabled.');
  126.                 }
  127.             }
  128.         } catch (\Exception $e) {
  129.             $io->error($e->getMessage());
  130.             return 1;
  131.         }
  132.         return 0;
  133.     }
  134. }