No Description

Shell.php 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 (!function_exists('exec')) {
  47. return false;
  48. }
  49. // todo: find a better way (command could not be available)
  50. $testCommand = 'which ';
  51. if (self::TYPE_WINDOWS === $this->type) {
  52. $testCommand = 'where ';
  53. }
  54. $command = escapeshellcmd($command);
  55. exec($testCommand.$command, $output, $code);
  56. return 0 === $code && count($output) > 0;
  57. }
  58. /**
  59. * Guesses OS type.
  60. *
  61. * @return int
  62. */
  63. private function guessType()
  64. {
  65. $os = strtolower(PHP_OS);
  66. if (false !== strpos($os, 'cygwin')) {
  67. return self::TYPE_CYGWIN;
  68. }
  69. if (false !== strpos($os, 'darwin')) {
  70. return self::TYPE_DARWIN;
  71. }
  72. if (false !== strpos($os, 'bsd')) {
  73. return self::TYPE_BSD;
  74. }
  75. if (0 === strpos($os, 'win')) {
  76. return self::TYPE_WINDOWS;
  77. }
  78. return self::TYPE_UNIX;
  79. }
  80. }