xautoload is a comprehensive and yet highly efficient PHP class loading suite.

Most importantly, it provides Drupal-8-style PSR-4 (and the old D8 PSR-0-style in "lib") autoloading for Drupal 7. But it is also a great tool for 3rd party autoloading.

Since 7.x-5.4, it can also completely replace the Drupal core class loader, which allows to use the xautoload cache options for traditional core and contrib classes, and gets rid of the bogus distinction of drupal_autoload_class() vs drupal_autoload_interface().

Class loading for Drupal modules:

  • D8-style PSR-4 module namespaces (out of the box since 7.x-5.x) (under "$module_dir/src/..").
  • Old D8-style PSR-0 module namespaces (out of the box) (under "$module_dir/lib/..")
  • PHP 5.2 compatibility pattern ("PEAR-flat") with prefix/underscore class names instead of namespaces.
  • Shortcut syntax for files[] directive in *.info file, if you stick with the D7 registry class loading.

Class loading for 3rd party libraries:

  • hook_xautoload() to register additional namespaces with PSR-4, PSR-0 or whatever you like.
  • Libraries API: Namespace registration for libraries directly from hook_libraries_info().
    #1781794: Registration of additional PSR-0 folders in modules and sites/all/libraries
  • Can scan downloaded libraries to build a classmap. (which will be cached)
  • Can read and understand composer.json files from downloaded libraries.
  • Can process composer-generated autoload files in a vendor/composer directory, to register all this stuff to the xautoload class loader.
    (all of these can be registered from hook_xautoload() or hook_libraries_info())

Robustness:

  • Early-bootstrap mode: The class loader can be available as early as you wish, if you include it in your settings.php (or even your index.php). But even without that, it is registered damn early in the request..
  • Does not break even if you move the module to a different folder.
  • Works great even on freshly enabled modules (especially in the 7.x-5.x branch).

Performance:

  • Cache options: APCu self-updating classmap, APC / APCu key / value class lookup, and similar options based on WinCache, XCache, or even the database.
  • Can optionally replace the Drupal core classloader, so that Drupal classes are cached in the same APCu cache.
  • Module namespaces registration on bootstrap can be skipped if all classes are in cache, saving yet another few milliseconds.
  • Lookup algorithm designed to scale up to a large number of namespaces, even if not cached.
  • Benchmarks: #1912694-1: Run benchmarks (somewhat outdated)

Why?

The code of xautoload looks quite big, so isn't this overkill?
- A lot of the code is for unit tests.
- A lot of the code is for special case stuff, and won't be loaded if not needed.
- A lot of the code is never included if the class loader cache is hot.
- Loosely coupled single purpose-components make for small but many classes. That's the price.

Why would you need a class loader that is that flexible and powerful? Is it not enough to support basic PSR-0, and let strange libraries ship their own class loaders?

Yes, but..
- the smaller the autoload stack, the better.
- those strange libraries can now all benefit from the APC cache.
- no additional autoload.php or similar is processed, if the cache is hot.
-> profit.

Modules that use xautoload

Some of these modules have a compatibility fallback, to avoid having xautoload as an explicit requirement. Still, they should work better with xautoload enabled.

PSR-4 as in Drupal 8:

PSR-0 as in older Drupal 8 alpha:

PHP 5.2 compatibility style:

How to use

  1. Download / install / enable as usual.
  2. Optionally, enable APC and other cache options on admin/config/development/performance. The more checkboxes, the better!
  3. Add as a dependency in your modules that need it.
    dependencies[] = xautoload (>= 7.x-5.0)
  4. Now create your PSR-4 class files, e.g.
    File: "$module_dir/src/Foo/Bar.php"
    namespace Drupal\modulename\Foo;
    class Bar {..}
    
  5. More documentation

Wanted: Co-Maintainers for "soft tasks"

I am happy to do all the heavy coding and thinking on this module. Or, I really enjoy it!
But it would be great to have a second person on the boat to help with documentation, testing, brainstorming, support requests, and promoting the module to a wider audience.

Project Information

Downloads