Skip to content

Commit

Permalink
Issue #132 - add support to autoload shared library classes
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed Jan 6, 2021
1 parent b13d2eb commit ffdf2ba
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
50 changes: 47 additions & 3 deletions libs/class-oik-autoload.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php // (C) Copyright Bobbing Wide 2015, 2016
<?php // (C) Copyright Bobbing Wide 2016-2021
if ( !defined( "CLASS_OIK_AUTOLOAD_INCLUDED" ) ) {
define( "CLASS_OIK_AUTOLOAD_INCLUDED", "0.1.0" );
define( "CLASS_OIK_AUTOLOAD_INCLUDED", "1.0.0" );

/**
* Implement autoloading for shared libraries
Expand Down Expand Up @@ -33,7 +33,11 @@ class OIK_Autoload {
* @var OIK_autoload - the true instance
*/
private static $instance;


/**
* @var bool True to autoload shared library classes. False initially.
*/
private $autoload_shared_library;
/**
* Return a single instance of this class
*
Expand All @@ -50,6 +54,7 @@ public static function instance() {
* Constructor for the OIK_autoload class
*/
function __construct() {
$this->autoload_shared_library = false;
self::$loads = array();
$loads_more = apply_filters( "oik_query_autoload_classes", self::$loads );
self::$loads = $loads_more;
Expand All @@ -68,12 +73,25 @@ function __construct() {
* What if we can't?
*/
function autoload( $class ) {
if ( $this->autoload_shared_library ) {
$library_file = $this->load_shared_library_class_file( $class );
if ( $library_file && !is_wp_error( $library_file)) {
return;
}
}

$class_file = $this->locate_class( $class );
if ( $class_file ) {
$file = $this->file( $class_file );
oik_require( $file, $class_file->plugin );
} else {
// Perhaps it's a shared library file
// or perhaps it's in classes
$this->load_shared_library_class_file( $class );
}
}



/**
* Determine the file name from the class and path
Expand Down Expand Up @@ -164,6 +182,32 @@ function nortoload( $loads_more ) {
return( $loads_more );
}

/**
* Enables / disables the autoload shared library logic.
*
* @param bool $autoload_shared_library
*/
function set_autoload_shared_library( $autoload_shared_library ) {
if ( $autoload_shared_library ) {
$this->autoload_shared_library=$autoload_shared_library;
}
}

/**
* Try loading a shared library class.
*
* @param $class
* @return object/bool the library loaded or a simple bool if oik_libs is not loaded, so we used the fallback
*/
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;
}


}

Expand Down
14 changes: 10 additions & 4 deletions libs/oik-autoload.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php // (C) Copyright Bobbing Wide 2015-2019
<?php
if ( !defined( "OIK_AUTOLOAD_INCLUDED" ) ) {
define( "OIK_AUTOLOAD_INCLUDED", "0.1.0" );
define( "OIK_AUTOLOAD_INCLUDED", "1.0.0" );

/**
* @copyright (C) Copyright Bobbing Wide 2015-2021
* @package oik-libs
*
* Autoload library functions
*
* Library: oik-autoload
Expand Down Expand Up @@ -36,14 +39,17 @@ function oik_require_class( $class, $args=null ) {
* But I think it's better to implicitely invoke either oik_require_class() or oik_autoload() to instantiate the
* autoloading logic when you know that OO code will be used.
*
* Notice we use oik_require_file() to load a class file manually
* Notice we use oik_require_file() to load a class file manually.
*
* @param bool $autoload_shared_library True if we want autoloading of shared library classes.
*/
function oik_autoload() {
function oik_autoload( $autoload_shared_library=false ) {
if ( !class_exists( "OIK_Autoload" ) ) {
oik_require_file( "class-oik-autoload.php", "oik-autoload" );
}
if ( class_exists( "OIK_Autoload" ) ) {
$oik_autoload = OIK_Autoload::instance();
$oik_autoload->set_autoload_shared_library( $autoload_shared_library );
} else {
bw_trace2( "Class OIK_Autoload does not exist", null, false, BW_TRACE_ERROR );
die();
Expand Down

0 comments on commit ffdf2ba

Please sign in to comment.