0

Is there a way, within PHP, to get a list of all libraries loaded by Composer's /vendor/autoload.php file?

Example:

require_once("./composer/vendor/autoload.php");
// In some way, get a list of libraries that were loaded:
$libraries = composer_get_libraries();  // you get the idea

I can't seem to find this in the Composer docs or google...

It's easy to list the installed libraries ('composer show') but this doesn't show which ones are autoloaded in PHP. The reason I'm asking is that I just installed a new library with composer require [library name], but in PHP the library's class is not found. I'm trying to debug this problem and it would be helpful to ensure the autoload.php is actually loading this library.

Even enabling a "debugger" option that would dump the autoloader activity to a log file would be helpful.

Update: looking into the When unit test code, I see there must be a

use When\When;

before instantiating a When object.

Ryan Griggs
  • 2,457
  • 2
  • 35
  • 58
  • The list of all installed packages is located in `composer.lock`. You can load it from there... – Jirka Hrazdil Jun 21 '18 at 18:44
  • @JiriHrazdil As stated above, I want to do this from within PHP, and I want to get a list of libraries/packages that were *actually loaded* by the autoloader. – Ryan Griggs Jun 21 '18 at 18:45
  • Have a look at [https://stackoverflow.com/questions/36676074/php-how-to-get-all-classes-when-using-autoloader](https://stackoverflow.com/questions/36676074/php-how-to-get-all-classes-when-using-autoloader), you may get some options from there. – Jirka Hrazdil Jun 21 '18 at 18:48
  • I believe you just want to find out the reason why you cannot refer to class from composer package you've installed, right? are any other classes accessible? – skyboyer Jun 21 '18 at 18:49
  • @skyboyer Correct. Yes other classes/packages are available. Specifically, I'm trying to load the When library by tplaner, but when attempting to instantiate a When object, I get Class Not Found error. – Ryan Griggs Jun 21 '18 at 18:49
  • 2
    You could use the `get_included_files()` function to see what files were loaded which might help... – Matthew Knill Oct 28 '20 at 23:23

1 Answers1

3

PHP itself has no notion of a library or a package, only the functions, classes, etc, it defines.

Composer, in turn, has no notion of how the code is being used; it may register one or more autoloaders using the spl_autoload_register function, based on the configuration of the installed packages, but it can also include certain files on every page load, which might register their own autoloader, or just define classes and functions directly.

There is therefore no concept anywhere of "the autoloader loading that library" - there might be a combined PSR-4 autoloader built by Composer, plus a combined classmap autoloader, plus a bunch of custom autoloaders, none of which have any need to "remember" which package they belong to.

When you ask for the class, PHP runs all the autoloaders that are registered, and each has a chance to define the class, usually by including a file.

All of this happens in PHP code, so a debugger such as XDebug can be used to see what autoloaders are actually run, and what each does, but beware that they may be hard-to-read machine-generated code.

A class not being autoloaded suggests one of three things:

  • Composer has not generated the correct autoloader code. You might try running composer dump-autoload
  • The package contains incorrect autoload information in its composer.json. This is unlikely unless nobody else has installed it using Composer.
  • You have made a mistake in the way you referenced the class, for instance missing the namespace, or mistyping it in a use statement.
IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • 1
    I already ran composer dump-autoload. The package is tplaner/when, having 40,000+ installs. I referenced the class exactly as shown in the docs: $x = new When(); Did I miss something? – Ryan Griggs Jun 21 '18 at 19:31
  • Update: looking at the unit tests, I see they require a "use When\When;" before using the class. Solved! – Ryan Griggs Jun 21 '18 at 19:33