Simple module to add PSR-0 and PSR-4 support to the Drupal 7 Core registry.

Motivation

The registry autoload module fills a gap between Drupal 7 and Drupal 8.

Drupal 8 has been using namespaced classes, but relies on the composer autoloader. Drupal 7 has the code registry for autoloading, but does not support namespaced files.

While the core registry would be enough to autoload namespaced files in Drupal 7, Drupal 7 does not support the namespace PHP tag as it did not exist when Drupal 7 was created.

The Core registry also lacks any automation in finding new class files and all need to be specified manually via the files[] array.

PSR-0 / PSR-4 automatic file scanning

To your module.info file add:

registry_autoload[] = PSR-0
registry_autoload[] = PSR-4

PSR-0 will search the lib/ subdirectory of the module for .php files, while PSR-4 will search the src/ subdirectory.

Please see README.md for more information.

Note: This will do a file_scan_directory() on every registry_rebuild, which might be something you don't want.

Manual adding of files that support namespaces

Similar to how files[] works, supply filenames relative to your modules directory with:

registry_autoload_files[] = filename

The difference is that registry_autoload_files[] support namespaces, while files[] does not, yet.

Implementation

The registry_autoload module uses a trick to hook into the registry building process:

By ensuring the hash is the same in {registry_file} compared to the actual file and updating {registry} ourselves we can ensure that core does not re-parse the files we add to the files[] array via hook_registry_files_alter() and will continue to use the namespaced classes.

Similar modules

- xautoload - Provides a full PSR-0, PSR-4 and custom namespaces compatible autoloader, which finds files at run time. The main difference is that xautoload is a fully fledged autoloader with _lots_ of features while this only enhances the Drupal 7 core registry to scan files automatically (on demand) and provide namespace support.

The main use case was that xautoload was too heavy to depend on for a project, which wants to use PSR-0 and PSR-4 features.

References

Project Information

Downloads