Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get version of Composer package(s) in PHP, without using the command line

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.

like image 760
DaGhostman Dimitrov Avatar asked Jul 15 '14 10:07

DaGhostman Dimitrov


People also ask

How do I know which version of composer is installed?

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.

How do I find the composer of my server?

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.

How do I change the composer package version?

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.


1 Answers

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.

like image 95
DaGhostman Dimitrov Avatar answered Oct 03 '22 06:10

DaGhostman Dimitrov