What is the cleanest way to check whether a package (of any version) is installed/present, using PHP within our application?
Basically, inside our application we want to call a function with the following signature:
bool function hasComposerPackage(string $packageName)
What would this function have to contain so that we can do something like:
if (hasComposerPackage('phpunit/phpunit')) {
echo 'PHPUnit is installed!';
}
Ideally this needs to happen without any command-line exec calls and it should not autoload any unnecessary files in the process.
@user1132363 using shell_exec()
to run something like composer show
is the only way to know for sure, but you seem to refuse to want to go this route. I'm not sure why you refuse, this is the solution to your problem. There is no other reliable means. Using class_exists
is also unreliable as class names can change in packages.
That said, I think there's a bigger question here that you aren't asking: What problem are you actually trying to solve? As in, why do you need to check to see if a package is installed?