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

Breadcrumb

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

function CompletionTrait::suggestInstalledPackage

Suggest package names from installed.

File

vendor/composer/composer/src/Composer/Command/CompletionTrait.php, line 63

Class

CompletionTrait
Adds completion to arguments and options.

Namespace

Composer\Command

Code

private function suggestInstalledPackage(bool $includeRootPackage = true, bool $includePlatformPackages = false) : \Closure {
    return function (CompletionInput $input) use ($includeRootPackage, $includePlatformPackages) : array {
        $composer = $this->requireComposer();
        $installedRepos = [];
        if ($includeRootPackage) {
            $installedRepos[] = new RootPackageRepository(clone $composer->getPackage());
        }
        $locker = $composer->getLocker();
        if ($locker->isLocked()) {
            $installedRepos[] = $locker->getLockedRepository(true);
        }
        else {
            $installedRepos[] = $composer->getRepositoryManager()
                ->getLocalRepository();
        }
        $platformHint = [];
        if ($includePlatformPackages) {
            if ($locker->isLocked()) {
                $platformRepo = new PlatformRepository([], $locker->getPlatformOverrides());
            }
            else {
                $platformRepo = new PlatformRepository([], $composer->getConfig()
                    ->get('platform'));
            }
            if ($input->getCompletionValue() === '') {
                // to reduce noise, when no text is yet entered we list only two entries for ext- and lib- prefixes
                $hintsToFind = [
                    'ext-' => 0,
                    'lib-' => 0,
                    'php' => 99,
                    'composer' => 99,
                ];
                foreach ($platformRepo->getPackages() as $pkg) {
                    foreach ($hintsToFind as $hintPrefix => $hintCount) {
                        if (str_starts_with($pkg->getName(), $hintPrefix)) {
                            if ($hintCount === 0 || $hintCount >= 99) {
                                $platformHint[] = $pkg->getName();
                                $hintsToFind[$hintPrefix]++;
                            }
                            elseif ($hintCount === 1) {
                                unset($hintsToFind[$hintPrefix]);
                                $platformHint[] = substr($pkg->getName(), 0, max(strlen($pkg->getName()) - 3, strlen($hintPrefix) + 1)) . '...';
                            }
                            continue 2;
                        }
                    }
                }
            }
            else {
                $installedRepos[] = $platformRepo;
            }
        }
        $installedRepo = new InstalledRepository($installedRepos);
        return array_merge(array_map(static function (PackageInterface $package) {
            return $package->getName();
        }, $installedRepo->getPackages()), $platformHint);
    };
}

API Navigation

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