Registration of additional (3rd party) namespaces.

There are multiple ways in xautoload to register namespaces for 3rd party code.

A good idea is to look at xautoload.api.php, which is usually more up to date than this documentation page.

Direct registration (not recommended)

The most straightforward one direct registration:

function hook_{boot|init|..}() {
  // PSR-0
  xautoload()->finder->addPsr0($namespace, $dir);
}

The problem here is that you need to decide from where you do the registration.
You could do it from hook_boot() or hook_init(), but is that early enough?

Also, can you be sure that xautoload is installed and on the correct version? Otherwise you might see a WSOD..

Recommended: Using hook_xautoload()

Documentation moved here.

Support for Libraries API

Documentation moved here.

Class map discovery

Documentation moved here

Support for composer libraries?

If you are planning to pull in Composer libraries for your Drupal 7 modules, you should definitely have a look at Composer manager. This allows to automatically download the required Composer libraries, and it will generate an autoload.php. All of that completely without xautoload.

This being said, there are some ways that xautoload can help out with Composer libraries.

First, the XAutoload ClassFinder has registration methods very similar to those of Composer's ClassLoader.

Secondly, the ClassFinderAdapter classes in xautoload, that are passed into hook_xautoload() and friends have some convenience methods that are designed both for raw downloaded Composer libraries, and for Composer directories where Composer has already downloaded the dependencies and generated an autoload.php file.

// Parse the raw composer.json provided by a Composer package for autoload information. This will even trigger a scan for classmap generation, if the "classmap" setting is used.
$adapter->composerJson('path/to/composer.json');
// Scan a directory for Composer-generated autoload files.
$adapter->composerDir('[..]/vendor/composer');

What xautoload can't do is to download Composer packages. This is what other modules are for.

Also, the composerDir() and composerJson() currently have one limitation: If you have an "composer/include_paths.php" / or "composer/autoload_files.php" with composerDir(), or $json['include-path'] or $json['autoload']['files'] in composer.json, then these will have no effect on requests where xautoload is cached.

See #2299265: Composer include paths and include files have no effect if xautoload is cached., and chime in if you have a thought!

Classmap generation with XAutoload

Both hook_xautoload() and hook_libraries_info() allow to add directories for classmap discovery.

Register additional locations with hook_xautoload()

hook_xautoload() allows to register additional locations and patterns for class loading, beyond those registered by default.

Support for sites/all/libraries (Libraries API)

Libraries API is a module that helps you with integrating 3rd party libraries.

XAutoload Class finder plugins (expert only)

Class finder plugins enable xautoload to support weird and exotic patterns that are in no way compatible with PSR-0. This can be useful for

Guide maintainers

donquixote's picture