| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Generic executable finder. |
| 14 |
* |
| 15 |
* @author Fabien Potencier <fabien@symfony.com> |
| 16 |
* @author Johannes M. Schmitt <schmittjoh@gmail.com> |
| 17 |
*/ |
| 18 |
class Symfony_Process_ExecutableFinder |
| 19 |
{ |
| 20 |
private $suffixes = array('.exe', '.bat', '.cmd', '.com'); |
| 21 |
|
| 22 |
/** |
| 23 |
* Replaces default suffixes of executable. |
| 24 |
* |
| 25 |
* @param array $suffixes |
| 26 |
*/ |
| 27 |
public function setSuffixes(array $suffixes) |
| 28 |
{ |
| 29 |
$this->suffixes = $suffixes; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Adds new possible suffix to check for executable. |
| 34 |
* |
| 35 |
* @param string $suffix |
| 36 |
*/ |
| 37 |
public function addSuffix($suffix) |
| 38 |
{ |
| 39 |
$this->suffixes[] = $suffix; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Finds an executable by name. |
| 44 |
* |
| 45 |
* @param string $name The executable name (without the extension) |
| 46 |
* @param string $default The default to return if no executable is found |
| 47 |
* @param array $extraDirs Additional dirs to check into |
| 48 |
* |
| 49 |
* @return string The executable path or default value |
| 50 |
*/ |
| 51 |
public function find($name, $default = null, array $extraDirs = array()) |
| 52 |
{ |
| 53 |
if (ini_get('open_basedir')) { |
| 54 |
$searchPath = explode(PATH_SEPARATOR, ini_get('open_basedir')); |
| 55 |
$dirs = array(); |
| 56 |
foreach ($searchPath as $path) { |
| 57 |
if (@is_dir($path)) { |
| 58 |
$dirs[] = $path; |
| 59 |
} else { |
| 60 |
if (basename($path) == $name && is_executable($path)) { |
| 61 |
return $path; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} else { |
| 66 |
$dirs = array_merge( |
| 67 |
explode(PATH_SEPARATOR, getenv('PATH') ? getenv('PATH') : getenv('Path')), |
| 68 |
$extraDirs |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
$suffixes = array(''); |
| 73 |
if (Symfony_Process_ProcessUtils::isWindows()) { |
| 74 |
$pathExt = getenv('PATHEXT'); |
| 75 |
$suffixes = $pathExt ? explode(PATH_SEPARATOR, $pathExt) : $this->suffixes; |
| 76 |
} |
| 77 |
foreach ($suffixes as $suffix) { |
| 78 |
foreach ($dirs as $dir) { |
| 79 |
if (is_file($file = $dir.DIRECTORY_SEPARATOR.$name.$suffix) && (Symfony_Process_ProcessUtils::isWindows() || is_executable($file))) { |
| 80 |
return $file; |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
return $default; |
| 86 |
} |
| 87 |
} |
| 88 |
|