ServerStopCommand.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. * Stops a background process running PHP's built-in web server.
  18. *
  19. * @author Christian Flothmann <christian.flothmann@xabbuh.de>
  20. */
  21. class ServerStopCommand extends ServerCommand
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function configure()
  27. {
  28. $this
  29. ->setDefinition(array(
  30. new InputArgument('address', InputArgument::OPTIONAL, 'Address:port', '127.0.0.1'),
  31. new InputOption('port', 'p', InputOption::VALUE_REQUIRED, 'Address port number', '8000'),
  32. ))
  33. ->setName('server:stop')
  34. ->setDescription('Stops PHP\'s built-in web server that was started with the server:start command')
  35. ->setHelp(<<<'EOF'
  36. The <info>%command.name%</info> stops PHP's built-in web server:
  37. <info>php %command.full_name%</info>
  38. To change the default bind address and the default port use the <info>address</info> argument:
  39. <info>php %command.full_name% 127.0.0.1:8080</info>
  40. EOF
  41. )
  42. ;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function execute(InputInterface $input, OutputInterface $output)
  48. {
  49. $io = new SymfonyStyle($input, $output);
  50. $address = $input->getArgument('address');
  51. if (false === strpos($address, ':')) {
  52. $address .= ':'.$input->getOption('port');
  53. }
  54. $lockFile = $this->getLockFile($address);
  55. if (!file_exists($lockFile)) {
  56. $io->error(sprintf('No web server is listening on http://%s', $address));
  57. return 1;
  58. }
  59. unlink($lockFile);
  60. $io->success(sprintf('Stopped the web server listening on http://%s', $address));
  61. }
  62. }