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