function BumpCommand::doBump
Parameters
string[] $packagesFilter:
Throws
\Seld\JsonLint\ParsingException
1 call to BumpCommand::doBump()
- BumpCommand::execute in vendor/
composer/ composer/ src/ Composer/ Command/ BumpCommand.php
File
-
vendor/
composer/ composer/ src/ Composer/ Command/ BumpCommand.php, line 89
Class
- BumpCommand
- @author Jordi Boggiano <j.boggiano@seld.be>
Namespace
Composer\CommandCode
public function doBump(IOInterface $io, bool $devOnly, bool $noDevOnly, bool $dryRun, array $packagesFilter) : int {
/** @readonly */
$composerJsonPath = Factory::getComposerFile();
if (!Filesystem::isReadable($composerJsonPath)) {
$io->writeError('<error>' . $composerJsonPath . ' is not readable.</error>');
return self::ERROR_GENERIC;
}
$composerJson = new JsonFile($composerJsonPath);
$contents = file_get_contents($composerJson->getPath());
if (false === $contents) {
$io->writeError('<error>' . $composerJsonPath . ' is not readable.</error>');
return self::ERROR_GENERIC;
}
// check for writability by writing to the file as is_writable can not be trusted on network-mounts
// see https://github.com/composer/composer/issues/8231 and https://bugs.php.net/bug.php?id=68926
if (!is_writable($composerJsonPath) && false === Silencer::call('file_put_contents', $composerJsonPath, $contents)) {
$io->writeError('<error>' . $composerJsonPath . ' is not writable.</error>');
return self::ERROR_GENERIC;
}
unset($contents);
$composer = $this->requireComposer();
if ($composer->getLocker()
->isLocked()) {
if (!$composer->getLocker()
->isFresh()) {
$io->writeError('<error>The lock file is not up to date with the latest changes in composer.json. Run the appropriate `update` to fix that before you use the `bump` command.</error>');
return self::ERROR_LOCK_OUTDATED;
}
$repo = $composer->getLocker()
->getLockedRepository(true);
}
else {
$repo = $composer->getRepositoryManager()
->getLocalRepository();
}
if ($composer->getPackage()
->getType() !== 'project' && !$devOnly) {
$io->writeError('<warning>Warning: Bumping dependency constraints is not recommended for libraries as it will narrow down your dependencies and may cause problems for your users.</warning>');
$contents = $composerJson->read();
if (!isset($contents['type'])) {
$io->writeError('<warning>If your package is not a library, you can explicitly specify the "type" by using "composer config type project".</warning>');
$io->writeError('<warning>Alternatively you can use --dev-only to only bump dependencies within "require-dev".</warning>');
}
unset($contents);
}
$bumper = new VersionBumper();
$tasks = [];
if (!$devOnly) {
$tasks['require'] = $composer->getPackage()
->getRequires();
}
if (!$noDevOnly) {
$tasks['require-dev'] = $composer->getPackage()
->getDevRequires();
}
if (count($packagesFilter) > 0) {
$pattern = BasePackage::packageNamesToRegexp(array_unique(array_map('strtolower', $packagesFilter)));
foreach ($tasks as $key => $reqs) {
foreach ($reqs as $pkgName => $link) {
if (!Preg::isMatch($pattern, $pkgName)) {
unset($tasks[$key][$pkgName]);
}
}
}
}
$updates = [];
foreach ($tasks as $key => $reqs) {
foreach ($reqs as $pkgName => $link) {
if (PlatformRepository::isPlatformPackage($pkgName)) {
continue;
}
$currentConstraint = $link->getPrettyConstraint();
$package = $repo->findPackage($pkgName, '*');
// name must be provided or replaced
if (null === $package) {
continue;
}
while ($package instanceof AliasPackage) {
$package = $package->getAliasOf();
}
$bumped = $bumper->bumpRequirement($link->getConstraint(), $package);
if ($bumped === $currentConstraint) {
continue;
}
$updates[$key][$pkgName] = $bumped;
}
}
if (!$dryRun && !$this->updateFileCleanly($composerJson, $updates)) {
$composerDefinition = $composerJson->read();
foreach ($updates as $key => $packages) {
foreach ($packages as $package => $version) {
$composerDefinition[$key][$package] = $version;
}
}
$composerJson->write($composerDefinition);
}
$changeCount = array_sum(array_map('count', $updates));
if ($changeCount > 0) {
if ($dryRun) {
$io->write('<info>' . $composerJsonPath . ' would be updated with:</info>');
foreach ($updates as $requireType => $packages) {
foreach ($packages as $package => $version) {
$io->write(sprintf('<info> - %s.%s: %s</info>', $requireType, $package, $version));
}
}
}
else {
$io->write('<info>' . $composerJsonPath . ' has been updated (' . $changeCount . ' changes).</info>');
}
}
else {
$io->write('<info>No requirements to update in ' . $composerJsonPath . '.</info>');
}
if (!$dryRun && $composer->getLocker()
->isLocked() && $composer->getConfig()
->get('lock') && $changeCount > 0) {
$composer->getLocker()
->updateHash($composerJson);
}
if ($dryRun && $changeCount > 0) {
return self::ERROR_GENERIC;
}
return 0;
}