Created
January 16, 2012 13:50
-
-
Save asaokamei/1620960 to your computer and use it in GitHub Desktop.
ClassLoader for PSR-0 using closure.
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 | |
/** | |
* 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