vendor/symfony/web-server-bundle/Command/ServerStatusCommand.php line 37

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\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Exception\InvalidArgumentException;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Symfony\Component\Console\Style\SymfonyStyle;
  19. /**
  20.  * Shows the status of a process that is running PHP's built-in web server in
  21.  * the background.
  22.  *
  23.  * @author Christian Flothmann <christian.flothmann@xabbuh.de>
  24.  *
  25.  * @deprecated since Symfony 4.4, to be removed in 5.0; the new Symfony local server has more features, you can use it instead.
  26.  */
  27. class ServerStatusCommand extends Command
  28. {
  29.     protected static $defaultName = 'server:status';
  30.     private $pidFileDirectory;
  31.     public function __construct(string $pidFileDirectory = null)
  32.     {
  33.         $this->pidFileDirectory = $pidFileDirectory;
  34.         parent::__construct();
  35.     }
  36.     /**
  37.      * {@inheritdoc}
  38.      */
  39.     protected function configure()
  40.     {
  41.         $this
  42.             ->setDefinition([
  43.                 new InputOption('pidfile', null, InputOption::VALUE_REQUIRED, 'PID file'),
  44.                 new InputOption('filter', null, InputOption::VALUE_REQUIRED, 'The value to display (one of port, host, or address)'),
  45.             ])
  46.             ->setDescription('Output the status of the local web server')
  47.             ->setHelp(<<<'EOF'
  48. <info>%command.name%</info> shows the details of the given local web
  49. server, such as the address and port where it is listening to:
  50.   <info>php %command.full_name%</info>
  51. To get the information as a machine readable format, use the
  52. <comment>--filter</> option:
  53. <info>php %command.full_name% --filter=port</info>
  54. Supported values are <comment>port</>, <comment>host</>, and <comment>address</>.
  55. EOF
  56.             )
  57.         ;
  58.     }
  59.     /**
  60.      * {@inheritdoc}
  61.      */
  62.     protected function execute(InputInterface $input, OutputInterface $output)
  63.     {
  64.         @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);
  65.         $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
  66.         $server = new WebServer($this->pidFileDirectory);
  67.         if ($filter = $input->getOption('filter')) {
  68.             if ($server->isRunning($input->getOption('pidfile'))) {
  69.                 [$host, $port] = explode(':', $address = $server->getAddress($input->getOption('pidfile')));
  70.                 if ('address' === $filter) {
  71.                     $output->write($address);
  72.                 } elseif ('host' === $filter) {
  73.                     $output->write($host);
  74.                 } elseif ('port' === $filter) {
  75.                     $output->write($port);
  76.                 } else {
  77.                     throw new InvalidArgumentException(sprintf('"%s" is not a valid filter.', $filter));
  78.                 }
  79.             } else {
  80.                 return 1;
  81.             }
  82.         } else {
  83.             if ($server->isRunning($input->getOption('pidfile'))) {
  84.                 $io->success(sprintf('Web server still listening on http://%s', $server->getAddress($input->getOption('pidfile'))));
  85.             } else {
  86.                 $io->warning('No web server is listening.');
  87.                 return 1;
  88.             }
  89.         }
  90.         return 0;
  91.     }
  92. }