Drupal investigation

Shell.php 2.1KB

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