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
- D7 Registry does not support namespaces: http://drupal.stackexchange.com/questions/84348/files-lib-php-fails-to-a...
- Core issue for Drupal 7: https://www.drupal.org/node/1399974
- Namespaces in Drupal 8: https://www.drupal.org/node/1353118
Project information
- Minimally maintained
Maintainers monitor issues, but fast responses are not guaranteed. - Maintenance fixes only
Considered feature-complete by its maintainers. - Module categories: Developer Tools
- 3,217 sites report using this module
- Created by Fabianx on , updated
- Stable releases for this project are covered by the security advisory policy.
Look for the shield icon below.