| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 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 <[email protected]> |
| 18 |
* @author Johannes M. Schmitt <[email protected]> |
| 19 |
*/ |
| 20 |
class ExecutableFinder |
| 21 |
{ |
| 22 |
private const CMD_BUILTINS = [ |
| 23 |
'assoc', 'break', 'call', 'cd', 'chdir', 'cls', 'color', 'copy', 'date', |
| 24 |
'del', 'dir', 'echo', 'endlocal', 'erase', 'exit', 'for', 'ftype', 'goto', |
| 25 |
'help', 'if', 'label', 'md', 'mkdir', 'mklink', 'move', 'path', 'pause', |
| 26 |
'popd', 'prompt', 'pushd', 'rd', 'rem', 'ren', 'rename', 'rmdir', 'set', |
| 27 |
'setlocal', 'shift', 'start', 'time', 'title', 'type', 'ver', 'vol', |
| 28 |
]; |
| 29 |
|
| 30 |
private $suffixes = []; |
| 31 |
|
| 32 |
/** |
| 33 |
* Replaces default suffixes of executable. |
| 34 |
*/ |
| 35 |
public function setSuffixes(array $suffixes) |
| 36 |
{ |
| 37 |
$this->suffixes = $suffixes; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Adds new possible suffix to check for executable. |
| 42 |
*/ |
| 43 |
public function addSuffix(string $suffix) |
| 44 |
{ |
| 45 |
$this->suffixes[] = $suffix; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Finds an executable by name. |
| 50 |
* |
| 51 |
* @param string $name The executable name (without the extension) |
| 52 |
* @param string|null $default The default to return if no executable is found |
| 53 |
* @param array $extraDirs Additional dirs to check into |
| 54 |
* |
| 55 |
* @return string|null |
| 56 |
*/ |
| 57 |
public function find(string $name, ?string $default = null, array $extraDirs = []) |
| 58 |
{ |
| 59 |
// windows built-in commands that are present in cmd.exe should not be resolved using PATH as they do not exist as exes |
| 60 |
if ('\\' === \DIRECTORY_SEPARATOR && \in_array(strtolower($name), self::CMD_BUILTINS, true)) { |
| 61 |
return $name; |
| 62 |
} |
| 63 |
|
| 64 |
$dirs = array_merge( |
| 65 |
explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')), |
| 66 |
$extraDirs |
| 67 |
); |
| 68 |
|
| 69 |
$suffixes = []; |
| 70 |
if ('\\' === \DIRECTORY_SEPARATOR) { |
| 71 |
$pathExt = getenv('PATHEXT'); |
| 72 |
$suffixes = $this->suffixes; |
| 73 |
$suffixes = array_merge($suffixes, $pathExt ? explode(\PATH_SEPARATOR, $pathExt) : ['.exe', '.bat', '.cmd', '.com']); |
| 74 |
} |
| 75 |
$suffixes = '' !== pathinfo($name, PATHINFO_EXTENSION) ? array_merge([''], $suffixes) : array_merge($suffixes, ['']); |
| 76 |
foreach ($suffixes as $suffix) { |
| 77 |
foreach ($dirs as $dir) { |
| 78 |
if ('' === $dir) { |
| 79 |
$dir = '.'; |
| 80 |
} |
| 81 |
if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) { |
| 82 |
return $file; |
| 83 |
} |
| 84 |
|
| 85 |
if (!@is_dir($dir) && basename($dir) === $name.$suffix && @is_executable($dir)) { |
| 86 |
return $dir; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if ('\\' === \DIRECTORY_SEPARATOR || !\function_exists('exec') || \strlen($name) !== strcspn($name, '/'.\DIRECTORY_SEPARATOR)) { |
| 92 |
return $default; |
| 93 |
} |
| 94 |
|
| 95 |
$execResult = exec('command -v -- '.escapeshellarg($name)); |
| 96 |
|
| 97 |
if (($executablePath = substr($execResult, 0, strpos($execResult, \PHP_EOL) ?: null)) && @is_executable($executablePath)) { |
| 98 |
return $executablePath; |
| 99 |
} |
| 100 |
|
| 101 |
return $default; |
| 102 |
} |
| 103 |
} |
| 104 |
|