ServerCommand.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. /**
  12. * Base methods for commands related to PHP's built-in web server.
  13. *
  14. * @author Christian Flothmann <christian.flothmann@xabbuh.de>
  15. */
  16. abstract class ServerCommand extends ContainerAwareCommand
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function isEnabled()
  22. {
  23. if (\PHP_VERSION_ID < 50400 || \defined('HHVM_VERSION')) {
  24. return false;
  25. }
  26. if (!class_exists('Symfony\Component\Process\Process')) {
  27. return false;
  28. }
  29. return parent::isEnabled();
  30. }
  31. /**
  32. * Determines the name of the lock file for a particular PHP web server process.
  33. *
  34. * @param string $address An address/port tuple
  35. *
  36. * @return string The filename
  37. */
  38. protected function getLockFile($address)
  39. {
  40. return sys_get_temp_dir().'/'.strtr($address, '.:', '--').'.pid';
  41. }
  42. protected function isOtherServerProcessRunning($address)
  43. {
  44. $lockFile = $this->getLockFile($address);
  45. if (file_exists($lockFile)) {
  46. return true;
  47. }
  48. $pos = strrpos($address, ':');
  49. $hostname = substr($address, 0, $pos);
  50. $port = substr($address, $pos + 1);
  51. $fp = @fsockopen($hostname, $port, $errno, $errstr, 5);
  52. if (false !== $fp) {
  53. fclose($fp);
  54. return true;
  55. }
  56. return false;
  57. }
  58. }