ServerStatusCommand.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\FrameworkBundle\Command;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Style\SymfonyStyle;
  16. /**
  17. * Shows the status of a process that is running PHP's built-in web server in
  18. * the background.
  19. *
  20. * @author Christian Flothmann <christian.flothmann@xabbuh.de>
  21. */
  22. class ServerStatusCommand extends ServerCommand
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function configure()
  28. {
  29. $this
  30. ->setDefinition(array(
  31. new InputArgument('address', InputArgument::OPTIONAL, 'Address:port', '127.0.0.1'),
  32. new InputOption('port', 'p', InputOption::VALUE_REQUIRED, 'Address port number', '8000'),
  33. ))
  34. ->setName('server:status')
  35. ->setDescription('Outputs the status of the built-in web server for the given address')
  36. ;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. protected function execute(InputInterface $input, OutputInterface $output)
  42. {
  43. $io = new SymfonyStyle($input, $output);
  44. $address = $input->getArgument('address');
  45. if (false === strpos($address, ':')) {
  46. $address .= ':'.$input->getOption('port');
  47. }
  48. // remove an orphaned lock file
  49. if (file_exists($this->getLockFile($address)) && !$this->isServerRunning($address)) {
  50. unlink($this->getLockFile($address));
  51. }
  52. if (file_exists($this->getLockFile($address))) {
  53. $io->success(sprintf('Web server still listening on http://%s', $address));
  54. } else {
  55. $io->warning(sprintf('No web server is listening on http://%s', $address));
  56. }
  57. }
  58. private function isServerRunning($address)
  59. {
  60. list($hostname, $port) = explode(':', $address);
  61. if (false !== $fp = @fsockopen($hostname, $port, $errno, $errstr, 1)) {
  62. fclose($fp);
  63. return true;
  64. }
  65. return false;
  66. }
  67. }