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 application meta data
Project Page: https://www.drupal.org/sandbox/fabianx/2354855
Pareview: http://pareview.sh/pareview/httpgitdrupalorgsandboxfabianx2354855git
Git Clone: git clone --branch 7.x-1.x http://git.drupal.org/sandbox/Fabianx/2354855.git registry_autoload
Reviews of other projects:
@todo
Comments
Comment #1
fabianx commentedComment #2
PA robot commentedWe are currently quite busy with all the project applications and we prefer projects with a review bonus. Please help reviewing and put yourself on the high priority list, then we will take a look at your project right away :-)
Also, you should get your friends, colleagues or other community members involved to review this application. Let them go through the review checklist and post a comment that sets this issue to "needs work" (they found some problems with the project) or "reviewed & tested by the community" (they found no major flaws).
I'm a robot and this is an automated message from Project Applications Scraper.
Comment #3
fabianx commentedComment #4
fabianx commentedI am putting this to "Needs review" for now. I am unfortunately too busy this week to get the review bonus :-/.
But will try to follow-up on other applications as soon as possible.
Comment #5
fabianx commentedI am putting this to "Needs review" for now. I am unfortunately too busy this week to get the review bonus :-/.
But will try to follow-up on other applications as soon as possible.
Comment #6
fabianx commentedComment #7
klausimanual review:
there is tests directory, but there is no *.test file containing an automated test case that would make use of the test module's code files? Am I missing something?
But otherwise looks RTBC to me.
Assigning to chx as he might have time to take a final look at this.
Comment #8
fabianx commentedThanks for the review.
The test module was made for manual testing. This is unfortunately only stated in the header comment of the module file (http://cgit.drupalcode.org/sandbox-Fabianx-2354855/tree/tests/modules/re...). The test case is so simple, that a manual test was better than setting up simpletest extra for that.
Comment #9
chx commentedOf course it looks OK, I think core subsystem maintainers should be auto-approved, really. I do not have user editing powers but I will try to find someone who does :)
Comment #10
mpdonadioComment #11
klausiOh, chx is not a git admin anymore? That's sad, I removed you from the list of git admins so that we are all aware of it :-(
Comment #12
mpdonadioAutomated Review
Review of the 7.x-1.x branch (commit 6ed0d1f):
This automated report was generated with PAReview.sh, your friendly project application review script. You can also use the online version to check your project. You have to get a review bonus to get a review from me.
Manual Review
You have a tests directory with that appears to be a test module, but no .test file? Ah, this was mentioned above.
The starred items (*) are fairly big issues and warrant going back to Needs Work. Items marked with a plus sign (+) are important and should be addressed before a stable project release. The rest of the comments in the code walkthrough are recommendations.
Nothing major, nothing minor, either.
If added, please don't remove the security tag, we keep that for statistics and to show examples of security problems.
This review uses the Project Application Review Template.
Comment #13
mpdonadioThanks for your contribution, Fabianx!
I updated your account so you can promote this to a full project and also create new projects as either a sandbox or a "full" project.
Here are some recommended readings to help with excellent maintainership:
You can find lots more contributors chatting on IRC in #drupal-contribute. So, come hang out and stay involved!
Thanks, also, for your patience with the review process. Anyone is welcome to participate in the review process. Please consider reviewing other projects that are pending review. I encourage you to learn more about that process and join the group of reviewers.
Thanks to the dedicated reviewer(s) as well.
Comment #14
mpdonadioComment #15
chx commented> Oh, chx is not a git admin anymore?
#2172149-30: Disable the Github Sync user account: it's confusing I am not anything any more.
Comment #16
fabianx commentedThanks so much @all!
I added the .test file now that I had a short moment to grok it.
Created the module here and automated testing is enabled:
https://www.drupal.org/project/registry_autoload
Also created a first stable release.
Thanks again for all the feedback and the great review tools!