How can I get hold of the Composer object?

Usually, I’m doing this using ReflectionClass.

Take this example: to load the cacert file which comes inside one of composer packages:

  • First, locate any class in that package using ReflectionClass.
  • Secondly, get the package path using dirname bypassing the file name.

Code example:

// Load certificate file.
$reflection = new ReflectionClass(GSRequest::class);
$packagePath = dirname($reflection->getFileName());
$gsRequest::setCAFile(realpath($packagePath . '/cacert.pem'));