Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it easier to use OIK_autoload #132

Open
bobbingwide opened this issue Nov 17, 2019 · 3 comments
Open

Make it easier to use OIK_autoload #132

bobbingwide opened this issue Nov 17, 2019 · 3 comments

Comments

@bobbingwide
Copy link
Owner

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

  • Should oik be initiating the autoloading process?
  • What's the easiest way of listing classes - should it be a .json file held in each plugin?
  • Can we remove the namespace from the class name to create a file name?
  • Or should we use the namespaces following PSR something?
@bobbingwide
Copy link
Owner Author

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 bobbingwide/wp-top12#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:

  • Once shared library autoloading is set, it cannot be unset.
  • I don't expect much overlap between using the original method and the new method.
  • If the class cannot be loaded from the shared library directories, then the oik_query_autoload_classes method is used.

@bobbingwide
Copy link
Owner Author

bobbingwide commented Jan 25, 2021

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
Copy link
Owner Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant