1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179:
<?php
/**
* ImpressCMS Autoloader
*
* @copyright http://www.impresscms.org/ The ImpressCMS Project
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License (GPL)
* @category ICMS
* @since 1.3
* @author Marc-André Lanciault (aka marcan) <mal@inboxintl.com>
* @version $Id: Autoloader.php 11448 2011-11-21 16:37:13Z fiammy $
*/
class icms_Autoloader {
/**
* Paths of known global repositories
* @var array
*/
static protected $globalRepositories = array();
/**
* Paths of known namespace repositories
* @var array
* @internal Each entry is stored as array(strlen($ns), $path), to avoid having to call strlen repeatedly during autoload
*/
static protected $localRepositories = array();
/**
* Imported namespaces
* @var array
*/
static protected $imported = array();
/**
* Whether setup has already been called or not
* @var bool
*/
static protected $initialized = FALSE;
/**
* Initialize the autoloader, and register its autoload method
* @return void
*/
static public function setup() {
if (!self::$initialized) {
self::register(dirname(dirname(__FILE__)));
spl_autoload_register(array('icms_Autoloader', 'autoload'));
spl_autoload_register(array('icms_Autoloader', 'registerLegacy'));
self::$initialized = TRUE;
}
}
/**
* Split a fully qualified class name into Namespace and Local class name
*
* Supports both PHP 5.3 proj\sub\Class and 5.2 proj_sub_Class naming convention.
*
* @param string $class
* @return array
*/
static public function split($class) {
if (FALSE === ($pos = strrpos($class, "\\"))) {
$pos = strrpos($class, "_");
}
if ($pos) {
$ns = substr($class, 0, $pos);
$local = substr($class, $pos + 1);
} else {
$ns = "";
$local = $class;
}
return array($ns, $local);
}
/**
* Register/unregister a class repository
*
* The autoload system will look for classes in all registered repositories one after the other.
*
* @param string $path Repository physical path
* @param string $namespace If specified, all classes of the repository belong to this namespace
* @return void
*/
static public function register($path, $namespace = "") {
if ($namespace) {
self::$localRepositories[ $namespace ] = array(strlen($namespace), $path);
} else {
self::$globalRepositories[] = $path;
}
}
/**
* Import a namespace global elements (constants and functions)
*
* If a namespace has functions or constants, they must be put in a file called "namespace.php"
* that will be read by this function.
*
* @param string $namespace
* @param bool $required Whether to throw an exception or not if the namespace file is not found
* @return bool
*/
static public function import($namespace, $required = TRUE) {
if (!isset(self::$imported[ $namespace ])) {
$nspath = self::classPath($namespace, TRUE, DIRECTORY_SEPARATOR . "namespace.php");
if ($nspath) {
include_once($nspath . DIRECTORY_SEPARATOR . "namespace.php");
return self::$imported[$namespace] = TRUE;
}
self::$imported[$namespace] = FALSE;
}
if (!self::$imported[$namespace] && $required) {
throw new RuntimeException("No namespace file for namespace '$namespace'.");
}
return self::$imported[$namespace];
}
/**
* Locate and load the appropriate file, based on a class name
*
* @param string $class
* @return bool
*/
static public function autoload($class) {
if ($path = self::classPath($class)) {
list($ns, $local) = self::split($class);
include_once "$path.php";
return TRUE;
}
return FALSE;
}
/**
* Attempt to find a class path by scanning registered repositories
*
* @param string $class Name of the class to find
* @param bool $useIncludePath If to search include paths too
*/
static public function classPath($class, $useIncludePath = FALSE, $ext = ".php") {
$classPath = str_replace(array("\\", "_"), DIRECTORY_SEPARATOR, $class);
// First, try local repositories
if (strpos($class, "\\") || strpos($class, "_")) {
foreach (self::$localRepositories as $name => $info) {
list($len, $path) = $info;
if (!strncmp($name . "\\", $class, $len+1) || !strncmp($name . "_", $class, $len+1)) {
$localPath = substr($classPath, $len + 1);
if (file_exists($path . DIRECTORY_SEPARATOR . $localPath . $ext)) {
return $path . DIRECTORY_SEPARATOR . $localPath;
}
}
}
}
// Search global repositories
foreach(self::$globalRepositories as $path) {
if (file_exists($path . DIRECTORY_SEPARATOR . $classPath . $ext)) {
return $path . DIRECTORY_SEPARATOR . $classPath;
}
}
// Search include path
// On Windows include paths use "/" as directory_separator, even if added to set_include_path with anti-slashes
// We do this to make sure the string we get compensates for that
if ($useIncludePath) {
foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) {
if (file_exists($path . DIRECTORY_SEPARATOR . $classPath . $ext)) {
return (DIRECTORY_SEPARATOR != "/" ? str_replace("/", DIRECTORY_SEPARATOR, $path) : $path) . DIRECTORY_SEPARATOR . $classPath;
}
}
}
return FALSE;
}
/**
* This function maps the legacy classes that were included in common.php and xoopsformloader.php
* Rather than including all the legacy files, this defines where PHP should look to use these classes
* It's ugly, but so was the code we're getting rid of
*/
static public function registerLegacy($class) {
$class = strtolower($class);
$legacyClassPath = array();
if (in_array($class, array_keys($legacyClassPath))) {
include_once ICMS_ROOT_PATH . $legacyClassPath[$class];
}
}
}