Mediawiki的自动加载类机制--autoload 收藏 评论
2012年02月18日

这是MediaWiki的自动类加载机制。


/* This defines autoloading handler for whole MediaWiki framework */

ini_set(\'unserialize_callback_func\', \'__autoload\' );

# Locations of core classes
# Extension classes are specified with $wgAutoloadClasses
# This array is a global instead of a static member of AutoLoader to work around a bug in APC
global $wgAutoloadLocalClasses;
$wgAutoloadLocalClasses = array(
	# Includes
	\'AjaxDispatcher\' => \'includes/AjaxDispatcher.php\',
	\'AjaxResponse\' => \'includes/AjaxResponse.php\',
);

class AutoLoader {
	/**
	 * autoload - take a class name and attempt to load it
	 *
	 * @param string $className Name of class we\'re looking for.
	 * @return bool Returning false is important on failure as
	 * it allows Zend to try and look in other registered autoloaders
	 * as well.
	 */
	static function autoload( $className ) {
		global $wgAutoloadClasses, $wgAutoloadLocalClasses;

		if ( isset( $wgAutoloadLocalClasses[$className] ) ) {
			$filename = $wgAutoloadLocalClasses[$className];
		} elseif ( isset( $wgAutoloadClasses[$className] ) ) {
			$filename = $wgAutoloadClasses[$className];
		} else {
			# Try a different capitalisation
			# The case can sometimes be wrong when unserializing PHP 4 objects
			$filename = false;
			$lowerClass = strtolower( $className );
			foreach ( $wgAutoloadLocalClasses as $class2 => $file2 ) {
				if ( strtolower( $class2 ) == $lowerClass ) {
					$filename = $file2;
				}
			}
			if ( !$filename ) {
				if( function_exists( \'wfDebug\' ) ) 	
					wfDebug( "Class {$className} not found; skipped loading\\n" );
				# Give up
				return false;
			}
		}

		# Make an absolute path, this improves performance by avoiding some stat calls
		if ( substr( $filename, 0, 1 ) != \'/\' && substr( $filename, 1, 1 ) != \':\' ) {
			global $IP;
			$filename = "$IP/$filename";
		}
		require( $filename );
		return true;
	}

	static function loadAllExtensions() {
		global $wgAutoloadClasses;

		foreach( $wgAutoloadClasses as $class => $file ) {
			if( !( class_exists( $class, false ) || interface_exists( $class, false ) ) ) {
				require( $file );
			}
		}
	}
}

function wfLoadAllExtensions() {
	AutoLoader::loadAllExtensions();
}

if ( function_exists( \'spl_autoload_register\' ) ) {
	spl_autoload_register( array( \'AutoLoader\', \'autoload\' ) );
} else {
	function __autoload( $class ) {
		AutoLoader::autoload( $class );
	}
}

http://blog.webinno.cn/article/view/2

本文地址:http://blog.webinno.cn/article/view/2

发表于 @ 2012年02月18日 | 浏览2876次| 编辑 |评论(loading... ) | 分享到:QQ空间新浪微博腾讯微博微信

评论列表

发表评论