ExecutableFinder.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 array $suffixes = [];
  27. /**
  28. * Replaces default suffixes of executable.
  29. */
  30. public function setSuffixes(array $suffixes): void
  31. {
  32. $this->suffixes = $suffixes;
  33. }
  34. /**
  35. * Adds new possible suffix to check for executable.
  36. */
  37. public function addSuffix(string $suffix): void
  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. public function find(string $name, ?string $default = null, array $extraDirs = []): ?string
  49. {
  50. // windows built-in commands that are present in cmd.exe should not be resolved using PATH as they do not exist as exes
  51. if ('\\' === \DIRECTORY_SEPARATOR && \in_array(strtolower($name), self::CMD_BUILTINS, true)) {
  52. return $name;
  53. }
  54. $dirs = array_merge(
  55. explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
  56. $extraDirs
  57. );
  58. $suffixes = [];
  59. if ('\\' === \DIRECTORY_SEPARATOR) {
  60. $pathExt = getenv('PATHEXT');
  61. $suffixes = $this->suffixes;
  62. $suffixes = array_merge($suffixes, $pathExt ? explode(\PATH_SEPARATOR, $pathExt) : ['.exe', '.bat', '.cmd', '.com']);
  63. }
  64. $suffixes = '' !== pathinfo($name, PATHINFO_EXTENSION) ? array_merge([''], $suffixes) : array_merge($suffixes, ['']);
  65. foreach ($suffixes as $suffix) {
  66. foreach ($dirs as $dir) {
  67. if ('' === $dir) {
  68. $dir = '.';
  69. }
  70. if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) {
  71. return $file;
  72. }
  73. if (!@is_dir($dir) && basename($dir) === $name.$suffix && @is_executable($dir)) {
  74. return $dir;
  75. }
  76. }
  77. }
  78. if ('\\' === \DIRECTORY_SEPARATOR || !\function_exists('exec') || \strlen($name) !== strcspn($name, '/'.\DIRECTORY_SEPARATOR)) {
  79. return $default;
  80. }
  81. $execResult = exec('command -v -- '.escapeshellarg($name));
  82. if (($executablePath = substr($execResult, 0, strpos($execResult, \PHP_EOL) ?: null)) && @is_executable($executablePath)) {
  83. return $executablePath;
  84. }
  85. return $default;
  86. }
  87. }