Skip to content

Instantly share code, notes, and snippets.

@asaokamei
Created January 16, 2012 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asaokamei/1620960 to your computer and use it in GitHub Desktop.
Save asaokamei/1620960 to your computer and use it in GitHub Desktop.
ClassLoader for PSR-0 using closure.
<?php
/**
* SPL Class Loader for NameSpace classes using closure.
* ex: spl_autoload_register( ClassLoader() );
* copied from https://gist.github.com/221634
* @param $path
* @param array $option
* @return closure
*/
spl_autoload_register( My_ClassLoader() );
function My_ClassLoader( $path=FALSE )
{
if( $path === FALSE ) $path = __DIR__;
$args = func_get_args();
$args = array_slice( $args, 1 );
$default = array(
'ext' => '.php',
'sep' => '\\',
'tip' => FALSE,
);
$args = $args + $default;
if( !$args[ 'tip' ] ) {
$args[ 'tip' ] = substr( $path, strripos( $path, DIRECTORY_SEPARATOR ) + 1 );
$path = substr( $path, 0, strripos( $path, DIRECTORY_SEPARATOR ) );
}
$args[ 'path' ] = $path;
return function( $className ) use ( $args ) {
$tip = $args[ 'tip' ];
if( !$tip ) return;
$ds = DIRECTORY_SEPARATOR;
$sep = $args[ 'sep' ];
$ext = $args[ 'ext' ];
$path = $args[ 'path' ];
$className = ltrim( $className, '\\' );
if( substr( $className, 0, strlen( $tip.$sep ) ) === $tip.$sep ) {
$fileName = '';
if( FALSE !== ( $lastNsPos = strripos( $className, $sep ) ) ) {
$namespace = substr( $className, 0, $lastNsPos );
$className = substr( $className, $lastNsPos + 1 );
$fileName = str_replace( $sep, $ds, $namespace ) . $ds;
}
$fileName .= str_replace( '_', $ds, $className) . $ext;
$inc = ( $path !== null ? $path . $ds : '' ) . $fileName;
require( $inc );
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment