Created
June 23, 2014 03:43
-
-
Save rmccue/e03d99d04e1db8ab06a6 to your computer and use it in GitHub Desktop.
Human Made Autoloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: HM Autoloader | |
* Description: Provides a standard autoloader utility for the site to use | |
* Author: Human Made Limited | |
* Author URI: http://hmn.md/ | |
*/ | |
namespace HM; | |
class Autoloader { | |
const NS_SEPARATOR = '\\'; | |
protected $prefix; | |
protected $prefix_length; | |
protected $path; | |
public function __construct( $prefix, $path ) { | |
$this->prefix = $prefix; | |
$this->prefix_length = strlen( $prefix ); | |
$this->path = trailingslashit( $path ); | |
} | |
public function load( $class ) { | |
if ( strpos( $class, $this->prefix . self::NS_SEPARATOR ) !== 0 ) { | |
return; | |
} | |
// Strip prefix from the start (ala PSR-4) | |
$class = substr( $class, $this->prefix_length + 1 ); | |
$class = strtolower( $class ); | |
$file = ''; | |
if ( false !== ( $last_ns_pos = strripos( $class, self::NS_SEPARATOR ) ) ) { | |
$namespace = substr( $class, 0, $last_ns_pos ); | |
$class = substr( $class, $last_ns_pos + 1 ); | |
$file = str_replace( self::NS_SEPARATOR, DIRECTORY_SEPARATOR, $namespace ) . DIRECTORY_SEPARATOR; | |
} | |
$file .= 'class-' . str_replace( '_', '-', $class ) . '.php'; | |
$path = $this->path . $file; | |
if ( file_exists( $path ) ) { | |
require_once $path; | |
} | |
} | |
} | |
function register_class_path( $prefix, $path ) { | |
$loader = new Autoloader( $prefix, $path ); | |
spl_autoload_register( [ $loader, 'load' ] ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment