菜谱项目

ExecutableFinder.php 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 = array('.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. * @param string $suffix
  31. */
  32. public function addSuffix($suffix)
  33. {
  34. $this->suffixes[] = $suffix;
  35. }
  36. /**
  37. * Finds an executable by name.
  38. *
  39. * @param string $name The executable name (without the extension)
  40. * @param string $default The default to return if no executable is found
  41. * @param array $extraDirs Additional dirs to check into
  42. *
  43. * @return string The executable path or default value
  44. */
  45. public function find($name, $default = null, array $extraDirs = array())
  46. {
  47. if (ini_get('open_basedir')) {
  48. $searchPath = explode(PATH_SEPARATOR, ini_get('open_basedir'));
  49. $dirs = array();
  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 = array('');
  67. if ('\\' === DIRECTORY_SEPARATOR) {
  68. $pathExt = getenv('PATHEXT');
  69. $suffixes = array_merge($suffixes, $pathExt ? explode(PATH_SEPARATOR, $pathExt) : $this->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. }