MDL-81919 core: Add support for autoloading legacy classes

This commit is contained in:
Andrew Nicols 2024-05-16 14:18:09 +08:00
parent d3ae1391ab
commit 302b94c2b7
No known key found for this signature in database
GPG Key ID: 6D1E3157C8CFBF14
2 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,8 @@
issueNumber: MDL-81919
notes:
core:
- message: >
Added a mechanism to support autoloading of legacy class files.
This will help to reduce the number of require_once calls in the codebase, and move away from the use of monolithic libraries.
type: improved

View File

@ -690,17 +690,20 @@ $cache = ' . var_export($cache, true) . ';
self::$classmap = [];
self::load_classes('core', "$CFG->dirroot/lib/classes");
self::load_legacy_classes($CFG->libdir);
foreach (self::$subsystems as $subsystem => $fulldir) {
if (!$fulldir) {
continue;
}
self::load_classes('core_' . $subsystem, "$fulldir/classes");
self::load_legacy_classes($fulldir);
}
foreach (self::$plugins as $plugintype => $plugins) {
foreach ($plugins as $pluginname => $fulldir) {
self::load_classes($plugintype . '_' . $pluginname, "$fulldir/classes");
self::load_legacy_classes($fulldir);
}
}
ksort(self::$classmap);
@ -1401,6 +1404,34 @@ $cache = ' . var_export($cache, true) . ';
}
}
/**
* Load legacy classes based upon the db/legacyclasses.php file.
*
* The legacyclasses.php should contain a key => value array ($legacyclasses) where the key is the class name,
* and the value is the path to the class file within the relative ../classes/ directory.
*
* @param string|null $fulldir The directory to the legacy classes.
*/
protected static function load_legacy_classes(?string $fulldir): void {
if (is_null($fulldir)) {
return;
}
$file = $fulldir . '/db/legacyclasses.php';
if (is_readable($file)) {
$legacyclasses = null;
require($file);
if (is_array($legacyclasses)) {
foreach ($legacyclasses as $classname => $path) {
$fullpath = "{$fulldir}/classes/{$path}";
if (file_exists($fullpath)) {
self::$classmap[$classname] = $fullpath;
}
}
}
}
}
/**
* Returns a list of frankenstyle component names and their paths, for all components (plugins and subsystems).
*