diff options
author | Adam Roses Wight <awight@wikimedia.org> | 2013-03-14 01:26:31 -0700 |
---|---|---|
committer | Adam Roses Wight <awight@wikimedia.org> | 2013-03-14 01:26:31 -0700 |
commit | c9958d61e71c0443d5135c26f735a4c44e01c449 (patch) | |
tree | ea4ecc0ab8ce3f2db41de89afe4f59cb73af5789 | |
parent | b5299c2663ad346788804d46bea43c66200e497c (diff) |
experimenting with namespaced class autoloadingorigin/sandbox/adamw/nsroot
-rw-r--r-- | includes/AutoLoader.php | 20 | ||||
-rw-r--r-- | includes/DefaultSettings.php | 2 |
2 files changed, 20 insertions, 2 deletions
diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 713623223c92..3868c38d817d 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -21,6 +21,11 @@ */ /** + * An array of top-level namespaces which map to a root dir containing classes named according to PSR-0 + */ +global $wgAutoloadNamespaceRootDirs; + +/** * 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 @@ -1115,7 +1120,7 @@ class AutoLoader { * as well. */ static function autoload( $className ) { - global $wgAutoloadClasses, $wgAutoloadLocalClasses; + global $wgAutoloadClasses, $wgAutoloadLocalClasses, $wgAutoloadNamespaceRootDirs; // Workaround for PHP bug <https://bugs.php.net/bug.php?id=49143> (5.3.2. is broken, it's fixed in 5.3.6). // Strip leading backslashes from class names. When namespaces are used, leading backslashes are used to indicate @@ -1125,7 +1130,18 @@ class AutoLoader { // backlash in this case, causing autoloading to fail. $className = ltrim( $className, '\\' ); - if ( isset( $wgAutoloadLocalClasses[$className] ) ) { + # Try the namespace registry. A more sophisticated algorithm would + # check each prefix; for now only top-level namespaces are supported. + $classTuple = explode( '\\', $className ); + $toplevelNamespace = array_shift( $classTuple ); + if ( $classTuple + and isset( $wgAutoloadNamespaceRootDirs[$toplevelNamespace] ) ) { + $filename = implode( DIRECTORY_SEPARATOR, array_merge( + array( $wgAutoloadNamespaceRootDirs[$toplevelNamespace] ), + $classTuple + ) ); + $filename .= '.php'; + } elseif ( isset( $wgAutoloadLocalClasses[$className] ) ) { $filename = $wgAutoloadLocalClasses[$className]; } elseif ( isset( $wgAutoloadClasses[$className] ) ) { $filename = $wgAutoloadClasses[$className]; diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 5d051d7cbef0..ce1e8e3502b9 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -5442,6 +5442,8 @@ $wgValidSkinNames = array(); */ $wgSpecialPages = array(); +$wgAutoloadNamespaceRootDirs = array(); + /** * Array mapping class names to filenames, for autoloading. */ |