is there any way to know which composer
(.phar
) version had generated the currently loaded autoloader?
Meaning, after running:
composer init
composer update
And creating a php file that includes the autoloader:
<?php
require 'vendor/autoload.php';
// ??
Inside this php file, Can i somehow determine which version of composer was used to generated the vendor
folder (hence also the autoload.php
file)?
Is there something in php, under the \Composer
namespace or whatever, that tells me that (for example) a composer v2.5.5
generated the autoloader?
Running shell_exec('composer --version')
is not a solution because
composer
binary that was used, might not be whatever the shell locates when running composer --version
proc_open
, shell_exec
, exec
, etc) might be disabled for security reasonsI also found \Composer\InstalledVersions::getAllRawData()
, \Composer\InstalledVersions::getRootPackage()
and similar, but there is no information about composer itself.
Also, final note: requiring composer/composer
in my composer.json
and then analyzing that package, is not the same thing...
thank you
I found a solution using composer's scripts, and possibly a support file.
composer.json
:
{
"scripts": {
"pre-install-cmd": [
"@composer --version"
],
"pre-update-cmd": [
"@composer --version"
],
"post-autoload-dump": [
"@composer --version >composer_version.log"
]
}
}
To call Composer commands, you can use @composer which will automatically resolve to whatever composer.phar is currently being used
Redirecting to a file might be a way to have some information in environments where you don't have access to the entire composer install
log, like GAE on GCP, for example; the problem with this particular case is that the file system is either ephemeral or read only, so who knows where the composer_version.log
file ends up, if it even gets created... but that's a different question.
EDIT:
Redirecting to a file allows for reading that file with php via a simple file_get_contents()
, which was the original question.
So, to recap:
composer.json
{
"scripts":{
"post-install-cmd": ["@composer --version >composer_version.log"]
}
}
and some file.php
:
<?php
require 'vendor/autoload.php'; // not even necessary actually
$composerVersion = file_get_contents('composer_version.log');
echo $composerVersion;
That's it.