Problem/Motivation
The drupal_classloader() function was introduced into core but was added towards the end of the first bootstrap phase (bootstrap configuration). There is actually quite a bit of code that executes before the class autoloader is available that could very well make use of autoloading capability. (e.g. initializing the request object or other code that could be used in the earliest parts of the bootstrap process).
At first it seemed it might be enough to call drupal_classloader at the beginning bootstrap configuration, but in some cases (e.g. during install) autoloading capabilities may be required even BEFORE configuration is bootstrapped.
Proposed resolution
Add a new bootstrap phase dedicated to loading the autoloader and registering namespaces of classes required in the early bootstrap. This phase would run before anything else.
Remaining tasks
None.
User interface changes
None.
API changes
No API's touched or created.
Original report by me (andremolnar)
- Original questioning of 'late' inclusion of autoloader: #1290658-98: Move all module-provided classes to PHP namespaces (PSR-0 or similar), and autoload them without the registry [policy, no patch]
- Initial patch moving autoloader to the beginning of bootstrap configuration #1290658-100: Move all module-provided classes to PHP namespaces (PSR-0 or similar), and autoload them without the registry [policy, no patch]
- Follow up patches introducing and explaining new bootstrap phase #1290658-107: Move all module-provided classes to PHP namespaces (PSR-0 or similar), and autoload them without the registry [policy, no patch] + #1290658-109: Move all module-provided classes to PHP namespaces (PSR-0 or similar), and autoload them without the registry [policy, no patch]
| Comment | File | Size | Author |
|---|---|---|---|
| #20 | drupal_classloader-1451524-19.patch | 5.96 KB | andremolnar |
| #19 | drupal_classloader-1451524-19.patch | 4.4 KB | andremolnar |
| #10 | drupal_classloader-1451524-10.patch | 4.4 KB | andremolnar |
| #8 | 1451524.patch | 4.39 KB | robloach |
| #6 | drupal_classloader-1451524-6.patch | 4.22 KB | andremolnar |
Comments
Comment #2
andremolnar commentedThis patch is a follow up to the code review by @Rob Loach in #1290658-110: Move all module-provided classes to PHP namespaces (PSR-0 or similar), and autoload them without the registry [policy, no patch]
(edit - accidentally re-uploaded last patch from the old issue under a new name in comment #1 - deleted comment - this now the correct patch)
Comment #3
andremolnar commentedComment #4
robloachHaving the autoloader earlier in the bootstrap means we can move some of the bootstrap to PSR-0 classes in follow up patches. Patch applies cleanly, code looks good, tests pass.
Comment #5
lars toomre commentedThe docblock for this new function needs some work.
The first line should start with an active verb and it should be no more than 80 characters.
You might want to add a second explanation line (after an intervening space) that expands on what this phase of bootstrap does.
Comment #6
andremolnar commentedUpdated based on comments in #5
Comment #7
sunThe new phase name "autoloadER" doesn't seem to be in line with the other phase names.
What about DRUPAL_BOOTSTRAP_AUTOLOAD ?
This additional description doesn't provide any further information - except for explicitly mentioning Symfony and Drupal namespaces, but that's rather a code/implementation detail.
I'd recommend to drop the description and just keep the summary:
Initializes the class autoloader and registers namespaces.
The blank line here would be nice to keep for readability.
Comment #8
robloachUpdated with comments in #7. Replaced the description to point over to drupal_classloader() and uses "autoload" for the const and bootstrap function name.
Comment #9
lars toomre commentedThis patch now looks good to me. I will leave to others for RTBC.
Comment #10
andremolnar commentedActually 'autoloader' is the Noun. We are boostrapping the autoloader - not the 'autoload'.
Otherwise all other code comments and whitespace changes look good.
Comment #11
robloachAgreed. The "autoloader" is what we're bootstrapping, just like how we're bootstrapping the "configuration".
Comment #12
catchFixing component.
Comment #13
catchTwo things:
- Why do these belong in _drupal_bootstrap_configuration() more than _drupal_bootstrap_autoload(), I'm assuming they were put here simply because it was the first step, now it's not. Seems like a good chance to tidy up.
- more importantly. drupal_classloader() calls variable_get(), variable_get() depends on $conf, which can be altered by settings.php, so the patch is broken as it currently stands.
Comment #14
Crell commentedI suggest we just rip out the APC option for the autoloader for the moment. We know that other changes are coming in this area. We can revisit that later when we're done breaking stuff.
As for the error handlers, I disagree. Those don't belong in autoload. If anything they'd be part of an "environment init" bootstrap phase, but really, we need to stop thinking in terms of "environment creation". (Arguably those functions could be moved to a class, then autoload that! :-) ). We don't need those for the class loader, I think, so let's punt on that for now as well.
Comment #15
sunThat assumes there cannot be any errors in the autoloader code, which sounds bogus.
Comment #16
Crell commentedNo, it assumes that any errors there can be handled by the default PHP error handling, because they're sufficiently rare.
Comment #17
andremolnar commentedRe: #13
These lines in the patch are simply artifacts of the way the diff came out. That code is already committed in _drupal_bootstrap_configuration - can the virtue of those functions and their dependency on $conf be moved to its own issue?
On a more looking-towards-the-future level error.inc could be re-written to be a class that is autoloaded. We'll need an autoloader ready before we can do that.
The purpose of this patch is to get autoloading available ASAP in bootstrap since it already poses a development obstacle that is sure to come up more in the near future. Can the patch be considered on that basis alone?
Comment #18
Crell commentedandre: Those functions do not depend on $conf. The variable_get() call in the class loader setup depends on $conf. That's what I suggest we just remove and come back to later.
Comment #19
andremolnar commentedOf course. My mistake.
Here's an updated patch to remove the offending APC code committed in #1290658-97: Move all module-provided classes to PHP namespaces (PSR-0 or similar), and autoload them without the registry [policy, no patch]. I left the code comment as a @TODO (maybe to be followed up in the other issue?!)
Comment #20
andremolnar commentedGah, must remember to commit before git diff 8.x..foobranch > patch
Corrected patch attached.
Comment #22
robloachMight be easier to get #1424924: Use Composer for updating Symfony components (without removing Symfony code from repo) in first, because then we could simply just call
drupal_classloader()during the bootstrap. Saves us a function definition.Comment #22.0
robloachUpdating links to use markdown for readability
Comment #23
sunUnless I'm terribly mistaken, this issue is obsolete by now?
We're using the Composer ClassLoader in the meantime, which is manually included by all front-controllers before drupal_bootstrap() is called.
Comment #24
sylvain lecoy commentedUnless the composer classloader does not loads modules custom classes, this issue is still not fixed, otherwise yes..!