Moodle 3.3 filesystem "plugin" loading system

Moodle 3.3 filesystem "plugin" loading system

by Mattia Belletti -
Number of replies: 2

  Hello,

  I'm working on an Azure filesystem "plugin", as detailed in File System API.

  I've been able to make it work, but the loading mechanism leaves me a bit perplexed.

  If I'm not wrong, the basic idea is that the file system API is very low level, and as such not loadable as a plugin. The mechanism is that an alternative class is loaded by specifying it in the config.php like that:

$CFG->alternative_file_system_class = 'azure_storage_file_system';

The class must be available to the file_storage class when it's time to load, though. Given the file_storage::setup_file_system code:

public function setup_file_system() {
    global $CFG;
    if ($this->filesystem === null) {
        require_once($CFG->libdir . '/filestorage/file_system.php');
        if (!empty($CFG->alternative_file_system_class)) {
            $class = $CFG->alternative_file_system_class;
        } else {
            // The default file_system is the filedir.
            require_once($CFG->libdir . '/filestorage/file_system_filedir.php');
            $class = file_system_filedir::class;
        }
        $this->filesystem = new $class();
    }

    return $this->filesystem;
}

it's clear that the loading of the default file_system class is handled internally, through a require_once. For my alternative class, though, my PHP file must be already loaded. The only place I can do that is directly in the config.php, like this:

require_once(__DIR__ . '/azure_storage/azure_storage_file_system.php');
$CFG->alternative_file_system_class = 'azure_storage_file_system';

This has a problem though: my code needs to extend the file_system class, and because of that needs to:

require_once(__DIR__ . '/../lib/filestorage/file_system.php');

And at the beginning of that file we find:

defined('MOODLE_INTERNAL') || die();

And apparently this is too soon for MOODLE_INTERNAL to be defined: it clearly breaks during the installation routines. The bad workaround I found now is to modify the config.php code like this:

$oldmoodleinternal = defined('MOODLE_INTERNAL');
define('MOODLE_INTERNAL', true);
require_once(__DIR__ . '/azure_storage/azure_storage_file_system.php');
if($oldmoodleinternal) {
    runkit_constant_remove('MOODLE_INTERNAL');
}
$CFG->alternative_file_system_class = 'azure_storage_file_system';

But I would very much like to remove this hack smile.

Am I getting something wrong? Is there a better way to handle this? Is there some design problem?

Average of ratings: -
In reply to Mattia Belletti

Re: Moodle 3.3 filesystem "plugin" loading system

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Use https://docs.moodle.org/dev/Automatic_class_loading

Call the class something like local_azurefilestorage_file_system, and define it in local/azurefilestorage/classes/file_system.php.

Average of ratings: Useful (2)