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 $suffixes = array('.exe', '.bat', '.cmd', '.com');
|
23
|
|
24
|
/**
|
25
|
* Replaces default suffixes of executable.
|
26
|
*
|
27
|
* @param array $suffixes
|
28
|
*/
|
29
|
public function setSuffixes(array $suffixes)
|
30
|
{
|
31
|
$this->suffixes = $suffixes;
|
32
|
}
|
33
|
|
34
|
/**
|
35
|
* Adds new possible suffix to check for executable.
|
36
|
*
|
37
|
* @param string $suffix
|
38
|
*/
|
39
|
public function addSuffix($suffix)
|
40
|
{
|
41
|
$this->suffixes[] = $suffix;
|
42
|
}
|
43
|
|
44
|
/**
|
45
|
* Finds an executable by name.
|
46
|
*
|
47
|
* @param string $name The executable name (without the extension)
|
48
|
* @param string $default The default to return if no executable is found
|
49
|
* @param array $extraDirs Additional dirs to check into
|
50
|
*
|
51
|
* @return string The executable path or default value
|
52
|
*/
|
53
|
public function find($name, $default = null, array $extraDirs = array())
|
54
|
{
|
55
|
if (ini_get('open_basedir')) {
|
56
|
$searchPath = explode(PATH_SEPARATOR, ini_get('open_basedir'));
|
57
|
$dirs = array();
|
58
|
foreach ($searchPath as $path) {
|
59
|
// Silencing against https://bugs.php.net/69240
|
60
|
if (@is_dir($path)) {
|
61
|
$dirs[] = $path;
|
62
|
} else {
|
63
|
if (basename($path) == $name && is_executable($path)) {
|
64
|
return $path;
|
65
|
}
|
66
|
}
|
67
|
}
|
68
|
} else {
|
69
|
$dirs = array_merge(
|
70
|
explode(PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
|
71
|
$extraDirs
|
72
|
);
|
73
|
}
|
74
|
|
75
|
$suffixes = array('');
|
76
|
if ('\\' === DIRECTORY_SEPARATOR) {
|
77
|
$pathExt = getenv('PATHEXT');
|
78
|
$suffixes = $pathExt ? explode(PATH_SEPARATOR, $pathExt) : $this->suffixes;
|
79
|
}
|
80
|
foreach ($suffixes as $suffix) {
|
81
|
foreach ($dirs as $dir) {
|
82
|
if (is_file($file = $dir.DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === DIRECTORY_SEPARATOR || is_executable($file))) {
|
83
|
return $file;
|
84
|
}
|
85
|
}
|
86
|
}
|
87
|
|
88
|
return $default;
|
89
|
}
|
90
|
}
|