diff --git a/.upgradenotes/MDL-81919-1715904408998.yml b/.upgradenotes/MDL-81919-1715904408998.yml new file mode 100644 index 00000000000..ff99de4dadc --- /dev/null +++ b/.upgradenotes/MDL-81919-1715904408998.yml @@ -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 diff --git a/lib/classes/component.php b/lib/classes/component.php index 258d7eb25a2..ae12e1a52ae 100644 --- a/lib/classes/component.php +++ b/lib/classes/component.php @@ -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). *