Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ExecutableFinder.php

function ExecutableFinder::find

Same name in this branch
  1. 11.1.x vendor/php-tuf/composer-stager/src/Internal/Finder/Service/ExecutableFinder.php \PhpTuf\ComposerStager\Internal\Finder\Service\ExecutableFinder::find()
  2. 11.1.x core/modules/package_manager/src/ExecutableFinder.php \Drupal\package_manager\ExecutableFinder::find()

Finds an executable by name.

Parameters

string $name The executable name (without the extension):

string|null $default The default to return if no executable is found:

array $extraDirs Additional dirs to check into:

File

vendor/symfony/process/ExecutableFinder.php, line 58

Class

ExecutableFinder
Generic executable finder.

Namespace

Symfony\Component\Process

Code

public function find(string $name, ?string $default = null, array $extraDirs = []) : ?string {
    // windows built-in commands that are present in cmd.exe should not be resolved using PATH as they do not exist as exes
    if ('\\' === \DIRECTORY_SEPARATOR && \in_array(strtolower($name), self::CMD_BUILTINS, true)) {
        return $name;
    }
    $dirs = array_merge(explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')), $extraDirs);
    $suffixes = $this->suffixes;
    if ('\\' === \DIRECTORY_SEPARATOR) {
        $pathExt = getenv('PATHEXT');
        $suffixes = array_merge($suffixes, $pathExt ? explode(\PATH_SEPARATOR, $pathExt) : [
            '.exe',
            '.bat',
            '.cmd',
            '.com',
        ]);
    }
    $suffixes = '' !== pathinfo($name, PATHINFO_EXTENSION) ? array_merge([
        '',
    ], $suffixes) : array_merge($suffixes, [
        '',
    ]);
    foreach ($suffixes as $suffix) {
        foreach ($dirs as $dir) {
            if ('' === $dir) {
                $dir = '.';
            }
            if (@is_file($file = $dir . \DIRECTORY_SEPARATOR . $name . $suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) {
                return $file;
            }
            if (!@is_dir($dir) && basename($dir) === $name . $suffix && @is_executable($dir)) {
                return $dir;
            }
        }
    }
    if ('\\' === \DIRECTORY_SEPARATOR || !\function_exists('exec') || \strlen($name) !== strcspn($name, '/' . \DIRECTORY_SEPARATOR)) {
        return $default;
    }
    $execResult = exec('command -v -- ' . escapeshellarg($name));
    if (($executablePath = substr($execResult, 0, strpos($execResult, \PHP_EOL) ?: null)) && @is_executable($executablePath)) {
        return $executablePath;
    }
    return $default;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal