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 | namespace Symfony\Component\Process; |
---|
13 | |
---|
14 | /** |
---|
15 | * Generic executable finder. |
---|
16 | * |
---|
17 | * @author Fabien Potencier <fabien@symfony.com> |
---|
18 | * @author Johannes M. Schmitt <schmittjoh@gmail.com> |
---|
19 | */ |
---|
20 | class ExecutableFinder |
---|
21 | { |
---|
22 | private static $isWindows; |
---|
23 | |
---|
24 | private $suffixes = array('.exe', '.bat', '.cmd', '.com'); |
---|
25 | |
---|
26 | public function __construct() |
---|
27 | { |
---|
28 | if (null === self::$isWindows) { |
---|
29 | self::$isWindows = 0 === stripos(PHP_OS, 'win'); |
---|
30 | } |
---|
31 | } |
---|
32 | |
---|
33 | public function setSuffixes(array $suffixes) |
---|
34 | { |
---|
35 | $this->suffixes = $suffixes; |
---|
36 | } |
---|
37 | |
---|
38 | public function addSuffix($suffix) |
---|
39 | { |
---|
40 | $this->suffixes[] = $suffix; |
---|
41 | } |
---|
42 | |
---|
43 | /** |
---|
44 | * Finds an executable by name. |
---|
45 | * |
---|
46 | * @param string $name The executable name (without the extension) |
---|
47 | * @param string $default The default to return if no executable is found |
---|
48 | * @param array $extraDirs Additional dirs to check into |
---|
49 | * |
---|
50 | * @return string The executable path or default value |
---|
51 | */ |
---|
52 | public function find($name, $default = null, array $extraDirs = array()) |
---|
53 | { |
---|
54 | if (ini_get('open_basedir')) { |
---|
55 | $searchPath = explode(PATH_SEPARATOR, getenv('open_basedir')); |
---|
56 | $dirs = array(); |
---|
57 | foreach ($searchPath as $path) { |
---|
58 | if (is_dir($path)) { |
---|
59 | $dirs[] = $path; |
---|
60 | } else { |
---|
61 | $file = str_replace(dirname($path), '', $path); |
---|
62 | if ($file == $name && is_executable($path)) { |
---|
63 | return $path; |
---|
64 | } |
---|
65 | } |
---|
66 | } |
---|
67 | } else { |
---|
68 | $dirs = array_merge( |
---|
69 | explode(PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')), |
---|
70 | $extraDirs |
---|
71 | ); |
---|
72 | } |
---|
73 | |
---|
74 | $suffixes = array(''); |
---|
75 | if (defined('PHP_WINDOWS_VERSION_BUILD')) { |
---|
76 | $pathExt = getenv('PATHEXT'); |
---|
77 | $suffixes = $pathExt ? explode(PATH_SEPARATOR, $pathExt) : $this->suffixes; |
---|
78 | } |
---|
79 | foreach ($suffixes as $suffix) { |
---|
80 | foreach ($dirs as $dir) { |
---|
81 | if (is_file($file = $dir.DIRECTORY_SEPARATOR.$name.$suffix) && (self::$isWindows || is_executable($file))) { |
---|
82 | return $file; |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | return $default; |
---|
88 | } |
---|
89 | } |
---|