Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Process; Chris@0: Chris@0: /** Chris@0: * Generic executable finder. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: * @author Johannes M. Schmitt Chris@0: */ Chris@0: class ExecutableFinder Chris@0: { Chris@17: private $suffixes = ['.exe', '.bat', '.cmd', '.com']; Chris@0: Chris@0: /** Chris@0: * Replaces default suffixes of executable. Chris@0: */ Chris@0: public function setSuffixes(array $suffixes) Chris@0: { Chris@0: $this->suffixes = $suffixes; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds new possible suffix to check for executable. Chris@0: * Chris@0: * @param string $suffix Chris@0: */ Chris@0: public function addSuffix($suffix) Chris@0: { Chris@0: $this->suffixes[] = $suffix; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Finds an executable by name. Chris@0: * Chris@17: * @param string $name The executable name (without the extension) Chris@17: * @param string|null $default The default to return if no executable is found Chris@17: * @param array $extraDirs Additional dirs to check into Chris@0: * Chris@17: * @return string|null The executable path or default value Chris@0: */ Chris@17: public function find($name, $default = null, array $extraDirs = []) Chris@0: { Chris@0: if (ini_get('open_basedir')) { Chris@18: $searchPath = array_merge(explode(PATH_SEPARATOR, ini_get('open_basedir')), $extraDirs); Chris@17: $dirs = []; Chris@0: foreach ($searchPath as $path) { Chris@0: // Silencing against https://bugs.php.net/69240 Chris@0: if (@is_dir($path)) { Chris@0: $dirs[] = $path; Chris@0: } else { Chris@0: if (basename($path) == $name && @is_executable($path)) { Chris@0: return $path; Chris@0: } Chris@0: } Chris@0: } Chris@0: } else { Chris@0: $dirs = array_merge( Chris@0: explode(PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')), Chris@0: $extraDirs Chris@0: ); Chris@0: } Chris@0: Chris@17: $suffixes = ['']; Chris@17: if ('\\' === \DIRECTORY_SEPARATOR) { Chris@0: $pathExt = getenv('PATHEXT'); Chris@16: $suffixes = array_merge($pathExt ? explode(PATH_SEPARATOR, $pathExt) : $this->suffixes, $suffixes); Chris@0: } Chris@0: foreach ($suffixes as $suffix) { Chris@0: foreach ($dirs as $dir) { Chris@17: if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) { Chris@0: return $file; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $default; Chris@0: } Chris@0: }