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

Breadcrumb

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

function ComposerRepository::search

@inheritDoc

Overrides ArrayRepository::search

File

vendor/composer/composer/src/Composer/Repository/ComposerRepository.php, line 551

Class

ComposerRepository
@author Jordi Boggiano <j.boggiano@seld.be>

Namespace

Composer\Repository

Code

public function search(string $query, int $mode = 0, ?string $type = null) {
    $this->loadRootServerFile(600);
    if ($this->searchUrl && $mode === self::SEARCH_FULLTEXT) {
        $url = str_replace([
            '%query%',
            '%type%',
        ], [
            urlencode($query),
            $type,
        ], $this->searchUrl);
        $search = $this->httpDownloader
            ->get($url, $this->options)
            ->decodeJson();
        if (empty($search['results'])) {
            return [];
        }
        $results = [];
        foreach ($search['results'] as $result) {
            // do not show virtual packages in results as they are not directly useful from a composer perspective
            if (!empty($result['virtual'])) {
                continue;
            }
            $results[] = $result;
        }
        return $results;
    }
    if ($mode === self::SEARCH_VENDOR) {
        $results = [];
        $regex = '{(?:' . implode('|', Preg::split('{\\s+}', $query)) . ')}i';
        $vendorNames = $this->getVendorNames();
        foreach (Preg::grep($regex, $vendorNames) as $name) {
            $results[] = [
                'name' => $name,
                'description' => '',
            ];
        }
        return $results;
    }
    if ($this->hasProviders() || $this->lazyProvidersUrl) {
        // optimize search for "^foo/bar" where at least "^foo/" is present by loading this directly from the listUrl if present
        if (Preg::isMatchStrictGroups('{^\\^(?P<query>(?P<vendor>[a-z0-9_.-]+)/[a-z0-9_.-]*)\\*?$}i', $query, $match) && $this->listUrl !== null) {
            $url = $this->listUrl . '?vendor=' . urlencode($match['vendor']) . '&filter=' . urlencode($match['query'] . '*');
            $result = $this->httpDownloader
                ->get($url, $this->options)
                ->decodeJson();
            $results = [];
            foreach ($result['packageNames'] as $name) {
                $results[] = [
                    'name' => $name,
                    'description' => '',
                ];
            }
            return $results;
        }
        $results = [];
        $regex = '{(?:' . implode('|', Preg::split('{\\s+}', $query)) . ')}i';
        $packageNames = $this->getPackageNames();
        foreach (Preg::grep($regex, $packageNames) as $name) {
            $results[] = [
                'name' => $name,
                'description' => '',
            ];
        }
        return $results;
    }
    return parent::search($query, $mode);
}
RSS feed
Powered by Drupal