Shell.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\Component\Finder\Shell;
  11. /**
  12. * @author Jean-François Simon <contact@jfsimon.fr>
  13. */
  14. class Shell
  15. {
  16. const TYPE_UNIX = 1;
  17. const TYPE_DARWIN = 2;
  18. const TYPE_CYGWIN = 3;
  19. const TYPE_WINDOWS = 4;
  20. const TYPE_BSD = 5;
  21. /**
  22. * @var string|null
  23. */
  24. private $type;
  25. /**
  26. * Returns guessed OS type.
  27. *
  28. * @return int
  29. */
  30. public function getType()
  31. {
  32. if (null === $this->type) {
  33. $this->type = $this->guessType();
  34. }
  35. return $this->type;
  36. }
  37. /**
  38. * Tests if a command is available.
  39. *
  40. * @param string $command
  41. *
  42. * @return bool
  43. */
  44. public function testCommand($command)
  45. {
  46. if (self::TYPE_WINDOWS === $this->type) {
  47. // todo: find a way to test if windows command exists
  48. return false;
  49. }
  50. if (!function_exists('exec')) {
  51. return false;
  52. }
  53. // todo: find a better way (command could not be available)
  54. exec('command -v '.$command, $output, $code);
  55. return 0 === $code && count($output) > 0;
  56. }
  57. /**
  58. * Guesses OS type.
  59. *
  60. * @return int
  61. */
  62. private function guessType()
  63. {
  64. $os = strtolower(PHP_OS);
  65. if (false !== strpos($os, 'cygwin')) {
  66. return self::TYPE_CYGWIN;
  67. }
  68. if (false !== strpos($os, 'darwin')) {
  69. return self::TYPE_DARWIN;
  70. }
  71. if (false !== strpos($os, 'bsd')) {
  72. return self::TYPE_BSD;
  73. }
  74. if (0 === strpos($os, 'win')) {
  75. return self::TYPE_WINDOWS;
  76. }
  77. return self::TYPE_UNIX;
  78. }
  79. }