Reputation: 73
For a Symfony based console application, I need to read the it's version number from it's composer file (not branch alias), programmatically.
Manually, I can use the composer show -s
to get the root package information, but there does not seem to be any command to to get the clean version of my package.
The purpose is to automatically display the installed package version, when running the application, without having to deal with *.txt files or other file-based ways that contain a semantic version number.
Sadly I am not familiar with composer's architecture, so I have no idea what components I can use to achieve this.
Any ideas or perhaps already made packages that solves is mostly welcome.
Upvotes: 2
Views: 1990
Reputation: 147
Install Composer in your project
composer require composer/composer --dev
example.php
<?php
// Include Composer Autoload
require __DIR__ . '/vendor/autoload.php';
use Composer\Factory;
use Composer\IO\NullIO;
$composer = Factory::create(new NullIo(), './composer.json', false);
$localRepo = $composer->getRepositoryManager()->getLocalRepository();
foreach ($localRepo->getPackages() as $package) {
echo $package->getName() . PHP_EOL;
echo $package->getVersion() . PHP_EOL;
echo $package->getType() . PHP_EOL;
// ...
}
Upvotes: 5