Search code examples
symfonycomposer-php

Get version from composer.json [Symfony]


I wonder if there is a way to get the composer.json version from a controller with Symfony. My composer.json looks like:

{
    "name": "myProject",
    "version": "0.0.0",
    "description": "myProject description",
    "license": "AGPL-3.0",
    "type": "project",
    "require": {
        "php": "^7.1.3",
        ...
    }
}

I can't find any reference to this.

PS: I'm using Symfony 4.


Solution

  • You can use the PackageVersions library and its relative PrettyPackageVersions.

    They provide a single class API to get the currently installed version of your Composer dependencies, for example:

    use PackageVersions\Versions;
    use Jean85\PrettyVersions;
    
    // Will output "1.0.0@0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"
    echo Versions::getVersion('myvendor/mypackage');
    
    // Will output "1.0.0"
    echo (string) PrettyVersions::getVersion('myvendor/mypackage');