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

Breadcrumb

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

function GitHubDriver::getFundingInfo

Return value

array<int, array{type: string, url: string}>|false

1 call to GitHubDriver::getFundingInfo()
GitHubDriver::getComposerInformation in vendor/composer/composer/src/Composer/Repository/Vcs/GitHubDriver.php
@inheritDoc

File

vendor/composer/composer/src/Composer/Repository/Vcs/GitHubDriver.php, line 202

Class

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

Namespace

Composer\Repository\Vcs

Code

private function getFundingInfo() {
    if (null !== $this->fundingInfo) {
        return $this->fundingInfo;
    }
    if ($this->originUrl !== 'github.com') {
        return $this->fundingInfo = false;
    }
    foreach ([
        $this->getApiUrl() . '/repos/' . $this->owner . '/' . $this->repository . '/contents/.github/FUNDING.yml',
        $this->getApiUrl() . '/repos/' . $this->owner . '/.github/contents/FUNDING.yml',
    ] as $file) {
        try {
            $response = $this->httpDownloader
                ->get($file, [
                'retry-auth-failure' => false,
            ])
                ->decodeJson();
        } catch (TransportException $e) {
            continue;
        }
        if (empty($response['content']) || $response['encoding'] !== 'base64' || !($funding = base64_decode($response['content']))) {
            continue;
        }
        break;
    }
    if (empty($funding)) {
        return $this->fundingInfo = false;
    }
    $result = [];
    $key = null;
    foreach (Preg::split('{\\r?\\n}', $funding) as $line) {
        $line = trim($line);
        if (Preg::isMatchStrictGroups('{^(\\w+)\\s*:\\s*(.+)$}', $line, $match)) {
            if ($match[2] === '[') {
                $key = $match[1];
                continue;
            }
            if (Preg::isMatchStrictGroups('{^\\[(.*?)\\](?:\\s*#.*)?$}', $match[2], $match2)) {
                foreach (array_map('trim', Preg::split('{[\'"]?\\s*,\\s*[\'"]?}', $match2[1])) as $item) {
                    $result[] = [
                        'type' => $match[1],
                        'url' => trim($item, '"\' '),
                    ];
                }
            }
            elseif (Preg::isMatchStrictGroups('{^([^#].*?)(?:\\s+#.*)?$}', $match[2], $match2)) {
                $result[] = [
                    'type' => $match[1],
                    'url' => trim($match2[1], '"\' '),
                ];
            }
            $key = null;
        }
        elseif (Preg::isMatchStrictGroups('{^(\\w+)\\s*:\\s*#\\s*$}', $line, $match)) {
            $key = $match[1];
        }
        elseif ($key !== null && (Preg::isMatchStrictGroups('{^-\\s*(.+)(?:\\s+#.*)?$}', $line, $match) || Preg::isMatchStrictGroups('{^(.+),(?:\\s*#.*)?$}', $line, $match))) {
            $result[] = [
                'type' => $key,
                'url' => trim($match[1], '"\' '),
            ];
        }
        elseif ($key !== null && $line === ']') {
            $key = null;
        }
    }
    foreach ($result as $key => $item) {
        switch ($item['type']) {
            case 'tidelift':
                $result[$key]['url'] = 'https://tidelift.com/funding/github/' . $item['url'];
                break;
            case 'github':
                $result[$key]['url'] = 'https://github.com/' . basename($item['url']);
                break;
            case 'patreon':
                $result[$key]['url'] = 'https://www.patreon.com/' . basename($item['url']);
                break;
            case 'otechie':
                $result[$key]['url'] = 'https://otechie.com/' . basename($item['url']);
                break;
            case 'open_collective':
                $result[$key]['url'] = 'https://opencollective.com/' . basename($item['url']);
                break;
            case 'liberapay':
                $result[$key]['url'] = 'https://liberapay.com/' . basename($item['url']);
                break;
            case 'ko_fi':
                $result[$key]['url'] = 'https://ko-fi.com/' . basename($item['url']);
                break;
            case 'issuehunt':
                $result[$key]['url'] = 'https://issuehunt.io/r/' . $item['url'];
                break;
            case 'community_bridge':
                $result[$key]['url'] = 'https://funding.communitybridge.org/projects/' . basename($item['url']);
                break;
            case 'buy_me_a_coffee':
                $result[$key]['url'] = 'https://www.buymeacoffee.com/' . basename($item['url']);
                break;
        }
    }
    return $this->fundingInfo = $result;
}
RSS feed
Powered by Drupal