mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 05:58:34 +01:00
MDL-81919 core: Add support for autoloading legacy classes
This commit is contained in:
parent
d3ae1391ab
commit
302b94c2b7
8
.upgradenotes/MDL-81919-1715904408998.yml
Normal file
8
.upgradenotes/MDL-81919-1715904408998.yml
Normal 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
|
@ -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).
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user