ExecutableFinder.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * @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. if (\ini_get('open_basedir')) {
  48. $searchPath = array_merge(explode(\PATH_SEPARATOR, \ini_get('open_basedir')), $extraDirs);
  49. $dirs = [];
  50. foreach ($searchPath as $path) {
  51. // Silencing against https://bugs.php.net/69240
  52. if (@is_dir($path)) {
  53. $dirs[] = $path;
  54. } else {
  55. if (basename($path) == $name && @is_executable($path)) {
  56. return $path;
  57. }
  58. }
  59. }
  60. } else {
  61. $dirs = array_merge(
  62. explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
  63. $extraDirs
  64. );
  65. }
  66. $suffixes = [''];
  67. if ('\\' === \DIRECTORY_SEPARATOR) {
  68. $pathExt = getenv('PATHEXT');
  69. $suffixes = array_merge($pathExt ? explode(\PATH_SEPARATOR, $pathExt) : $this->suffixes, $suffixes);
  70. }
  71. foreach ($suffixes as $suffix) {
  72. foreach ($dirs as $dir) {
  73. if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) {
  74. return $file;
  75. }
  76. }
  77. }
  78. return $default;
  79. }
  80. }