ExecutableFinder.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 array $suffixes = ['.exe', '.bat', '.cmd', '.com'];
  20. /**
  21. * Replaces default suffixes of executable.
  22. *
  23. * @return void
  24. */
  25. public function setSuffixes(array $suffixes)
  26. {
  27. $this->suffixes = $suffixes;
  28. }
  29. /**
  30. * Adds new possible suffix to check for executable.
  31. *
  32. * @return void
  33. */
  34. public function addSuffix(string $suffix)
  35. {
  36. $this->suffixes[] = $suffix;
  37. }
  38. /**
  39. * Finds an executable by name.
  40. *
  41. * @param string $name The executable name (without the extension)
  42. * @param string|null $default The default to return if no executable is found
  43. * @param array $extraDirs Additional dirs to check into
  44. */
  45. public function find(string $name, ?string $default = null, array $extraDirs = []): ?string
  46. {
  47. $dirs = array_merge(
  48. explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
  49. $extraDirs
  50. );
  51. $suffixes = [''];
  52. if ('\\' === \DIRECTORY_SEPARATOR) {
  53. $pathExt = getenv('PATHEXT');
  54. $suffixes = array_merge($pathExt ? explode(\PATH_SEPARATOR, $pathExt) : $this->suffixes, $suffixes);
  55. }
  56. foreach ($suffixes as $suffix) {
  57. foreach ($dirs as $dir) {
  58. if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) {
  59. return $file;
  60. }
  61. if (!@is_dir($dir) && basename($dir) === $name.$suffix && @is_executable($dir)) {
  62. return $dir;
  63. }
  64. }
  65. }
  66. $command = '\\' === \DIRECTORY_SEPARATOR ? 'where' : 'command -v --';
  67. if (\function_exists('exec') && ($executablePath = strtok(@exec($command.' '.escapeshellarg($name)), \PHP_EOL)) && @is_executable($executablePath)) {
  68. return $executablePath;
  69. }
  70. return $default;
  71. }
  72. }