Jake B.
Well-known member
This is what I'm using currently to autoload any classes I'm requiring through Composer for add-ons. It hasn't been thoroughly tested yet to make sure everything works properly if two add-ons are including the same packages, but from my brief testing it seems to function. For the most part it's pulled directly out of Composer so I can't imagine there being any major issues
Just create an app_setup event listener that points to this method:
obviously you'll replace
If anyone more knowledgeable with the inner workings of XF's autoloader and composer sees any issues with this do let me know
Just create an app_setup event listener that points to this method:
Code:
public static function autoloadClasses()
{
$composerDirectory = \XF::getAddOnDirectory() . '/VendorPrefix/AddOn/vendor/composer';
$loader = \XF::$autoLoader;
$map = require $composerDirectory . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$map = require $composerDirectory . '/autoload_psr4.php';
foreach ($map as $namespace => $path) {
$loader->setPsr4($namespace, $path);
}
$classMap = require $composerDirectory . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
}
obviously you'll replace
VendorPrefix/AddOn
with your Add-on's ID, you can probably also use __DIR__ instead if the file is in the root directory of your add-on, or __DIR__ . '/../'
if it's in an Listeners
directory. This will load namespaces, classmaps, etc directly from vendor/composer/* and register all of the classes for you automagically If anyone more knowledgeable with the inner workings of XF's autoloader and composer sees any issues with this do let me know
Last edited: