ExecutableFinder.php 2.5 KB

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