Skip to content

Commit

Permalink
Suggest platform packages + PHP 7.1+ features
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Dec 1, 2021
1 parent c879176 commit e4a71ac
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
42 changes: 30 additions & 12 deletions src/Composer/Command/BaseCommand.php
Expand Up @@ -228,12 +228,12 @@ protected function getPreferredInstallOptions(Config $config, InputInterface $in
}

/**
* @return bool
* Suggestion values for "prefer-install" option
*/
protected function completePreferInstall(CompletionInput $input, CompletionSuggestions $suggestions)
{
if ($input->mustSuggestOptionValuesFor('prefer-install')) {
$suggestions->suggestValues(array('dist', 'source', 'auto'));
$suggestions->suggestValues(['dist', 'source', 'auto']);

return true;
}
Expand All @@ -242,9 +242,9 @@ protected function completePreferInstall(CompletionInput $input, CompletionSugge
}

/**
* @return bool
* Suggest package names from installed ones.
*/
protected function completeInstalledPackage(CompletionInput $input, CompletionSuggestions $suggestions, $withPlatform = false)
protected function completeInstalledPackage(CompletionInput $input, CompletionSuggestions $suggestions, $withPlatform = false): bool
{
if (!$input->mustSuggestArgumentValuesFor('packages') &&
!$input->mustSuggestArgumentValuesFor('package') &&
Expand All @@ -254,19 +254,17 @@ protected function completeInstalledPackage(CompletionInput $input, CompletionSu
}

$composer = $this->getComposer();
$installedRepos = array(
new RootPackageRepository(clone $composer->getPackage()),
);
$installedRepos = [new RootPackageRepository(clone $composer->getPackage())];

$locker = $composer->getLocker();
if ($locker->isLocked()) {
if ($withPlatform) {
$installedRepos[] = new PlatformRepository(array(), $locker->getPlatformOverrides());
$installedRepos[] = new PlatformRepository([], $locker->getPlatformOverrides());
}
$installedRepos[] = $locker->getLockedRepository(true);
} else {
if ($withPlatform) {
$installedRepos[] = new PlatformRepository(array(), $composer->getConfig()->get('platform') ?: array());
$installedRepos[] = new PlatformRepository([], $composer->getConfig()->get('platform') ?: []);
}
$installedRepos[] = $composer->getRepositoryManager()->getLocalRepository();
}
Expand All @@ -280,9 +278,9 @@ protected function completeInstalledPackage(CompletionInput $input, CompletionSu
}

/**
* @return bool
* Suggest package names available on all configured repositories.
*/
protected function completeAvailablePackage(CompletionInput $input, CompletionSuggestions $suggestions)
protected function completeAvailablePackage(CompletionInput $input, CompletionSuggestions $suggestions): bool
{
if (!$input->mustSuggestArgumentValuesFor('packages') &&
!$input->mustSuggestArgumentValuesFor('package') &&
Expand All @@ -295,7 +293,7 @@ protected function completeAvailablePackage(CompletionInput $input, CompletionSu
$composer = $this->getComposer();
$repos = new CompositeRepository($composer->getRepositoryManager()->getRepositories());

$packages = $repos->search('^'.$input->getCompletionValue(), RepositoryInterface::SEARCH_NAME);
$packages = $repos->search('^'.preg_quote($input->getCompletionValue()), RepositoryInterface::SEARCH_NAME);

foreach (array_slice($packages, 0, 150) as $package) {
$suggestions->suggestValue($package['name']);
Expand All @@ -304,6 +302,26 @@ protected function completeAvailablePackage(CompletionInput $input, CompletionSu
return true;
}

/**
* Suggests ext- packages from the ones available on the currently-running PHP
*/
protected function completePlatformPackage(CompletionInput $input, CompletionSuggestions $suggestions): bool
{
if (!$input->mustSuggestOptionValuesFor('require') &&
!$input->mustSuggestOptionValuesFor('require-dev') &&
!str_starts_with($input->getCompletionValue(), 'ext-')
) {
return false;
}

$repos = new PlatformRepository([], $this->getComposer()->getConfig()->get('platform') ?: []);
$suggestions->suggestValues(array_map(function (PackageInterface $package) {
return $package->getName();
}, $repos->getPackages()));

return true;
}

/**
* @param array<string, string> $requirements
*
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/RequireCommand.php
Expand Up @@ -61,7 +61,7 @@ class RequireCommand extends InitCommand

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
$this->completeAvailablePackage($input, $suggestions);
$this->completePlatformPackage($input, $suggestions) || $this->completeAvailablePackage($input, $suggestions);
}

/**
Expand Down

0 comments on commit e4a71ac

Please sign in to comment.