public static function getExecutable($name, $throwException = false)
{
if (isset(self::$executableCache[$name])) {
return self::$executableCache[$name];
}
$pathVariable = Config::getSystemConfig()->general->path_variable;
$paths = [];
if ($pathVariable) {
$paths = explode(PATH_SEPARATOR, $pathVariable);
}
array_push($paths, "");
// allow custom setup routines for certain programs
$customSetupMethod = "setup" . ucfirst($name);
if (method_exists(__CLASS__, $customSetupMethod)) {
self::$customSetupMethod();
}
// allow custom check routines for certain programs
$customCheckMethod = "check" . ucfirst($name);
if (!method_exists(__CLASS__, $customCheckMethod)) {
$customCheckMethod = "checkDummy";
}
foreach ($paths as $path) {
foreach (["--help", "-h", "-help"] as $option) {
try {
$path = rtrim($path, "/\\ ");
if ($path) {
$executablePath = $path . DIRECTORY_SEPARATOR . $name;
} else {
$executablePath = $name;
}
$process = new Process($executablePath . " " . $option);
$process->run();
if ($process->isSuccessful() || self::$customCheckMethod($process)) {
if (empty($path) && self::getSystemEnvironment() == "unix") {
// get the full qualified path, seems to solve a lot of problems :)
// if not using the full path, timeout, nohup and nice will fail
$fullQualifiedPath = shell_exec("which " . $executablePath);
$fullQualifiedPath = trim($fullQualifiedPath);
if ($fullQualifiedPath) {
$executablePath = $fullQualifiedPath;
}
}
self::$executableCache[$name] = $executablePath;
return $executablePath;
}
} catch (\Exception $e) {
}
}
}
self::$executableCache[$name] = false;
if ($throwException) {
throw new \Exception("No '{$name}' executable found, please install the application or add it to the PATH (in system settings or to your PATH environment variable");
}
return false;
}