X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=includes%2FAutoLoader.php;h=52410fede871b1c5d78b3c36e79304d732fe95f1;hp=883b8a32c1e9c4d8334ee4b53d2f21e23564e5f6;hb=08edb27f6ce31c676660a4ef89b87da79bde2cc2;hpb=8e5ec3f6cf9cb5f3ecf5ff30430ff37b5a7857a7 diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 883b8a32c1..52410fede8 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -30,6 +30,12 @@ require_once __DIR__ . '/../autoload.php'; class AutoLoader { static protected $autoloadLocalClassesLower = null; + /** + * @private Only public for ExtensionRegistry + * @var string[] Namespace (ends with \) => Path (ends with /) + */ + static public $psr4Namespaces = []; + /** * autoload - take a class name and attempt to load it * @@ -67,6 +73,28 @@ class AutoLoader { } } + if ( !$filename && strpos( $className, '\\' ) !== false ) { + // This class is namespaced, so try looking at the namespace map + $prefix = $className; + while ( false !== $pos = strrpos( $prefix, '\\' ) ) { + // Check to see if this namespace prefix is in the map + $prefix = substr( $className, 0, $pos + 1 ); + if ( isset( self::$psr4Namespaces[$prefix] ) ) { + $relativeClass = substr( $className, $pos + 1 ); + // Build the expected filename, and see if it exists + $file = self::$psr4Namespaces[$prefix] . + str_replace( '\\', '/', $relativeClass ) . '.php'; + if ( file_exists( $file ) ) { + $filename = $file; + break; + } + } + + // Remove trailing separator for next iteration + $prefix = rtrim( $prefix, '\\' ); + } + } + if ( !$filename ) { // Class not found; let the next autoloader try to find it return; @@ -81,18 +109,6 @@ class AutoLoader { require $filename; } - /** - * Force a class to be run through the autoloader, helpful for things like - * Sanitizer that have define()s outside of their class definition. Of course - * this wouldn't be necessary if everything in MediaWiki was class-based. Sigh. - * - * @param string $class - * @return bool Return the results of class_exists() so we know if we were successful - */ - static function loadClass( $class ) { - return class_exists( $class ); - } - /** * Method to clear the protected class property $autoloadLocalClassesLower. * Used in tests. @@ -100,6 +116,22 @@ class AutoLoader { static function resetAutoloadLocalClassesLower() { self::$autoloadLocalClassesLower = null; } + + /** + * Get a mapping of namespace => file path + * The namespaces should follow the PSR-4 standard for autoloading + * + * @see + * @private Only public for usage in AutoloadGenerator + * @since 1.31 + * @return string[] + */ + public static function getAutoloadNamespaces() { + return [ + 'MediaWiki\\Linker\\' => __DIR__ .'/linker/' + ]; + } } +AutoLoader::$psr4Namespaces = AutoLoader::getAutoloadNamespaces(); spl_autoload_register( [ 'AutoLoader', 'autoload' ] );