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

Breadcrumb

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

function VcsRepository::initialize

Overrides ArrayRepository::initialize

File

vendor/composer/composer/src/Composer/Repository/VcsRepository.php, line 183

Class

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

Namespace

Composer\Repository

Code

protected function initialize() {
    parent::initialize();
    $isVerbose = $this->isVerbose;
    $isVeryVerbose = $this->isVeryVerbose;
    $driver = $this->getDriver();
    if (!$driver) {
        throw new \InvalidArgumentException('No driver found to handle VCS repository ' . $this->url);
    }
    $this->versionParser = new VersionParser();
    if (!$this->loader) {
        $this->loader = new ArrayLoader($this->versionParser);
    }
    $hasRootIdentifierComposerJson = false;
    try {
        $hasRootIdentifierComposerJson = $driver->hasComposerFile($driver->getRootIdentifier());
        if ($hasRootIdentifierComposerJson) {
            $data = $driver->getComposerInformation($driver->getRootIdentifier());
            $this->packageName = !empty($data['name']) ? $data['name'] : null;
        }
    } catch (\Exception $e) {
        if ($e instanceof TransportException && $this->shouldRethrowTransportException($e)) {
            throw $e;
        }
        if ($isVeryVerbose) {
            $this->io
                ->writeError('<error>Skipped parsing ' . $driver->getRootIdentifier() . ', ' . $e->getMessage() . '</error>');
        }
    }
    foreach ($driver->getTags() as $tag => $identifier) {
        $tag = (string) $tag;
        $msg = 'Reading composer.json of <info>' . ($this->packageName ?: $this->url) . '</info> (<comment>' . $tag . '</comment>)';
        // strip the release- prefix from tags if present
        $tag = str_replace('release-', '', $tag);
        $cachedPackage = $this->getCachedPackageVersion($tag, $identifier, $isVerbose, $isVeryVerbose);
        if ($cachedPackage) {
            $this->addPackage($cachedPackage);
            continue;
        }
        if ($cachedPackage === false) {
            $this->emptyReferences[] = $identifier;
            continue;
        }
        if (!($parsedTag = $this->validateTag($tag))) {
            if ($isVeryVerbose) {
                $this->io
                    ->writeError('<warning>Skipped tag ' . $tag . ', invalid tag name</warning>');
            }
            continue;
        }
        if ($isVeryVerbose) {
            $this->io
                ->writeError($msg);
        }
        elseif ($isVerbose) {
            $this->io
                ->overwriteError($msg, false);
        }
        try {
            $data = $driver->getComposerInformation($identifier);
            if (null === $data) {
                if ($isVeryVerbose) {
                    $this->io
                        ->writeError('<warning>Skipped tag ' . $tag . ', no composer file</warning>');
                }
                $this->emptyReferences[] = $identifier;
                continue;
            }
            // manually versioned package
            if (isset($data['version'])) {
                $data['version_normalized'] = $this->versionParser
                    ->normalize($data['version']);
            }
            else {
                // auto-versioned package, read value from tag
                $data['version'] = $tag;
                $data['version_normalized'] = $parsedTag;
            }
            // make sure tag packages have no -dev flag
            $data['version'] = Preg::replace('{[.-]?dev$}i', '', $data['version']);
            $data['version_normalized'] = Preg::replace('{(^dev-|[.-]?dev$)}i', '', $data['version_normalized']);
            // make sure tag do not contain the default-branch marker
            unset($data['default-branch']);
            // broken package, version doesn't match tag
            if ($data['version_normalized'] !== $parsedTag) {
                if ($isVeryVerbose) {
                    if (Preg::isMatch('{(^dev-|[.-]?dev$)}i', $parsedTag)) {
                        $this->io
                            ->writeError('<warning>Skipped tag ' . $tag . ', invalid tag name, tags can not use dev prefixes or suffixes</warning>');
                    }
                    else {
                        $this->io
                            ->writeError('<warning>Skipped tag ' . $tag . ', tag (' . $parsedTag . ') does not match version (' . $data['version_normalized'] . ') in composer.json</warning>');
                    }
                }
                continue;
            }
            $tagPackageName = $this->packageName ?: $data['name'] ?? '';
            if ($existingPackage = $this->findPackage($tagPackageName, $data['version_normalized'])) {
                if ($isVeryVerbose) {
                    $this->io
                        ->writeError('<warning>Skipped tag ' . $tag . ', it conflicts with an another tag (' . $existingPackage->getPrettyVersion() . ') as both resolve to ' . $data['version_normalized'] . ' internally</warning>');
                }
                continue;
            }
            if ($isVeryVerbose) {
                $this->io
                    ->writeError('Importing tag ' . $tag . ' (' . $data['version_normalized'] . ')');
            }
            $this->addPackage($this->loader
                ->load($this->preProcess($driver, $data, $identifier)));
        } catch (\Exception $e) {
            if ($e instanceof TransportException) {
                $this->versionTransportExceptions['tags'][$tag] = $e;
                if ($e->getCode() === 404) {
                    $this->emptyReferences[] = $identifier;
                }
                if ($this->shouldRethrowTransportException($e)) {
                    throw $e;
                }
            }
            if ($isVeryVerbose) {
                $this->io
                    ->writeError('<warning>Skipped tag ' . $tag . ', ' . ($e instanceof TransportException ? 'no composer file was found (' . $e->getCode() . ' HTTP status code)' : $e->getMessage()) . '</warning>');
            }
            continue;
        }
    }
    if (!$isVeryVerbose) {
        $this->io
            ->overwriteError('', false);
    }
    $branches = $driver->getBranches();
    // make sure the root identifier branch gets loaded first
    if ($hasRootIdentifierComposerJson && isset($branches[$driver->getRootIdentifier()])) {
        $branches = [
            $driver->getRootIdentifier() => $branches[$driver->getRootIdentifier()],
        ] + $branches;
    }
    foreach ($branches as $branch => $identifier) {
        $branch = (string) $branch;
        $msg = 'Reading composer.json of <info>' . ($this->packageName ?: $this->url) . '</info> (<comment>' . $branch . '</comment>)';
        if ($isVeryVerbose) {
            $this->io
                ->writeError($msg);
        }
        elseif ($isVerbose) {
            $this->io
                ->overwriteError($msg, false);
        }
        if (!($parsedBranch = $this->validateBranch($branch))) {
            if ($isVeryVerbose) {
                $this->io
                    ->writeError('<warning>Skipped branch ' . $branch . ', invalid name</warning>');
            }
            continue;
        }
        // make sure branch packages have a dev flag
        if (strpos($parsedBranch, 'dev-') === 0 || VersionParser::DEFAULT_BRANCH_ALIAS === $parsedBranch) {
            $version = 'dev-' . str_replace('#', '+', $branch);
            $parsedBranch = str_replace('#', '+', $parsedBranch);
        }
        else {
            $prefix = strpos($branch, 'v') === 0 ? 'v' : '';
            $version = $prefix . Preg::replace('{(\\.9{7})+}', '.x', $parsedBranch);
        }
        $cachedPackage = $this->getCachedPackageVersion($version, $identifier, $isVerbose, $isVeryVerbose, $driver->getRootIdentifier() === $branch);
        if ($cachedPackage) {
            $this->addPackage($cachedPackage);
            continue;
        }
        if ($cachedPackage === false) {
            $this->emptyReferences[] = $identifier;
            continue;
        }
        try {
            $data = $driver->getComposerInformation($identifier);
            if (null === $data) {
                if ($isVeryVerbose) {
                    $this->io
                        ->writeError('<warning>Skipped branch ' . $branch . ', no composer file</warning>');
                }
                $this->emptyReferences[] = $identifier;
                continue;
            }
            // branches are always auto-versioned, read value from branch name
            $data['version'] = $version;
            $data['version_normalized'] = $parsedBranch;
            unset($data['default-branch']);
            if ($driver->getRootIdentifier() === $branch) {
                $data['default-branch'] = true;
            }
            if ($isVeryVerbose) {
                $this->io
                    ->writeError('Importing branch ' . $branch . ' (' . $data['version'] . ')');
            }
            $packageData = $this->preProcess($driver, $data, $identifier);
            $package = $this->loader
                ->load($packageData);
            if ($this->loader instanceof ValidatingArrayLoader && \count($this->loader
                ->getWarnings()) > 0) {
                throw new InvalidPackageException($this->loader
                    ->getErrors(), $this->loader
                    ->getWarnings(), $packageData);
            }
            $this->addPackage($package);
        } catch (TransportException $e) {
            $this->versionTransportExceptions['branches'][$branch] = $e;
            if ($e->getCode() === 404) {
                $this->emptyReferences[] = $identifier;
            }
            if ($this->shouldRethrowTransportException($e)) {
                throw $e;
            }
            if ($isVeryVerbose) {
                $this->io
                    ->writeError('<warning>Skipped branch ' . $branch . ', no composer file was found (' . $e->getCode() . ' HTTP status code)</warning>');
            }
            continue;
        } catch (\Exception $e) {
            if (!$isVeryVerbose) {
                $this->io
                    ->writeError('');
            }
            $this->branchErrorOccurred = true;
            $this->io
                ->writeError('<error>Skipped branch ' . $branch . ', ' . $e->getMessage() . '</error>');
            $this->io
                ->writeError('');
            continue;
        }
    }
    $driver->cleanup();
    if (!$isVeryVerbose) {
        $this->io
            ->overwriteError('', false);
    }
    if (!$this->getPackages()) {
        throw new InvalidRepositoryException('No valid composer.json was found in any branch or tag of ' . $this->url . ', could not load a package from it.');
    }
}
RSS feed
Powered by Drupal