ExecutableFinder.php 3.4 KB

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