determineWin(); } else if(stristr(PHP_OS, 'linux')!==false) { return $this->determineLinux(); } else { return $this->determineOther(); } } /** * @return string */ protected function determineWin() { $file=$this->findInDirs(array( PHP_BINDIR, getenv('PHP_BINDIR') ), '.exe'); if($file!==NULL) { return $file; } $file=$this->findInPathEnv('.exe'); if($file!==NULL) { return $file; } //return $this->searchRecursive(); return NULL; } /** * @return string */ protected function determineLinux() { $file=$this->findInDirs(array( PHP_BINDIR, getenv('PHP_BINDIR') )); if($file!==NULL) { return $file; } $file=$this->findInPathEnv(); if($file!==NULL) { return $file; } $file=$this->findWithWhich(); if($file!==NULL) { return $file; } //return $this->searchRecursive(); return NULL; } /** * @return string */ protected function determineOther() { throw new BadMethodCallException('Not yet for other environments implemented'); } /** * Tryes to find a file with one of the names in $possibleNames in one of * the given directories. * * @return string or NULL on failure * @param array Directory names * @param string optional suffix for each file */ protected function findInDirs(array $dirs, $suffix='') { foreach($dirs as $dir) { if(is_string($dir) and is_dir($dir)) { $file=$this->findFile($dir, $suffix); if($file!==NULL) { return $file; } } } } /** * Tryes to find a file within on of the directories in the PATH env var. * * @return string or NULL on failure * @param string optional suffix for each file */ protected function findInPathEnv($suffix='') { $path=getenv('PATH'); if(!is_string($path)) { return NULL; } $path=explode(PATH_SEPARATOR, $path); foreach($path as $key=>$p) { if($p[0]==='"' and $p[strlen($p)-1]==='"') { $p=substr($p, 1, -1); $p=str_replace('\"', '"', $p); } $path[$key]=$p; } return $this->findInDirs($path, $suffix); } /** * Tryes to find a file with one of the names in $possibleNames in the * given directory. If a suffix is given, it will be appended to the file name. * * @return string or NULL on failure * @param string Directory name * @param string optional suffix for each file */ protected function findFile($dir, $suffix='') { foreach(self::$possibleNames as $name) { if(is_file($dir.DIRECTORY_SEPARATOR.$name.$suffix)) { return $dir.DIRECTORY_SEPARATOR.$name.$suffix; } } return NULL; } /** * Tryes to find a file with one of the names in $possibleNames with the * shell command "which". * * @return string or NULL on failure */ protected function findWithWhich() { foreach(self::$possibleNames as $name) { $path=trim(shell_exec('which '.$name)); if($path!=='') { return $path; } } return NULL; } } $determine=new DeterminePhpBinary(); $path=$determine->determine(); if($path===NULL) { echo 'php binary not found'; exit(1); } else { echo 'php binary found at '.$path.PHP_EOL; echo shell_exec('"'.$path.'" -v'); }