bobbingwide / oik

OIK Information Kit
https://www.oik-plugins.com/oik-plugins/oik
GNU General Public License v2.0
2 stars 0 forks source link

Make it easier to use OIK_autoload #132

Open bobbingwide opened 4 years ago

bobbingwide commented 4 years ago

Consider the options for making it easier to use OIK_Autoload functionality. The current implementation requires any plugin that wants its classes to be autoloaded to opt-in to OIK_Autoload. Participating plugins must use code like this

function oiksc_autoload() {
    $lib_autoload = oik_require_lib( "oik-autoload" );
    if ( $lib_autoload && !is_wp_error( $lib_autoload ) ) {
        oik_autoload();
    }   
}

When oik_autoload() is invoked it loads the OIK_Autoload class which determines which classes can be autoloaded by invoking the oik_query_autoload_classes filter function. Plugins respond to this by providing the class names to autoload, and where to find them. OIK_Autoload then registers its autoloading logic to spl_autoload_register.

The OIK_Autoload::autoload method locates the requested $class and finds the file to load for the class.

Each plugin has to provide a list of classes that can be autoloaded. There's no composer like logic to provide the list.

In my newer code I've been using namespaces. In order for these classes to be loaded I have to provide the plugin, class and file as the logic doesn't cater for the namespace.

Considerations / questions

bobbingwide commented 3 years ago

In my newer code I've been using namespaces. In order for these classes to be loaded I have to provide the plugin, class and file as the logic doesn't cater for the namespace.

I should have said where I'm doing this. In my most recent code, for slog and wp-top12, I'm not using namespaces. For https://github.com/bobbingwide/wp-top12/issues/13 I started changing the code by putting the class files in the libs folder. I was then going to use oik_require_lib to load the files. Before doing so I tried changing the logic in OIK_autoload adding a method to load shared library classes from the libs folder.

function load_shared_library_class_file( $class ) {
        $lib = 'class-';
        $file = str_replace( "_", "-", $class );
        $file = strtolower( $file );
        $lib .= $file;
        $library_file = oik_require_lib( $lib );
        return $library_file;
    }

Once the library name has been constructed from the class name it uses oik_require_lib() to load the required file. There's no version checking, but I don't consider this to be a problem.

I also added code to turn on this new logic, with a set_autoload_shared_library method that's used by any routine that wants to opt in to this processing. It's called in the oik_autoload function. Plugins still need to request autoloading, by loading the library and calling oik_autoload(). The default value is false, so plugins wanting to opt into autoloading shared library file classes will pass true.

/**
 * Enables autoload processing using shared library classes.
 *
 */
function slog_enable_autoload() {
    $lib_autoload = oik_require_lib( 'oik-autoload');
    if ( $lib_autoload && !is_wp_error( $lib_autoload ) ) {
        oik_autoload( true );
    } else {
        BW_::p( "oik-autoload library not loaded");
    }
}

Notes:

bobbingwide commented 3 years ago

If the class cannot be loaded from the shared library directories, then the oik_query_autoload_classes method is used.

This doesn't appear to work. When slog is activated and I try to run update plugin version then I get

Fatal error: Uncaught Error: Class 'OIK_component_update' not found in 
C:\apache\htdocs\wordpress\wp-content\plugins\oik-update\oik-update.php:83
bobbingwide commented 3 years ago

This doesn't appear to work.

I had to update the shared library loading to allow a plugin to request the oik_query_autoload_classes filter to be run again. This is needed by the oik-update plugin.