ExecutableFinder.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. public function setSuffixes(array $suffixes)
  21. {
  22. $this->suffixes = $suffixes;
  23. }
  24. public function addSuffix($suffix)
  25. {
  26. $this->suffixes[] = $suffix;
  27. }
  28. /**
  29. * Finds an executable by name.
  30. *
  31. * @param string $name The executable name (without the extension)
  32. * @param string $default The default to return if no executable is found
  33. *
  34. * @return string The executable path or default value
  35. */
  36. public function find($name, $default = null)
  37. {
  38. $dirs = explode(PATH_SEPARATOR, ini_get('open_basedir') ? ini_get('open_basedir') : (getenv('PATH') ? getenv('PATH') : getenv('Path')));
  39. $suffixes = DIRECTORY_SEPARATOR == '\\' ? (getenv('PATHEXT') ? explode(PATH_SEPARATOR, getenv('PATHEXT')) : $this->suffixes) : array('');
  40. foreach ($suffixes as $suffix) {
  41. foreach ($dirs as $dir) {
  42. if (is_file($file = $dir.DIRECTORY_SEPARATOR.$name.$suffix) && is_executable($file)) {
  43. return $file;
  44. }
  45. }
  46. }
  47. return $default;
  48. }
  49. }