Hi, I'm Liam

I talk about code and stuff

Detecting Autoloaders in Composer Bin Commands

Published on

If you’re building a command line application you want to distribute with Composer, like I just did with cpx, what path do you require to get the Composer autoloader so your code works?

It might seem like a simple question, but depending on where your CLI is being run from, the path to the autoloader will be different:

  1. If the CLI from its own repository (eg. for development of the CLI itself), you probably want to require vendor/autoload.php
  2. If the CLI is being run from the vendor/<vendor>/<package> directory of a project, you probably want to require ../../autoload.php
  3. If the CLI is being run from the vendor/bin directory of a project, Composer will set a variable in $GLOBALS['_composer_autoload_path'] that you can use

Taking all of this into account, here’s the code I use in cpx to detect the autoloader so it’ll work in all scenarios:

if (isset($GLOBALS['_composer_autoload_path'])) {
    require_once $GLOBALS['_composer_autoload_path'];

    unset($GLOBALS['_composer_autoload_path']);
} else {
    foreach ([__DIR__ . '/../../autoload.php', __DIR__ . '/../vendor/autoload.php', __DIR__ . '/vendor/autoload.php'] as $file) {
        if (file_exists($file)) {
            require_once $file;

            break;
        }
    }

    unset($file);
}
Photo of Liam Hammett
written by
Liam Hammett
Found a typo? Suggest a fix here!
Hit me up on Twitter / GitHub / LinkedIn / email me
Copyright © 2024 Liam Hammett and all that kind of stuff