Is there a way to get the version for a package installed with Composer, and which is currently used by an application, without invoking composer show -i
or anything similar?
I want to determinate the versions currently used by the application and and show an alert if some packages need to be updated, and eventually auto update.
Now, in the Variable Name type Path and in the Variable Value, paste the php location, i.e., C:\xampp\php. You can check your installed composer version using a command composer -v at the current path. Such as: composer -v.
The best place to look for available versions for composer packages is Packagist since that's the place composer loads the versions from when you install packages. The monolog versions are listed on http://packagist.org/packages/monolog/monolog. Save this answer. Show activity on this post.
To change to version one run the self-update command and pass in the –1 flag. This will change composer to version one and now you can install your dependencies. Once you have installed your dependencies, now you can run the same command and pass in –2 as the flag and this will switch back to composer version 2.
For the current task, I think the appropriate solution will be the following.
Composer creates a installed.json file under vendor/composer, which contains all the information about the installed packages, as they are defined.
The file looks something similar to this.
[
{
"name": "vendor_X/package_Y",
"version": "1.0.0",
"version_normalized": "1.1.0.0",
"source": {},
"dist": {},
"require": {},
"require-dev": {},
other data about the package
},
{"..other package's data..": ""},
{"...": ""}
]
A simple solution will be using the following code.
$data = array();
$packages = json_decode(file_get_contents('../vendor/composer/installed.json'));
// Assuming that the project root is one level above the web root.
foreach ($packages as $package) {
$data[$package['name']] = $package['version'];
}
// Make a cURL request to packagist.org to get the package data
// https://packagist.org/packages/vendor_X/package_Y.json
// The output is something like the following
/*
{
"package": {
"name": "omnipay/dummy",
"versions": {
"dev-master": {},
"v1.1.0": {},
"v1.0.0": {}
},
"type": "library",
}
}
*/
$packagist = json_decode($packagist_reponse_as_json);
if (strcmp($data['vendor_X/package_Y'], $packagist['package']['versions'][1]) < 0) {
// Fire a composer update, send email alert, show notification, or call the president.
}
The solution above is the simplest and a little ugly, but it shows the key points, namely, where to get local versions for packages and how to get the package versions from Composer.
A more practical solution should employ caching and should not be done during normal application operation; a better solution would be using a cron job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With