ExecutableFinder.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Process;
  11. /**
  12. * Generic executable finder.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  16. */
  17. class ExecutableFinder
  18. {
  19. private const CMD_BUILTINS = [
  20. 'assoc', 'break', 'call', 'cd', 'chdir', 'cls', 'color', 'copy', 'date',
  21. 'del', 'dir', 'echo', 'endlocal', 'erase', 'exit', 'for', 'ftype', 'goto',
  22. 'help', 'if', 'label', 'md', 'mkdir', 'mklink', 'move', 'path', 'pause',
  23. 'popd', 'prompt', 'pushd', 'rd', 'rem', 'ren', 'rename', 'rmdir', 'set',
  24. 'setlocal', 'shift', 'start', 'time', 'title', 'type', 'ver', 'vol',
  25. ];
  26. private $suffixes = [];
  27. /**
  28. * Replaces default suffixes of executable.
  29. */
  30. public function setSuffixes(array $suffixes)
  31. {
  32. $this->suffixes = $suffixes;
  33. }
  34. /**
  35. * Adds new possible suffix to check for executable.
  36. */
  37. public function addSuffix(string $suffix)
  38. {
  39. $this->suffixes[] = $suffix;
  40. }
  41. /**
  42. * Finds an executable by name.
  43. *
  44. * @param string $name The executable name (without the extension)
  45. * @param string|null $default The default to return if no executable is found
  46. * @param array $extraDirs Additional dirs to check into
  47. *
  48. * @return string|null
  49. */
  50. public function find(string $name, ?string $default = null, array $extraDirs = [])
  51. {
  52. // windows built-in commands that are present in cmd.exe should not be resolved using PATH as they do not exist as exes
  53. if ('\\' === \DIRECTORY_SEPARATOR && \in_array(strtolower($name), self::CMD_BUILTINS, true)) {
  54. return $name;
  55. }
  56. $dirs = array_merge(
  57. explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
  58. $extraDirs
  59. );
  60. $suffixes = [];
  61. if ('\\' === \DIRECTORY_SEPARATOR) {
  62. $pathExt = getenv('PATHEXT');
  63. $suffixes = $this->suffixes;
  64. $suffixes = array_merge($suffixes, $pathExt ? explode(\PATH_SEPARATOR, $pathExt) : ['.exe', '.bat', '.cmd', '.com']);
  65. }
  66. $suffixes = '' !== pathinfo($name, PATHINFO_EXTENSION) ? array_merge([''], $suffixes) : array_merge($suffixes, ['']);
  67. foreach ($suffixes as $suffix) {
  68. foreach ($dirs as $dir) {
  69. if ('' === $dir) {
  70. $dir = '.';
  71. }
  72. if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) {
  73. return $file;
  74. }
  75. if (!@is_dir($dir) && basename($dir) === $name.$suffix && @is_executable($dir)) {
  76. return $dir;
  77. }
  78. }
  79. }
  80. if ('\\' === \DIRECTORY_SEPARATOR || !\function_exists('exec') || \strlen($name) !== strcspn($name, '/'.\DIRECTORY_SEPARATOR)) {
  81. return $default;
  82. }
  83. $execResult = exec('command -v -- '.escapeshellarg($name));
  84. if (($executablePath = substr($execResult, 0, strpos($execResult, \PHP_EOL) ?: null)) && @is_executable($executablePath)) {
  85. return $executablePath;
  86. }
  87. return $default;
  88. }
  89. }