Skip to content

Instantly share code, notes, and snippets.

@dg
Last active February 13, 2023 14:14
Show Gist options
  • Save dg/734bebf55cf28ad6a5de1156d3099bff to your computer and use it in GitHub Desktop.
Save dg/734bebf55cf28ad6a5de1156d3099bff to your computer and use it in GitHub Desktop.
Composer Frontline: Updates all the version constraints of dependencies in the composer.json file to their latest version.
<?php
declare(strict_types=1);
// Updates all the version constraints of dependencies in the composer.json file to their latest version.
//
// usage: composer-frontline.php (updates all Nette packages)
// composer-frontline.php doctrine/* (updates all Doctrine packages)
// composer-frontline.php * (updates all packages)
$composer = json_decode(file_get_contents('composer.json'));
if (isset($composer->{'minimum-stability'}) && $composer->{'minimum-stability'} !== 'stable') {
echo "Please change 'minimum-stability' to 'stable in order to continue.\n";
exit;
}
exec('composer outdated -D --format=json', $output, $error);
if ($error) {
exit(1);
}
$masks = array_slice($argv, 1) ?: ['nette/*', 'tracy/*', 'latte/*'];
$outdated = json_decode(implode($output));
$upgrade = [];
foreach ($outdated->installed as $package) {
foreach ($masks as $mask) {
if (fnmatch($mask, $package->name)) {
$mode = isset($composer->require->{$package->name}) ? '' : '--dev';
$upgrade[$mode][] = $package->name;
continue 2;
}
}
}
if (!$upgrade) {
echo "nothing to update\n";
exit;
}
foreach ($upgrade as $mode => $packages) {
passthru('composer --no-update --no-scripts require ' . $mode . ' ' . implode(' ', $packages));
}
@dg
Copy link
Author

dg commented Jan 4, 2021

When you install a package using composer require vendor/package, a entry for example "vendor/package": "^1.4" is added to the composer.json file. It means that Composer can update to patch and minor releases: 1.4.1, 1.5.0 and so on. But not to major version, which means, in this example, 2.0 and higher.

To discover new releases of the packages, you run composer outdated. Some of those updates can be major releases. Running composer update won’t update the version of those.

To update to a new major versions, use this tool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment