From 2243d1b3958882279102abb87539b603e0241e2f Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 8 Aug 2024 12:57:34 +0800 Subject: [PATCH] MDL-82747 core: Provide a standard way to register the Autoloader --- admin/cli/install.php | 10 +++------- install.php | 11 +++-------- lib/classes/component.php | 11 +++++++++++ lib/setup.php | 8 ++------ lib/upgrade.txt | 7 +++++++ 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/admin/cli/install.php b/admin/cli/install.php index 80bcdd9d906..09768816040 100644 --- a/admin/cli/install.php +++ b/admin/cli/install.php @@ -177,6 +177,7 @@ $CFG->admin = array_pop($parts); ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path')); require_once($CFG->libdir.'/classes/component.php'); + require_once($CFG->libdir.'/classes/text.php'); require_once($CFG->libdir.'/classes/string_manager.php'); require_once($CFG->libdir.'/classes/string_manager_install.php'); @@ -192,13 +193,8 @@ require_once($CFG->libdir.'/adminlib.php'); require_once($CFG->libdir.'/componentlib.class.php'); require_once($CFG->dirroot.'/cache/lib.php'); -// Register our classloader, in theory somebody might want to replace it to load other hacked core classes. -// Required because the database checks below lead to session interaction which is going to lead us to requiring autoloaded classes. -if (defined('COMPONENT_CLASSLOADER')) { - spl_autoload_register(COMPONENT_CLASSLOADER); -} else { - spl_autoload_register('core_component::classloader'); -} +// Register our classloader. +\core_component::register_autoloader(); require($CFG->dirroot.'/version.php'); $CFG->target_release = $release; diff --git a/install.php b/install.php index 70c9e155b72..cfb1112889c 100644 --- a/install.php +++ b/install.php @@ -218,13 +218,9 @@ require_once($CFG->dirroot.'/cache/lib.php'); //the problem is that we need specific version of quickforms and hacked excel files :-( ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path')); -// Register our classloader, in theory somebody might want to replace it to load other hacked core classes. -// Required because the database checks below lead to session interaction which is going to lead us to requiring autoloaded classes. -if (defined('COMPONENT_CLASSLOADER')) { - spl_autoload_register(COMPONENT_CLASSLOADER); -} else { - spl_autoload_register('core_component::classloader'); -} +// Register our classloader. +\core_component::register_autoloader(); + require('version.php'); $CFG->target_release = $release; @@ -653,4 +649,3 @@ echo ''; install_print_footer($config); die; - diff --git a/lib/classes/component.php b/lib/classes/component.php index a9c3aef5973..0da8f0cd523 100644 --- a/lib/classes/component.php +++ b/lib/classes/component.php @@ -129,6 +129,17 @@ class core_component { 'Invoker' => 'lib/php-di/invoker/src', ]; + /** + * Register the Moodle class autoloader. + */ + public static function register_autoloader(): void { + if (defined('COMPONENT_CLASSLOADER')) { + spl_autoload_register(COMPONENT_CLASSLOADER); + } else { + spl_autoload_register([self::class, 'classloader']); + } + } + /** * Class loader for Frankenstyle named classes in standard locations. * Frankenstyle namespaces are supported. diff --git a/lib/setup.php b/lib/setup.php index 71f1e7c0328..dd163234f59 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -620,12 +620,8 @@ if (!empty($_SERVER['HTTP_X_moz']) && $_SERVER['HTTP_X_moz'] === 'prefetch'){ //the problem is that we need specific version of quickforms and hacked excel files :-( ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path')); -// Register our classloader, in theory somebody might want to replace it to load other hacked core classes. -if (defined('COMPONENT_CLASSLOADER')) { - spl_autoload_register(COMPONENT_CLASSLOADER); -} else { - spl_autoload_register('core_component::classloader'); -} +// Register our classloader. +\core_component::register_autoloader(); // Remember the default PHP timezone, we will need it later. core_date::store_default_php_timezone(); diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 4ad47cbd7dd..b8fff691a75 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -1,6 +1,13 @@ This files describes API changes in core libraries and APIs, information provided here is intended especially for developers. +=== 4.4.3 === + +* The Moodle autoloader should now be registered using `\core\component::register_autoloader` rather + than manually doing so in any exceptional location which requires it. It is not normally necessary + to include the autoloader manually, as it is registered automatically when the Moodle environment + is bootstrapped. + === 4.4.2 === * The `\core\dataformat::get_format_instance` method is now public, and can be used to retrieve a writer instance for